LaraCartServiceProvider::boot()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 2 Features 1
Metric Value
c 5
b 2
f 1
dl 0
loc 17
rs 9.4285
cc 2
eloc 10
nc 2
nop 0
1
<?php
2
3
namespace LukePOLO\LaraCart;
4
5
use Illuminate\Support\ServiceProvider;
6
7
/**
8
 * Class LaraCartServiceProvider.
9
 */
10
class LaraCartServiceProvider extends ServiceProvider
11
{
12
    /**
13
     * Perform post-registration booting of services.
14
     *
15
     * @return void
16
     */
17
    public function boot()
18
    {
19
        $this->publishes([
20
            __DIR__.'/config/laracart.php' => config_path('laracart.php'),
21
        ]);
22
23
        $this->mergeConfigFrom(
24
            __DIR__.'/config/laracart.php',
25
            'laracart'
26
        );
27
28
        if (!$this->migrationHasAlreadyBeenPublished()) {
29
            $this->publishes([
30
                __DIR__.'/database/migrations/add_cart_session_id_to_users_table.php.stub' => database_path('migrations/'.date('Y_m_d_His').'_add_cart_session_id_to_users_table.php'),
31
            ], 'migrations');
32
        }
33
    }
34
35
    /**
36
     * Register the service provider.
37
     *
38
     * @return void
39
     */
40
    public function register()
41
    {
42
        $this->app->singleton(LaraCart::SERVICE, function ($app) {
43
            return new LaraCart($app['session'], $app['events'], $app['auth']);
44
        }
45
        );
46
47
        $this->app->bind(
48
            LaraCart::HASH,
49
            function ($app, $data) {
50
                return md5(json_encode($data));
51
            }
52
        );
53
54
        $this->app->bind(
55
            LaraCart::RANHASH,
56
            function () {
57
                return str_random(40);
58
            }
59
        );
60
    }
61
62
    /**
63
     * Checks to see if the migration has already been published.
64
     *
65
     * @return bool
66
     */
67
    protected function migrationHasAlreadyBeenPublished()
68
    {
69
        $files = glob(database_path('migrations/*_add_cart_session_id_to_users_table.php'));
70
71
        return count($files) > 0;
72
    }
73
}
74