Completed
Push — master ( 6514ea...1cfd2b )
by Luke
02:55 queued 39s
created

migrationHasAlreadyBeenPublished()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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