Completed
Pull Request — master (#12)
by Aivis
02:03
created

UnderstandLaravel5ServiceProvider::handleEvent()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 32
rs 8.439
cc 5
eloc 17
nc 7
nop 3
1
<?php namespace Understand\UnderstandLaravel5;
2
3
use Illuminate\Support\ServiceProvider;
4
use Illuminate\Foundation\AliasLoader;
5
use Illuminate\Support\Str;
6
use Illuminate\Foundation\Application;
7
8
class UnderstandLaravel5ServiceProvider extends ServiceProvider
9
{
10
11
	/**
12
	 * Indicates if loading of the provider is deferred.
13
	 *
14
	 * @var bool
15
	 */
16
	protected $defer = false;
17
18
	/**
19
	 * Bootstrap the application events.
20
	 *
21
	 * @return void
22
	 */
23
	public function boot()
24
	{
25
        $configPath = __DIR__ . '/../../config/understand-laravel.php';
26
        $this->publishes([$configPath => config_path('understand-laravel.php')], 'config');
27
28
        if ($this->app['config']->get('understand-laravel.log_types.eloquent_log.enabled'))
29
        {
30
            $this->listenEloquentEvents();
31
        }
32
33
        if ($this->app['config']->get('understand-laravel.log_types.laravel_log.enabled'))
34
        {
35
            $this->listenLaravelEvents();
36
        }
37
	}
38
39
	/**
40
	 * Register the service provider.
41
	 *
42
	 * @return void
43
	 */
44
	public function register()
45
	{
46
		$this->registerConfig();
47
        $this->registerFieldProvider();
48
        $this->registerTokenProvider();
49
        $this->registerLogger();
50
        $this->registerModelEventListenerProvider();
51
        $this->registerExceptionEncoder();
52
        $this->registerExceptionLogger();
53
	}
54
55
    /**
56
     * Register config
57
     *
58
     * @return void
59
     */
60
    protected function registerConfig()
61
    {
62
        $configPath = __DIR__ . '/../../config/understand-laravel.php';
63
        $this->mergeConfigFrom($configPath, 'understand-laravel');
64
    }
65
66
    /**
67
     * Register field provider
68
     *
69
     * @return void
70
     */
71
    protected function registerFieldProvider()
72
    {
73
        $this->app->bind('understand.field-provider', function($app)
74
        {
75
            $fieldProvider = new FieldProvider();
76
77
	    if ($app['config']['session.driver']) 
78
	    {
79
                $fieldProvider->setSessionStore($app['session.store']);
80
            }
81
            $fieldProvider->setRouter($app['router']);
82
            $fieldProvider->setRequest($app['request']);
83
            $fieldProvider->setEnvironment($app->environment());
84
            $fieldProvider->setTokenProvider($app['understand.token-provider']);
85
86
            return $fieldProvider;
87
        });
88
89
        $this->app->booting(function()
90
        {
91
            $loader = AliasLoader::getInstance();
92
            $loader->alias('UnderstandFieldProvider', 'Understand\UnderstandLaravel5\Facades\UnderstandFieldProvider');
93
        });
94
    }
95
96
    /**
97
     * Register token generator class
98
     *
99
     * @return void
100
     */
101
    protected function registerTokenProvider()
102
    {
103
        $this->app->singleton('understand.token-provider', function()
104
        {
105
            return new TokenProvider();
106
        });
107
    }
108
109
    /**
110
     * Register exception encoder
111
     *
112
     * @return void
113
     */
114
    protected function registerExceptionEncoder()
115
    {
116
        $this->app->bind('understand.exception-encoder', function()
117
        {
118
            return new ExceptionEncoder;
119
        });
120
    }
121
122
    /**
123
     * Register exception logger
124
     *
125
     * @return void
126
     */
127
    protected function registerExceptionLogger()
128
    {
129
        $this->app->bind('understand.exceptionLogger', function($app)
130
        {
131
            $logger = $app['understand.logger'];
132
            $encoder = $app['understand.exception-encoder'];
133
134
            return new ExceptionLogger($logger, $encoder, $app['config']);
135
        });
136
137
        $this->app->booting(function()
138
        {
139
            $loader = AliasLoader::getInstance();
140
            $loader->alias('UnderstandExceptionLogger', 'Understand\UnderstandLaravel5\Facades\UnderstandExceptionLogger');
141
        });
142
    }
143
144
    /**
145
     * Register understand logger
146
     *
147
     * @return void
148
     */
149
    protected function registerLogger()
150
    {
151
        $this->app->singleton('understand.logger', function($app)
152
        {
153
            $fieldProvider = $app['understand.field-provider'];
154
            $handler = $this->resolveHandler($app);
155
            $silent = $app['config']->get('understand-laravel.silent');
156
157
            return new Logger($fieldProvider, $handler, $silent);
158
        });
159
160
        $this->app->booting(function()
161
        {
162
            $loader = AliasLoader::getInstance();
163
            $loader->alias('UnderstandLogger', 'Understand\UnderstandLaravel5\Facades\UnderstandLogger');
164
        });
165
    }
166
167
    /**
168
     * Return default handler
169
     *
170
     * @param type $app
171
     * @return mixed
172
     * @throws \ErrorException
173
     */
174
    protected function resolveHandler($app)
175
    {
176
        $inputToken = $app['config']->get('understand-laravel.token');
177
178
        $apiUrl = $app['config']->get('understand-laravel.url', 'https://api.understand.io');
179
        $silent = $app['config']->get('understand-laravel.silent');
180
        $handlerType = $app['config']->get('understand-laravel.handler');
181
        $sslBundlePath = $app['config']->get('understand-laravel.ssl_ca_bundle');
182
183
        if ($handlerType == 'async')
184
        {
185
            return new Handlers\AsyncHandler($inputToken, $apiUrl, $silent, $sslBundlePath);
186
        }
187
188
        if ($handlerType == 'sync')
189
        {
190
            return new Handlers\SyncHandler($inputToken, $apiUrl, $silent, $sslBundlePath);
191
        }
192
193
        if ($handlerType == 'queue')
194
        {
195
            return new Handlers\LaravelQueueHandler($inputToken, $apiUrl, $silent, $sslBundlePath);
196
        }
197
198
        throw new \ErrorException('understand-laravel handler misconfiguration:' . $handlerType);
199
    }
200
201
    /**
202
     * Register model event listener provider
203
     *
204
     * @return void
205
     */
206
    protected function registerModelEventListenerProvider()
207
    {
208
        $this->app->bind('understand.model-event-listener-provider', function($app)
209
        {
210
            $logger = $app['understand.logger'];
211
            $additional = $app['config']->get('understand-laravel.additional.model_log', []);
212
213
            return new ModelEventListener($logger, $additional);
214
        });
215
    }
216
217
    /**
218
     * Listen Laravel logs
219
     *
220
     * @return void
221
     */
222
    protected function listenLaravelEvents()
223
    {
224
        // only Laravel versions below L5.4 supports `illuminate.log`
225
        if (Str::startsWith(Application::VERSION, ['5.0', '5.1', '5.2', '5.3']))
226
        {
227
            $this->app['events']->listen('illuminate.log', function($level, $message, $context)
228
            {
229
                $this->handleEvent($level, $message, $context);
230
            });
231
        }
232
        else
233
        {
234
            // starting from L5.4 MessageLogged event class was introduced
235
            // https://github.com/laravel/framework/commit/57c82d095c356a0fe0f9381536afec768cdcc072
236
            $this->app['events']->listen('Illuminate\Log\Events\MessageLogged', function($log) 
237
            {
238
239
                $this->handleEvent($log->level, $log->message, $log->context);
240
            });
241
        }
242
    }
243
244
    /**
245
     * Handle a new log event
246
     * 
247
     * @param string $level
248
     * @param mixed $message
249
     * @param array $context
250
     * @return void
251
     */
252
    protected function handleEvent($level, $message, $context)
253
    {
254
        if ($message instanceof Exceptions\HandlerException)
255
        {
256
            return;
257
        }
258
        else if ($message instanceof \Exception)
259
        {
260
            $log = $this->app['understand.exception-encoder']->exceptionToArray($message);
261
            $log['tags'] = ['exception_log'];
262
        }
263
        else if (is_string($message))
264
        {
265
            $log['message'] = $message;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$log was never initialized. Although not strictly required by PHP, it is generally a good practice to add $log = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
266
            $log['tags'] = ['laravel_log'];
267
        }
268
        else
269
        {
270
            $log = $message;
271
            $log['tags'] = ['laravel_log'];
272
        }
273
274
        if ($context)
0 ignored issues
show
Bug Best Practice introduced by
The expression $context of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
275
        {
276
            $log['context'] = $context;
277
        }
278
279
        $log['level'] = $level;
280
281
        $additional = $this->app['config']->get('understand-laravel.log_types.laravel_log.meta', []);
282
        $this->app['understand.logger']->log($log, $additional);
283
    }
284
    
285
    /**
286
     * Listen eloquent model events and log them
287
     *
288
     * @return void
289
     */
290
    protected function listenEloquentEvents()
291
    {
292
        $modelLogger = $this->app['understand.model-event-listener-provider'];
293
294
        $events = [
295
            'eloquent.created*' => 'created',
296
            'eloquent.updated*' => 'updated',
297
            'eloquent.deleted*' => 'deleted',
298
            'eloquent.restored*' => 'restored',
299
        ];
300
301
        foreach ($events as $listenerName => $eventName)
302
        {
303
            $this->app['events']->listen($listenerName, function($model) use($modelLogger, $eventName)
304
            {
305
                $modelLogger->logModelEvent($eventName, $model);
306
            });
307
        }
308
    }
309
310
    /**
311
     * Get the services provided by the provider.
312
     *
313
     * @return array
314
     */
315
    public function provides()
316
    {
317
        return [
318
            'understand.field-provider',
319
            'understand.logger',
320
            'understand.model-event-listener-provider',
321
            'understand.exception-encoder',
322
            'understand.exceptionLogger'
323
        ];
324
    }
325
}
326