Completed
Push — master ( 12f217...414d39 )
by Alessandro
06:27
created

Collection::preserialize()   B

Complexity

Conditions 6
Paths 17

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6.2163

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 24
ccs 9
cts 11
cp 0.8182
rs 8.5125
cc 6
eloc 11
nc 17
nop 1
crap 6.2163
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Facile\MongoDbBundle\Capsule;
6
7
use Facile\MongoDbBundle\Services\Loggers\DataCollectorLoggerInterface;
8
use Facile\MongoDbBundle\Services\Loggers\Model\LogEvent;
9
use MongoDB\Collection as MongoCollection;
10
use MongoDB\Driver\Manager;
11
12
/**
13
 * Class Collection.
14
 */
15
final class Collection extends MongoCollection
16
{
17
    /**
18
     * @var DataCollectorLoggerInterface
19
     */
20
    private $logger;
21
22
    /**
23
     * Collection constructor.
24
     *
25
     * @param Manager                      $manager
26
     * @param string                       $databaseName
27
     * @param string                       $collectionName
28
     * @param array                        $options
29
     * @param DataCollectorLoggerInterface $logger
30
     */
31 11
    public function __construct(Manager $manager, $databaseName, $collectionName, array $options = [], DataCollectorLoggerInterface $logger)
32
    {
33 11
        parent::__construct($manager, $databaseName, $collectionName, $options);
34 11
        $this->logger = $logger;
35 11
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 1
    public function find($filter = [], array $options = [])
41
    {
42 1
        $event = $this->startQueryLogging($filter, __FUNCTION__);
43 1
        $result = parent::find($filter, $options);
44 1
        $this->logger->logQuery($event);
45
46 1
        return $result;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 1 View Code Duplication
    public function findOne($filter = [], array $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54 1
        $event = $this->startQueryLogging($filter, __FUNCTION__);
55 1
        $result = parent::findOne($filter, $options);
56 1
        $this->logger->logQuery($event);
57
58 1
        return $result;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 1 View Code Duplication
    public function findOneAndUpdate($filter, $update, array $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
    {
66
        $data = [
67 1
            "filter" => $filter,
68 1
            "update" => $update,
69
        ];
70 1
        $event = $this->startQueryLogging($data, __FUNCTION__);
71 1
        $result = parent::findOneAndUpdate($filter, $update, $options);
72 1
        $this->logger->logQuery($event);
73
74 1
        return $result;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 1 View Code Duplication
    public function findOneAndDelete($filter, array $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
    {
82 1
        $event = $this->startQueryLogging($filter, __FUNCTION__);
83 1
        $result = parent::findOneAndDelete($filter, $options);
84 1
        $this->logger->logQuery($event);
85
86 1
        return $result;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 1 View Code Duplication
    public function deleteMany($filter, array $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
    {
94 1
        $event = $this->startQueryLogging($filter, __FUNCTION__);
95 1
        $result = parent::deleteMany($filter, $options);
96 1
        $this->logger->logQuery($event);
97
98 1
        return $result;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 1 View Code Duplication
    public function deleteOne($filter, array $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
    {
106 1
        $event = $this->startQueryLogging($filter, __FUNCTION__);
107 1
        $result = parent::deleteOne($filter, $options);
108 1
        $this->logger->logQuery($event);
109
110 1
        return $result;
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116 1 View Code Duplication
    public function replaceOne($filter, $replacement, array $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
    {
118
        $data = [
119 1
            "filter" => $filter,
120 1
            "replacement" => $replacement,
121
        ];
122 1
        $event = $this->startQueryLogging($data, __FUNCTION__);
123 1
        $result = parent::replaceOne($filter, $replacement, $options);
124 1
        $this->logger->logQuery($event);
125
126 1
        return $result;
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132 1 View Code Duplication
    public function insertOne($document, array $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133
    {
134 1
        $event = $this->startQueryLogging($document, __FUNCTION__);
135 1
        $result = parent::insertOne($document, $options);
136 1
        $this->logger->logQuery($event);
137
138 1
        return $result;
139
    }
140
141
    /**
142
     * @param array  $data
143
     * @param string $method
144
     *
145
     * @return LogEvent
146
     */
147 8
    private function startQueryLogging(array $data, string $method): LogEvent
148
    {
149 8
        $debugInfo = $this->__debugInfo();
150
151 8
        $event = new LogEvent();
152 8
        $event->setData($this->preserialize($data));
153 8
        $event->setMethod($method);
154 8
        $event->setCollection($debugInfo['collectionName']);
155
156 8
        $this->logger->startLogging($event);
157
158 8
        return $event;
159
    }
160
161
    /**
162
     * @param array $data
163
     *
164
     * @return array
165
     */
166 8
    private function preserialize(array $data): array
167
    {
168 8
        foreach ($data as $key => $item) {
169
170 2
            if (method_exists($item, 'getArrayCopy')) {
171
                $data[$key] = $this->preserialize($item->getArrayCopy());
172
            }
173
174 2
            if (method_exists($item, '__toString')) {
175
                $data[$key] = $item->__toString();
176
            }
177
178 2
            if (is_array($item)) {
179 2
                $data[$key] = $this->preserialize($item);
180
            }
181
182 2
            if ($item instanceof \Serializable) {
183 2
                continue;
184
            }
185
186
        }
187
188 8
        return $data;
189
    }
190
}
191
192