ControllerQuery::iterateFieldList()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 11
rs 9.2
cc 4
eloc 6
nc 5
nop 1
1
<?php
2
3
namespace ComicVine\Api\Controllers;
4
5
use ComicVine\ComicVine;
6
use ComicVine\Api\Validation;
7
use ComicVine\Api\Filters\FilterHandlers;
8
use ComicVine\Api\Filters\FilterCheck;
9
use ComicVine\Api\Response\Type\ResponseFormat;
10
11
/**
12
 * Class responsible for creating a proper
13
 * query to ComicVine.
14
 *
15
 * Class ControllerApi
16
 *
17
 * @package grzgajda/comicvine-api
18
 * @author  Grzegorz Gajda <[email protected]>
19
 */
20
class ControllerQuery
21
{
22
    use FilterHandlers;
23
    use FilterCheck;
24
25
    /**
26
     * List of enabled filters generated by ControllerRequest.
27
     *
28
     * @var array
29
     */
30
    protected $enabledFilters = [];
31
32
    /**
33
     * List of setted filters by user.
34
     *
35
     * @var array
36
     */
37
    protected $settedFilters
38
        = [
39
            'field_list' => '',
40
            'limit'      => 100,
41
            'offset'     => 0,
42
            'filter'     => '',
43
            'sort'       => '',
44
            'api_key'    => '',
45
        ];
46
47
    /**
48
     * Part of url to proper resource.
49
     *
50
     * @var string
51
     */
52
    protected $url = "";
53
54
    /**
55
     * Connection instance.
56
     *
57
     * @var \ComicVine\Api\Connection\Connection
58
     */
59
    private $connection;
60
61
    /**
62
     * Validation object.
63
     *
64
     * @var Validation
65
     */
66
    private $validation;
67
68
    /**
69
     * ControllerQuery constructor.
70
     *
71
     * @param array  $filters List of enabled filters
72
     * @param string $url     Part of URL
73
     */
74
    public function __construct($filters, $url)
75
    {
76
        $this->enabledFilters           = $filters;
77
        $this->url                      = $url;
78
        $this->validation               = new Validation($this->enabledFilters);
79
        $this->settedFilters['api_key'] = ComicVine::getKey();
80
        $this->connection               = ComicVine::getConnection();
81
    }
82
83
    /**
84
     * Set fields to get in response.
85
     *
86
     * @param array $arr
87
     *
88
     * @return $this
89
     */
90
    public function setFieldList($arr = [])
91
    {
92
        if ($this->validation->validation('field_list', $arr) === true) {
93
            $this->iterateFieldList($arr);
94
        }
95
96
        return $this;
97
    }
98
99
    /**
100
     * Set filters for request.
101
     *
102
     * @param array $arr Filters defined by user.
103
     *
104
     * @return $this
105
     */
106 View Code Duplication
    public function setFilters($arr = [])
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...
107
    {
108
        $this->isEnabledFilter('filter', $this->enabledFilters, $this);
109
110
        if ($this->validation->validation('filter', $arr) === true) {
111
            $this->iterateFilterOrSort($arr, 'filter');
112
        }
113
114
        return $this;
115
    }
116
117
    /**
118
     * Set sort for request.
119
     *
120
     * @param array $arr Sort defined by user
121
     *
122
     * @return $this
123
     */
124 View Code Duplication
    public function setSorts($arr = [])
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...
125
    {
126
        $this->isEnabledFilter('sort', $this->enabledFilters, $this);
127
128
        if ($this->validation->validation('sort', $arr) === true) {
129
            $this->iterateFilterOrSort($arr, 'sort');
130
        }
131
132
        return $this;
133
    }
134
135
    /**
136
     * Set limit for request.
137
     *
138
     * @param int $limit Limit for elements. Limit can only be between 0 and 100.
139
     *
140
     * @return $this
141
     */
142 View Code Duplication
    public function setLimit($limit = 100)
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...
143
    {
144
        $this->isEnabledFilter('limit', $this->enabledFilters, $this);
145
146
        if ($this->validation->validation('limit', $limit) === true) {
147
            $this->settedFilters['limit'] = $limit;
148
        }
149
150
        return $this;
151
    }
152
153
    /**
154
     * Set offset for request.
155
     *
156
     * @param int $offset Offset for elements.
157
     *
158
     * @return $this
159
     */
160 View Code Duplication
    public function setOffset($offset = 0)
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...
161
    {
162
        $this->isEnabledFilter('offset', $this->enabledFilters, $this);
163
164
        if ($this->validation->validation('offset', $offset) === true) {
165
            $this->settedFilters['offset'] = $offset;
166
        }
167
168
        return $this;
169
    }
170
171
    /**
172
     * Set format for response.
173
     *
174
     * @param \ComicVine\Api\Response\Type\ResponseFormat $format
175
     *
176
     * @return $this
177
     */
178
    public function setFormat(ResponseFormat $format)
179
    {
180
        $this->settedFilters['format'] = array_values($format->get())[0];
181
182
        return $this;
183
    }
184
185
    /**
186
     * Get reponse from Connection instance.
187
     *
188
     * @return mixed
189
     */
190
    public function getResponse()
191
    {
192
        return ComicVine::getConnection()->makeConnection()
193
            ->setConnection($this->build())
194
            ->getResult();
195
    }
196
197
    /**
198
     * Build full url from protected attributes.
199
     *
200
     * @return string
201
     */
202
    public function build()
203
    {
204
        $filters = $this->flushDisabledFilters($this->settedFilters, $this->enabledFilters);
205
206
        $url   = "http://www.comicvine.com/api".$this->url.'/?';
207
        $query = urldecode(http_build_query($filters, null, '&'));
208
209
        return $url.$query;
210
211
    }
212
213
    /**
214
     * Iterate array to add elements to class variable.
215
     *
216
     * @param array  $array Array of elements to iterate
217
     * @param string $type  Type of elements: sort|filter
218
     *
219
     * @return false
220
     */
221
    protected function iterateFilterOrSort($array, $type)
222
    {
223
        foreach ($array as $key => $value) {
224
            $allKeys = array_keys($array);
225
            $last    = (end($allKeys) === $key) ? true : false;
226
            $this->addFilterOrSort($type, $key, $value, $last);
227
        }
228
    }
229
230
    /**
231
     * Add new filter or sort to settedFilters array.
232
     *
233
     * @param string     $type  Type: sort|filter
234
     * @param string     $param Param for request
235
     * @param string     $value Value for request
236
     * @param bool|false $last  Is element last?
237
     */
238
    protected function addFilterOrSort($type, $param, $value, $last = false)
239
    {
240
        $query = urlencode($param).':'.urlencode($value);
241
242
        if ($last === false) {
243
            $query .= ',';
244
        }
245
246
        $this->settedFilters[$type] .= $query;
247
    }
248
249
    /**
250
     * Iterate array to set allowed fields in response.
251
     *
252
     * @param array $array List of allowed fields in request.
253
     */
254
    protected function iterateFieldList($array)
255
    {
256
        for ($i = 0, $c = count($array); $i < $c; $i++) {
257
            $last = ($i === count($array) - 1) ? true : false;
258
            $this->settedFilters['field_list'] .= urlencode($array[$i]);
259
260
            if ($last !== true) {
261
                $this->settedFilters['field_list'] .= ',';
262
            }
263
        }
264
    }
265
266
}