Completed
Push — develop ( d2639f...6415b9 )
by
unknown
16s
created

AnalyticModel::parseObjectInstances()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 0
cts 12
cp 0
rs 8.8571
cc 5
eloc 7
nc 5
nop 1
crap 30
1
<?php
2
/**
3
 * Schema Class for output data.
4
 */
5
namespace Graviton\AnalyticsBundle\Model;
6
7
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
8
9
/**
10
 * Schema
11
 *
12
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
13
 * @license  https://opensource.org/licenses/MIT MIT License
14
 * @link     http://swisscom.ch
15
 */
16
class AnalyticModel
17
{
18
    protected $collection;
19
    protected $route;
20
    protected $aggregate = [];
21
    protected $schema;
22
    protected $type;
23
    protected $cacheTime;
24
    protected $params = [];
25
26
    /**
27
     * String collection
28
     * @return mixed
29
     */
30
    public function getCollection()
31
    {
32
        return $this->collection;
33
    }
34
35
    /**
36
     * Set value of collection
37
     * @param mixed $collection string name
38
     * @return void
39
     */
40
    public function setCollection($collection)
41
    {
42
        $this->collection = $collection;
43
    }
44
45
    /**
46
     * Route path
47
     * @return mixed
48
     */
49
    public function getRoute()
50
    {
51
        return $this->route;
52
    }
53
54
    /**
55
     * Set path
56
     * @param mixed $route string route
57
     * @return void
58
     */
59
    public function setRoute($route)
60
    {
61
        $this->route = $route;
62
    }
63
64
    /**
65
     * Set mongodb query
66
     * @param mixed $aggregate object type for query data
67
     * @return void
68
     */
69
    public function setAggregate($aggregate)
70
    {
71
        $this->aggregate = $aggregate;
72
    }
73
74
    /**
75
     * Schema for response
76
     * @return mixed
77
     */
78
    public function getSchema()
79
    {
80
        $schema = $this->schema;
81
        $schema->{'x-params'} = $this->getParams();
82
        return $schema;
83
    }
84
85
    /**
86
     * Schema data
87
     * @param mixed $schema object schema
88
     * @return void
89
     */
90
    public function setSchema($schema)
91
    {
92
        $this->schema = $schema;
93
    }
94
95
    /**
96
     * Type of response data
97
     * @return mixed
98
     */
99
    public function getType()
100
    {
101
        return $this->type;
102
    }
103
104
    /**
105
     * Type (array or object)
106
     * @param mixed $type string view
107
     * @return void
108
     */
109
    public function setType($type)
110
    {
111
        $this->type = $type;
112
    }
113
114
    /**
115
     * Time for this route data to be cached
116
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use integer.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
117
     */
118
    public function getCacheTime()
119
    {
120
        return $this->cacheTime;
121
    }
122
123
    /**
124
     * Time for this route data to be cached
125
     * @param integer $cacheTime seconds to be cached
126
     * @return void
127
     */
128
    public function setCacheTime($cacheTime)
129
    {
130
        $this->cacheTime = (int) $cacheTime;
131
    }
132
133
    /**
134
     * Build a output Db Model aggregation pipeline array.
135
     *
136
     * @param array $params params
137
     *
138
     * @return array the pipeline
139
     */
140
    public function getAggregate($params = [])
141
    {
142
        $aggregate = $this->getParameterizedAggregate($params);
143
144
        if (empty($aggregate)) {
145
            throw new InvalidArgumentException('Wrong configuration for Aggregation pipeline - it is empty!');
146
        }
147
148
        return $aggregate;
149
    }
150
151
    /**
152
     * get Params
153
     *
154
     * @return mixed Params
155
     */
156
    public function getParams()
157
    {
158
        return $this->params;
159
    }
160
161
    /**
162
     * set Params
163
     *
164
     * @param mixed $params params
165
     *
166
     * @return void
167
     */
168
    public function setParams($params)
169
    {
170
        $this->params = $params;
171
    }
172
173
    /**
174
     * returns the pipeline with param values replaced
175
     *
176
     * @param array $params the params
177
     *
178
     * @return array the pipeline with values filled in
179
     */
180
    private function getParameterizedAggregate(array $params)
181
    {
182
        $encoded = json_encode($this->aggregate);
183
184
        // are there any params?
185
        if (is_array($params) && !empty($params)) {
186
            foreach ($params as $name => $value) {
187
                if (!is_array($value)) {
188
                    // replace single standalone values in json
189
                    if (is_int($value) || is_bool($value)) {
190
                        $encoded = preg_replace('/"\$\{'.$name.'\}"/', $value, $encoded);
191
                    }
192
                    // the balance
193
                    $encoded = preg_replace('/\$\{'.$name.'\}/', $value, $encoded);
194
                } else {
195
                    $encoded = preg_replace('/"\$\{'.$name.'\}"/', json_encode($value), $encoded);
196
                }
197
            }
198
        }
199
200
        return $this->parseObjectInstances(json_decode($encoded, true));
201
    }
202
203
    /**
204
     * parse object structures that need to be injected in order to execute the query (like MongoDates or Ids)
205
     *
206
     * @param array $struct the pipeline
207
     *
208
     * @return array changed pipeline
209
     */
210
    private function parseObjectInstances(array $struct)
211
    {
212
        foreach ($struct as $key => $prop) {
213
            if (is_array($prop)) {
214
                $struct[$key] = $this->parseObjectInstances($prop);
215
            }
216
            if (is_string($prop) && $prop == '#newDate#') {
217
                $struct[$key] = new \MongoDate();
218
            }
219
        }
220
        return $struct;
221
    }
222
}
223