NwLaravelServiceProvider::bootValidator()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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