Completed
Push — master ( 74e291...f48cd5 )
by Aivis
12s
created

Logger::formatMessage()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
1
<?php namespace Understand\UnderstandLaravel5;
2
3
use Understand\UnderstandLaravel5\FieldProvider;
4
use Understand\UnderstandLaravel5\Handlers\BaseHandler;
5
6
class Logger
7
{
8
9
    /**
10
     * Field provider
11
     *
12
     * @var Understand\UnderstandLaravel\FieldProvider
13
     */
14
    protected $fieldProvider;
15
16
    /**
17
     * Transport layer
18
     *
19
     * @var Understand\UnderstandLaravel\Handlers\BaseHandler
20
     */
21
    protected $handler;
22
23
    /**
24
     * Specifies whether logger should throw an exception of issues detected
25
     *
26
     * @var bool
27
     */
28
    protected $silent = true;
29
30
    /**
31
     * @param \Understand\UnderstandLaravel\FieldProvider $fieldProvider
32
     * @param \Understand\UnderstandLaravel\Handlers\BaseHandler $handler
33
     * @param bool $silent
34
     */
35
    public function __construct(FieldProvider $fieldProvider, BaseHandler $handler, $silent = true)
36
    {
37
        $this->setFieldProvider($fieldProvider);
38
        $this->setHandler($handler);
39
        $this->silent = $silent;
40
    }
41
42
    /**
43
     * Resolve additonal fields and send event
44
     *
45
     * @param mixed $log
46
     * @param array $additional
47
     * @return array
48
     */
49
    public function log($log, array $additional = [])
50
    {
51
        $event = $this->prepare($log, $additional);
52
53
        return $this->send($event);
54
    }
55
56
    /**
57
     * Send multiple events
58
     *
59
     * @param array $data
0 ignored issues
show
Bug introduced by
There is no parameter named $data. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
60
     * @return array
61
     */
62
    public function bulkLog(array $events, array $additional = [])
63
    {
64
        foreach ($events as $key => $event)
65
        {
66
            $events[$key] = $this->prepare($event, $additional);
67
        }
68
69
        return $this->send($events);
70
    }
71
72
    /**
73
     * Format data
74
     *
75
     * @param mixed $log
76
     * @param array $additional
77
     * @return type
78
     */
79
    protected function prepare($log, array $additional = [])
80
    {
81
        // integer, float, string or boolean as message
82
        if (is_scalar($log))
83
        {
84
            $log = ['message' => $log];
85
        }
86
        
87
        if (isset($log['message']))
88
        {
89
            $log['message'] = $this->formatMessage($log['message']);
90
        }
91
92
        // resolve additonal properties from field providers
93
        $data = $this->fieldProvider->resolveValues($additional);
94
95
        $event = $data + $log;
96
97
        if (!isset($event['timestamp']))
98
        {
99
            $event['timestamp'] = round(microtime(true) * 1000);
100
        }
101
102
        return $event;
103
    }
104
    
105
    /**
106
     * Format message field
107
     * 
108
     * @param string $message
109
     * @return string
110
     */
111
    protected function formatMessage($message)
112
    {
113
        if ( ! is_bool($message))
114
        {
115
            return (string)$message;
116
        }
117
        
118
        // cast boolean values to "1" or "0" strings
119
        if ($message)
120
        {
121
            return '1';
122
        }
123
        
124
        return '0';
125
    }
126
127
    /**
128
     * Set handler
129
     *
130
     * @param BaseHandler $handler
131
     */
132
    public function setHandler(BaseHandler $handler)
133
    {
134
        $this->handler = $handler;
0 ignored issues
show
Documentation Bug introduced by
It seems like $handler of type object<Understand\Unders...5\Handlers\BaseHandler> is incompatible with the declared type object<Understand\Unders...l\Handlers\BaseHandler> of property $handler.

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...
135
    }
136
137
    /**
138
     * Set field provider
139
     *
140
     * @param \Understand\UnderstandLaravel\FieldProvider $fieldProvider
141
     */
142
    public function setFieldProvider(FieldProvider $fieldProvider)
143
    {
144
        $this->fieldProvider = $fieldProvider;
0 ignored issues
show
Documentation Bug introduced by
It seems like $fieldProvider of type object<Understand\Unders...Laravel5\FieldProvider> is incompatible with the declared type object<Understand\Unders...dLaravel\FieldProvider> of property $fieldProvider.

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...
145
    }
146
147
    /**
148
     * Send data to storage
149
     *
150
     * @param array $requestData
0 ignored issues
show
Bug introduced by
There is no parameter named $requestData. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
151
     * @return mixed
152
     */
153
    protected function send(array $event)
154
    {
155
        try
156
        {
157
            return $this->handler->handle($event);
158
        }
159
        catch (\Exception $ex)
160
        {
161
            if (! $this->silent)
162
            {
163
                throw new $ex;
164
            }
165
166
            return false;
167
        }
168
    }
169
170
}
171