CartServiceProvider::boot()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Treestoneit\ShoppingCart;
4
5
use Illuminate\Support\ServiceProvider;
6
use Treestoneit\ShoppingCart\Models\Cart;
7
8
class CartServiceProvider extends ServiceProvider
9
{
10
    /**
11
     * Bootstrap the application services.
12
     */
13
    public function boot()
14
    {
15
        if ($this->app->runningInConsole()) {
16
            $this->loadMigrationsFrom(__DIR__.'/../database/migrations');
17
18
            $this->publishes([
19
                __DIR__.'/../config/config.php' => config_path('shopping-cart.php'),
20
            ], 'config');
21
22
            $this->publishes([
23
                __DIR__.'/../database/migrations' => database_path('migrations'),
24
            ], 'migrations');
25
        }
26
    }
27
28
    /**
29
     * Register the application services.
30
     * TODO dedicated cart factory class
31
     * TODO replace Laravel framework facades with contracts.
32
     */
33
    public function register()
34
    {
35
        $this->mergeConfigFrom(__DIR__.'/../config/config.php', 'shopping-cart');
36
37
        $this->app->singleton('cart', function ($app) {
38
            if ($app['session']->has('cart')) {
39
                return CartManager::fromSessionIdentifier($app['session']->get('cart'));
40
            }
41
42
            if ($app['auth']->check()) {
43
                return CartManager::fromUserId($app['auth']->user());
44
            }
45
46
            return new CartManager(new Cart());
47
        });
48
49
        $this->app->alias('cart', CartManager::class);
50
        $this->app->alias('cart', CartContract::class);
51
    }
52
}
53