Completed
Push — master ( 9f03c4...670bd1 )
by Alessandro
14:40 queued 12s
created

LogEvent::prepareUnserializableData()   B

Complexity

Conditions 6
Paths 17

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 24
ccs 11
cts 11
cp 1
rs 8.5125
cc 6
eloc 11
nc 17
nop 1
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Facile\MongoDbBundle\Services\Loggers\Model;
6
7
/**
8
 * Class LogEvent.
9
 */
10
class LogEvent
11
{
12
    /** @var float */
13
    private $start;
14
15
    /** @var string */
16
    private $method;
17
18
    /** @var string */
19
    private $collection;
20
21
    /** @var array */
22
    private $data;
23
24
    /** @var int */
25
    private $executionTime;
26
27
    /**
28
     * LogEvent constructor.
29
     */
30 14
    public function __construct()
31
    {
32 14
        $this->start = 0.0;
33 14
        $this->collection = 'undefined';
34 14
        $this->method = 'undefined';
35 14
        $this->data = [];
36 14
        $this->executionTime = 0;
37 14
    }
38
39
    /**
40
     * @return float
41
     */
42 3
    public function getStart(): float
43
    {
44 3
        return $this->start;
45
    }
46
47
    /**
48
     * @param float $start
49
     */
50 1
    public function setStart(float $start)
51
    {
52 1
        $this->start = $start;
53 1
    }
54
55
    /**
56
     * @return string
57
     */
58 2
    public function getCollection(): string
59
    {
60 2
        return $this->collection;
61
    }
62
63
    /**
64
     * @param string $collection
65
     */
66 11
    public function setCollection(string $collection)
67
    {
68 11
        $this->collection = $collection;
69 11
    }
70
71
    /**
72
     * @return string
73
     */
74 2
    public function getMethod(): string
75
    {
76 2
        return $this->method;
77
    }
78
79
    /**
80
     * @param string $method
81
     */
82 10
    public function setMethod(string $method)
83
    {
84 10
        $this->method = $method;
85 10
    }
86
87
    /**
88
     * @return array
89
     */
90 2
    public function getData(): array
91
    {
92 2
        return $this->data;
93
    }
94
95
    /**
96
     * @return string
97
     */
98 2
    public function getDataJson(): string
99
    {
100 2
        return json_encode($this->data);
101
    }
102
103
    /**
104
     * @param array $data
105
     */
106 10
    public function setData(array $data)
107
    {
108 10
        $this->data = $this->prepareUnserializableData($data);
109 10
    }
110
111
    /**
112
     * @return float
113
     */
114 4
    public function getExecutionTime(): float
115
    {
116 4
        return $this->executionTime;
117
    }
118
119
    /**
120
     * @param float $executionTime
121
     */
122 4
    public function setExecutionTime(float $executionTime)
123
    {
124 4
        $this->executionTime = $executionTime;
0 ignored issues
show
Documentation Bug introduced by
The property $executionTime was declared of type integer, but $executionTime is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
125 4
    }
126
127
    /**
128
     * @param array $data
129
     *
130
     * @return array
131
     */
132 10
    private function prepareUnserializableData(array $data): array
133
    {
134 10
        foreach ($data as $key => $item) {
135
136 4
            if (method_exists($item, 'getArrayCopy')) {
137 1
                $data[$key] = $this->prepareUnserializableData($item->getArrayCopy());
138
            }
139
140 4
            if (method_exists($item, '__toString')) {
141 1
                $data[$key] = $item->__toString();
142
            }
143
144 4
            if (is_array($item)) {
145 2
                $data[$key] = $this->prepareUnserializableData($item);
146
            }
147
148 4
            if ($item instanceof \Serializable) {
149 4
                continue;
150
            }
151
152
        }
153
154 10
        return $data;
155
    }
156
}
157
158