Completed
Push — master ( a52a56...4b8006 )
by Renato
09:08
created

NwLaravelServiceProvider::bootActivityLog()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 11
ccs 0
cts 0
cp 0
crap 6
rs 9.4285
1
<?php
2
namespace NwLaravel\ServiceProvider;
3
4
use Carbon\Carbon;
5
use NwLaravel\Socialite\OlxProvider;
6
use Laravel\Socialite\Facades\Socialite;
7
use NwLaravel\Validation\ValidatorResolver;
8
use Illuminate\Support\ServiceProvider;
9
use Symfony\Component\Translation\Translator;
10
use Symfony\Component\Translation\Loader\ArrayLoader;
11
use NwLaravel\Activity\ActivityManager;
12
use NwLaravel\ActivityLog\Commands\CleanLogCommand;
13
/**
14
 * Class NwLaravelServiceProvider
15
 */
16
class NwLaravelServiceProvider extends ServiceProvider
17
{
18
    /**
19
     * Indicates if loading of the provider is deferred.
20
     *
21
     * @var bool
22
     */
23
    protected $defer = false;
24
25
    /**
26
     * Bootstrap any necessary services.
27
     *
28
     * @return void
29
     */
30
    public function boot()
31
    {
32
        // Publish config files
33
        $this->publishes([
34
            __DIR__.'/../../config/nwlaravel.php' => config_path('nwlaravel.php'),
35
        ], 'config');
36
37
        $this->bootValidator();
38
        $this->bootTranslatorCarbon();
39
        $this->bootActivityLog();
40
        $this->bootOlxDriver();
41
    }
42
43
    /**
44
     * Register the service provider.
45
     *
46
     * @return void
47
     */
48
    public function register()
49
    {
50
        $this->registerActivityLog();
51
    }
52
53
    /**
54
     * Boot Translator Carbon
55
     *
56
     * @return void
57
     */
58
    protected function bootTranslatorCarbon()
59
    {
60
        $locale = $this->app['config']->get('app.locale');
61
        $path = __DIR__.'/../../resources/lang/'.$locale.'/carbon.php';
62
63
        if (file_exists($path)) {
64
            $translator = new Translator($locale);
65
            $translator->addLoader('array', new ArrayLoader());
66
            $translator->addResource('array', require $path, $locale);
67
            Carbon::setTranslator($translator);
68
        }
69
    }
70
71
    /**
72
     * Boot Validator
73
     *
74
     * @return void
75
     */
76
    protected function bootValidator()
77
    {
78
        $this->app['validator']->resolver(function ($translator, $data, $rules, $messages, $customAttributes) {
79
            return new ValidatorResolver($translator, $data, $rules, $messages, $customAttributes);
80
        });
81
    }
82
83
    /**
84
     * Boot Activity Log
85
     *
86
     * @return void
87
     */
88
    protected function bootActivityLog()
89
    {
90
        // Publish migration
91
        $files = glob(database_path('/migrations/*_create_activity_log_table.php'));
92
        if (count($files) == 0) {
93
            $timestamp = date('Y_m_d_His', time());
94
            $this->publishes([
95
                __DIR__ . "/../../migrations/create_activity_log_table.stub" => database_path("/migrations/{$timestamp}_create_activity_log_table.php"),
96
            ], 'migrations');
97
        }
98
    }
99
100
    /**
101
     * Registero Activity Log
102
     *
103
     * @return void
104
     */
105
    protected function registerActivityLog()
106
    {
107
        $config = $this->app['config'];
108
        $handler = $this->app->make($config->get('nwlaravel.activity.handler'));
109
        $activityManager = new ActivityManager($handler, $this->app['auth'], $config);
110
111
        $this->app->bind('nwlaravel.activity', $activityManager);
112
        $this->app->alias('nwlaravel.activity', ActivityManager::class);
113
114
        $cleanLogCommand = new CleanLogCommand;
0 ignored issues
show
Bug introduced by
The call to CleanLogCommand::__construct() misses a required argument $activity.

This check looks for function calls that miss required arguments.

Loading history...
115
        $this->app->bind('nwlaravel.command.activity:clean', $cleanLogCommand);
0 ignored issues
show
Documentation introduced by
$cleanLogCommand is of type object<NwLaravel\Activit...mmands\CleanLogCommand>, but the function expects a object<Closure>|string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
116
        $this->commands(['nwlaravel.command.activity:clean']);
117
    }
118
119
    /**
120
     * Boot OlxDriver
121
     *
122
     * @return void
123
     */
124
    protected function bootOlxDriver()
125
    {
126
        if (class_exists(Socialite::class)) {
127
            Socialite::extend('olx', function ($app) {
128
                $config = $app['config']['services.olx'];
129
                return new OlxProvider(
130
                    $app['request'],
131
                    $config['client_id'],
132
                    $config['client_secret'],
133
                    $config['redirect']
134
                );
135
            });
136
        }
137
    }
138
}
139