ServiceProvider   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 11 2
B register() 0 44 5
A provides() 0 4 1
A registerMigrations() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of ibrand/laravel-shopping-cart.
5
 *
6
 * (c) iBrand <https://www.ibrand.cc>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace iBrand\Shoppingcart;
13
14
use iBrand\Shoppingcart\Storage\SessionStorage;
15
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
16
17
/**
18
 * Service provider for Laravel.
19
 */
20
class ServiceProvider extends LaravelServiceProvider
21
{
22
    /**
23
     * Indicates if loading of the provider is deferred.
24
     *
25
     * @var bool
26
     */
27
    protected $defer = true;
28
29
    /**
30
     * Boot the provider.
31
     */
32
    public function boot()
33
    {
34
        if ($this->app->runningInConsole()) {
35
            $this->registerMigrations();
36
        }
37
        //
38
        //publish a config file
39
        $this->publishes([
40
            __DIR__ . '/config.php' => config_path('ibrand/cart.php'),
41
        ]);
42
    }
43
44
    /**
45
     * Register the service provider.
46
     */
47
    public function register()
48
    {
49
        // merge configs
50
        $this->mergeConfigFrom(
51
            __DIR__ . '/config.php', 'ibrand.cart'
52
        );
53
54
        $this->app->singleton(Cart::class, function ($app) {
55
            $storage = config('ibrand.cart.storage');
56
57
            $cart = new Cart(new $storage(), $app['events']);
58
59
            if (SessionStorage::class == $storage) {
60
                return $cart;
61
            }
62
63
            //The below code is used of database storage
64
            $currentGuard = 'default';
65
            $user = null;
66
67
            if ($defaultGuard = $app['auth']->getDefaultDriver()) {
68
                $currentGuard = $defaultGuard;
69
                $user = auth($currentGuard)->user();
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
70
            }
71
72
            if ($user) {
73
                //The cart name like `cart.{guard}.{user_id}`: cart.api.1
74
                $aliases = config('ibrand.cart.aliases');
75
76
                if (isset($aliases[$currentGuard])) {
77
                    $currentGuard = $aliases[$currentGuard];
78
                }
79
80
                $cart->name($currentGuard . '.' . $user->id);
81
82
            } else {
83
                throw new Exception('Invalid auth.');
84
            }
85
86
            return $cart;
87
        });
88
89
        $this->app->alias(Cart::class, 'cart');
90
    }
91
92
    /**
93
     * Get the services provided by the provider.
94
     *
95
     * @return array
96
     */
97
    public function provides()
98
    {
99
        return [Cart::class, 'cart'];
100
    }
101
102
    /**
103
     * load migration files.
104
     */
105
    protected function registerMigrations()
106
    {
107
        return $this->loadMigrationsFrom(__DIR__ . '/../migrations');
108
    }
109
}
110