Completed
Push — master ( 925ecf...691399 )
by Aivis
08:17 queued 02:32
created

ExceptionLogger::logError()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 35
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 8.439
c 0
b 0
f 0
cc 6
eloc 16
nc 7
nop 3
1
<?php namespace Understand\UnderstandLaravel5;
2
3
use Illuminate\Config\Repository;
4
use Exception;
5
use Throwable;
6
7
class ExceptionLogger
8
{
9
10
    /**
11
     * Log writer
12
     *
13
     * @var Logger
14
     */
15
    protected $logger;
16
17
    /**
18
     * Exception encoder
19
     *
20
     * @var ExceptionEncoder
21
     */
22
    protected $encoder;
23
24
    /**
25
     * Configuration
26
     *
27
     * @var array
28
     */
29
    protected $config;
30
31
    /**
32
     * @param Logger $logger
33
     * @param ExceptionEncoder $encoder
34
     * @param Repository $config
35
     */
36
    public function __construct(
37
        Logger $logger,
38
        ExceptionEncoder $encoder,
39
        Repository $config
40
    )
41
    {
42
        $this->logger = $logger;
43
        $this->encoder = $encoder;
44
        $this->config = $config;
0 ignored issues
show
Documentation Bug introduced by
It seems like $config of type object<Illuminate\Config\Repository> is incompatible with the declared type array of property $config.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
45
46
        $this->encoder->setProjectRoot($this->config->get('understand-laravel.project_root'));
47
    }
48
49
    /**
50
     * Send PHP exception to Understand.io
51
     *
52
     * @deprecated
53
     * @param mixed $error
54
     * @return void
55
     */
56
    public function log($error)
57
    {
58
        if ( ! $this->canHandle($error))
59
        {
60
            return;
61
        }
62
63
        $errorArr = $this->encoder->exceptionToArray($error);
64
65
        return $this->dispatch($errorArr);
66
    }
67
68
    /**
69
     * @param string $level
70
     * @param mixed $message
71
     * @param mixed $context
72
     * @return void
73
     */
74
    public function logError($level, $message, $context)
75
    {
76
        if ( ! $this->canHandle($message))
77
        {
78
            return;
79
        }
80
81
        if ($message instanceof Exception || $message instanceof Throwable)
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
82
        {
83
            $log = $this->encoder->exceptionToArray($message);
84
        }
85
        // integer, float, string or boolean as message
86
        else if (is_scalar($message))
87
        {
88
            $log = [
89
                'message' => $message
90
            ];
91
92
            $log = $this->encoder->setCurrentStackTrace($log);
93
        }
94
        else
95
        {
96
            $log = (array)$message;
97
            $log = $this->encoder->setCurrentStackTrace($log);
98
        }
99
100
        $log['level'] = $level;
101
102
        if ($context)
103
        {
104
            $log['context'] = (array)$context;
105
        }
106
107
        return $this->dispatch($log);
108
    }
109
110
    /**
111
     * @param array $errorArr
112
     * @return array
113
     */
114
    protected function dispatch(array $errorArr)
115
    {
116
        $errorArr['tags'] = ['error_log'];
117
118
        $additionalFields = $this->getMetaFields();
119
        $customFields = $this->config->get('understand-laravel.errors.meta', []);
0 ignored issues
show
Bug introduced by
The method get cannot be called on $this->config (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
120
121
        return $this->logger->log($errorArr, $additionalFields, $customFields);
122
    }
123
124
    /**
125
     * @return array
126
     */
127
    protected function getMetaFields()
128
    {
129
        return [
130
            'session_id' => 'UnderstandFieldProvider::getSessionId',
131
            'request_id' => 'UnderstandFieldProvider::getProcessIdentifier',
132
            'group_id' => 'UnderstandFieldProvider::getGroupId',
133
            'user_id' => 'UnderstandFieldProvider::getUserId',
134
            'env' => 'UnderstandFieldProvider::getEnvironment',
135
            'url' => 'UnderstandFieldProvider::getUrl',
136
            'method' => 'UnderstandFieldProvider::getRequestMethod',
137
            'client_ip' => 'UnderstandFieldProvider::getClientIp',
138
            'user_agent' => 'UnderstandFieldProvider::getClientUserAgent',
139
            'laravel_version' => 'UnderstandFieldProvider::getLaravelVersion',
140
            'sql_queries' => 'UnderstandFieldProvider::getSqlQueries',
141
            'artisan_command' => 'UnderstandFieldProvider::getArtisanCommandName',
142
            'console' => 'UnderstandFieldProvider::getRunningInConsole',
143
            'logger_version' => 'UnderstandFieldProvider::getLoggerVersion',
144
        ];
145
    }
146
147
    /**
148
     * @param mixed $error
149
     * @return bool
150
     */
151
    protected function canHandle($error)
0 ignored issues
show
Unused Code introduced by
The parameter $error is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
152
    {
153
        return (bool)$this->config->get('understand-laravel.enabled');
0 ignored issues
show
Bug introduced by
The method get cannot be called on $this->config (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
154
    }
155
}