Completed
Push — master ( 0f3f6d...5694f4 )
by ARCANEDEV
14s
created

ErrorTracker::track()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
crap 1
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 105
    protected function getModel()
26
    {
27 105
        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 105
    public function track(Exception $exception)
43
    {
44 105
        return $this->getModel()->newQuery()->firstOrCreate([
45 105
            'code'    => $this->getCode($exception),
46 105
            'message' => $exception->getMessage(),
47 39
        ])->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 105
    public function getCode(Exception $exception)
63
    {
64 105
        if (method_exists($exception, 'getCode') && $code = $exception->getCode())
65 105
            return $code;
66
67 3
        if (method_exists($exception, 'getStatusCode') && $code = $exception->getStatusCode())
68 3
            return $code;
69
70
        return null;
71
    }
72
}
73