1
|
|
|
<?php namespace Arcanedev\LaravelTracker\Trackers; |
2
|
|
|
|
3
|
|
|
use Arcanedev\LaravelTracker\Contracts\Trackers\ErrorTracker as ErrorTrackerContract; |
4
|
|
|
use Arcanedev\LaravelTracker\Support\BindingManager; |
5
|
|
|
use Exception; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class ErrorTracker |
9
|
|
|
* |
10
|
|
|
* @package Arcanedev\LaravelTracker\Trackers |
11
|
|
|
* @author ARCANEDEV <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class ErrorTracker extends AbstractTracker implements ErrorTrackerContract |
14
|
|
|
{ |
15
|
|
|
/* ----------------------------------------------------------------- |
16
|
|
|
| Getters and Setters |
17
|
|
|
| ----------------------------------------------------------------- |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Get the model. |
22
|
|
|
* |
23
|
|
|
* @return \Arcanedev\LaravelTracker\Models\Error |
24
|
|
|
*/ |
25
|
6 |
|
protected function getModel() |
26
|
|
|
{ |
27
|
6 |
|
return $this->makeModel(BindingManager::MODEL_ERROR); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/* ----------------------------------------------------------------- |
31
|
|
|
| Main Methods |
32
|
|
|
| ----------------------------------------------------------------- |
33
|
|
|
*/ |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Track the exception error. |
37
|
|
|
* |
38
|
|
|
* @param \Exception $exception |
39
|
|
|
* |
40
|
|
|
* @return int |
41
|
|
|
*/ |
42
|
6 |
|
public function track(Exception $exception) |
43
|
|
|
{ |
44
|
6 |
|
return $this->getModel()->newQuery()->firstOrCreate([ |
45
|
6 |
|
'code' => $this->getCode($exception), |
46
|
6 |
|
'message' => $exception->getMessage(), |
47
|
6 |
|
])->getKey(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/* ----------------------------------------------------------------- |
51
|
|
|
| Other Methods |
52
|
|
|
| ----------------------------------------------------------------- |
53
|
|
|
*/ |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get the code from the exception. |
57
|
|
|
* |
58
|
|
|
* @param \Exception $exception |
59
|
|
|
* |
60
|
|
|
* @return int|mixed|null |
61
|
|
|
*/ |
62
|
6 |
|
public function getCode(Exception $exception) |
63
|
|
|
{ |
64
|
6 |
|
if (method_exists($exception, 'getCode') && $code = $exception->getCode()) |
65
|
3 |
|
return $code; |
66
|
|
|
|
67
|
3 |
|
if (method_exists($exception, 'getStatusCode') && $code = $exception->getStatusCode()) |
|
|
|
|
68
|
3 |
|
return $code; |
69
|
|
|
|
70
|
|
|
return null; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: