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

EventLogger::logEvent()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 13
nc 4
nop 3
1
<?php namespace Understand\UnderstandLaravel5;
2
3
use Illuminate\Config\Repository;
4
5
class EventLogger
6
{
7
8
    /**
9
     * Log writer
10
     *
11
     * @var Logger
12
     */
13
    protected $logger;
14
15
    /**
16
     * Configuration
17
     *
18
     * @var array
19
     */
20
    protected $config;
21
22
    /**
23
     * @param Logger $logger
24
     * @param Repository $config
25
     */
26
    public function __construct(
27
        Logger $logger,
28
        Repository $config
29
    )
30
    {
31
        $this->logger = $logger;
32
        $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...
33
    }
34
35
    /**
36
     * @param $level
37
     * @param $message
38
     * @param $context
39
     */
40
    public function logEvent($level, $message, $context)
41
    {
42
        // integer, float, string or boolean as message
43
        if (is_scalar($message))
44
        {
45
            $log = [
46
                'message' => $message
47
            ];
48
        }
49
        else
50
        {
51
            $log = (array)$message;
52
        }
53
54
        $log['tags'] = ['laravel_log'];
55
        $log['level'] = $level;
56
57
        if ($context)
58
        {
59
            $log['context'] = (array)$context;
60
        }
61
62
        $additionalFields = $this->getMetaFields();
63
        $customFields = $this->config->get('understand-laravel.events.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...
64
65
        $this->logger->log($log, $additionalFields, $customFields);
66
    }
67
68
    /**
69
     * @return array
70
     */
71
    protected function getMetaFields()
72
    {
73
        return [
74
            'session_id' => 'UnderstandFieldProvider::getSessionId',
75
            'request_id' => 'UnderstandFieldProvider::getProcessIdentifier',
76
            'user_id' => 'UnderstandFieldProvider::getUserId',
77
            'env' => 'UnderstandFieldProvider::getEnvironment',
78
            'url' => 'UnderstandFieldProvider::getUrl',
79
            'method' => 'UnderstandFieldProvider::getRequestMethod',
80
            'client_ip' => 'UnderstandFieldProvider::getClientIp',
81
        ];
82
    }
83
}