Completed
Push — master ( 4b8006...ac512f )
by Renato
06:07
created

NwLaravelServiceProvider::bootOlxDriver()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 14
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
        $this->bootPublishConfig();
33
34
        $this->bootValidator();
35
        $this->bootTranslatorCarbon();
36
        $this->bootActivityLog();
37
        $this->bootOlxDriver();
38
    }
39
40
    /**
41
     * Register the service provider.
42
     *
43
     * @return void
44
     */
45
    public function register()
46
    {
47
        $this->registerActivityLog();
48
    }
49
50
    /**
51
     * Boot Publish Config
52
     *
53
     * @return void
54
     */
55
    public function bootPublishConfig()
56
    {
57
        // Publish config files
58
        $this->publishes([
59
            __DIR__.'/../../config/nwlaravel.php' => config_path('nwlaravel.php'),
60
        ], 'config');
61
62
        // Merge config files
63
        $this->mergeConfigFrom(
64
            __DIR__.'/../config/nwlaravel.php', 'nwlaravel'
65
        );
66
    }
67
68
    /**
69
     * Boot Translator Carbon
70
     *
71
     * @return void
72
     */
73
    protected function bootTranslatorCarbon()
74
    {
75
        $locale = $this->app['config']->get('app.locale');
76
        $path = __DIR__.'/../../resources/lang/'.$locale.'/carbon.php';
77
78
        if (file_exists($path)) {
79
            $translator = new Translator($locale);
80
            $translator->addLoader('array', new ArrayLoader());
81
            $translator->addResource('array', require $path, $locale);
82
            Carbon::setTranslator($translator);
83
        }
84
    }
85
86
    /**
87
     * Boot Validator
88
     *
89
     * @return void
90
     */
91
    protected function bootValidator()
92
    {
93
        $this->app['validator']->resolver(function ($translator, $data, $rules, $messages, $customAttributes) {
94
            return new ValidatorResolver($translator, $data, $rules, $messages, $customAttributes);
95
        });
96
    }
97
98
    /**
99
     * Boot Activity Log
100
     *
101
     * @return void
102
     */
103
    protected function bootActivityLog()
104
    {
105
        // Publish migration
106
        $files = glob(database_path('/migrations/*_create_activity_log_table.php'));
107
        if (count($files) == 0) {
108
            $timestamp = date('Y_m_d_His', time());
109
            $this->publishes([
110
                __DIR__ . "/../../migrations/create_activity_log_table.stub" => database_path("/migrations/{$timestamp}_create_activity_log_table.php"),
111
            ], 'migrations');
112
        }
113
    }
114
115
    /**
116
     * Registero Activity Log
117
     *
118
     * @return void
119
     */
120
    protected function registerActivityLog()
121
    {
122
        $config = $this->app['config'];
123
        $handler = $this->app->make($config->get('nwlaravel.activity.handler'));
124
        $activityManager = new ActivityManager($handler, $this->app['auth'], $config);
125
126
        $this->app->bind('nwlaravel.activity', $activityManager);
127
        $this->app->alias('nwlaravel.activity', ActivityManager::class);
128
129
        $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...
130
        $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...
131
        $this->commands(['nwlaravel.command.activity:clean']);
132
    }
133
134
    /**
135
     * Boot OlxDriver
136
     *
137
     * @return void
138
     */
139
    protected function bootOlxDriver()
140
    {
141
        if (class_exists(Socialite::class)) {
142
            Socialite::extend('olx', function ($app) {
143
                $config = $app['config']['services.olx'];
144
                return new OlxProvider(
145
                    $app['request'],
146
                    $config['client_id'],
147
                    $config['client_secret'],
148
                    $config['redirect']
149
                );
150
            });
151
        }
152
    }
153
}
154