CartServiceProvider   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 45
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 14 2
A register() 0 19 3
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