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

Logger::resolveData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 3
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
     * Version Number
11
     */
12
    const VERSION = 2.0;
13
14
    /**
15
     * Field provider
16
     *
17
     * @var FieldProvider
18
     */
19
    protected $fieldProvider;
20
21
    /**
22
     * Transport layer
23
     *
24
     * @var BaseHandler
25
     */
26
    protected $handler;
27
28
    /**
29
     * @param FieldProvider $fieldProvider
30
     * @param BaseHandler $handler
31
     * @param bool $silent
0 ignored issues
show
Bug introduced by
There is no parameter named $silent. 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...
32
     */
33
    public function __construct(FieldProvider $fieldProvider, BaseHandler $handler)
34
    {
35
        $this->setFieldProvider($fieldProvider);
36
        $this->setHandler($handler);
37
    }
38
39
    /**
40
     * @param $log
41
     * @param array $additional
42
     * @param array $customFields
43
     * @return mixed
44
     */
45
    public function log($log, array $additional = [], array $customFields = [])
46
    {
47
        $event = $this->prepare($log, $additional, $customFields);
48
49
        return $this->send($event);
50
    }
51
52
    /**
53
     * @param $log
54
     * @param array $additional
55
     * @param array $customFields
56
     * @return array
57
     */
58
    protected function prepare($log, array $additional = [], array $customFields = [])
59
    {
60
        // integer, float, string or boolean as message
61
        if (is_scalar($log))
62
        {
63
            $log = ['message' => $log];
64
        }
65
        
66
        if (isset($log['message']))
67
        {
68
            $log['message'] = $this->formatMessage($log['message']);
69
        }
70
71
        // resolve additional properties from field providers
72
        $data = $this->resolveData($log, $additional, $customFields);
73
74
        $event = $data + $log;
75
76
        if (!isset($event['timestamp']))
77
        {
78
            $event['timestamp'] = round(microtime(true) * 1000);
79
        }
80
81
        return $event;
82
    }
83
84
    /**
85
     * @param $log
86
     * @param array $additional
87
     * @param array $customFields
88
     * @return array
89
     */
90
    protected function resolveData($log, array $additional = [], array $customFields = [])
91
    {
92
        $data = $this->fieldProvider->resolveValues($additional, $log);
93
94
        if ($customFields)
0 ignored issues
show
Bug Best Practice introduced by
The expression $customFields of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
95
        {
96
            $data['custom'] = $this->fieldProvider->resolveValues($customFields, $log);
97
        }
98
99
        return $data;
100
    }
101
    
102
    /**
103
     * Format message field
104
     * 
105
     * @param string $message
106
     * @return string
107
     */
108
    protected function formatMessage($message)
109
    {
110
        if ( ! is_bool($message))
111
        {
112
            return (string)$message;
113
        }
114
        
115
        // cast boolean values to "1" or "0" strings
116
        if ($message)
117
        {
118
            return '1';
119
        }
120
        
121
        return '0';
122
    }
123
124
    /**
125
     * Set handler
126
     *
127
     * @param BaseHandler $handler
128
     */
129
    public function setHandler(BaseHandler $handler)
130
    {
131
        $this->handler = $handler;
132
    }
133
134
    /**
135
     * Set field provider
136
     *
137
     * @param FieldProvider $fieldProvider
138
     */
139
    public function setFieldProvider(FieldProvider $fieldProvider)
140
    {
141
        $this->fieldProvider = $fieldProvider;
142
    }
143
144
    /**
145
     * Send data to storage
146
     *
147
     * @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...
148
     * @return mixed
149
     */
150
    protected function send(array $event)
151
    {
152
        try
153
        {
154
            return $this->handler->handle($event);
155
        }
156
        catch (\Exception $ex)
157
        {
158
            return false;
159
        }
160
    }
161
}
162