Completed
Push — master ( 5c5133...4bc4b0 )
by Marcin
02:40
created

QueryBuilder::get()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 32
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 25
cts 25
cp 1
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 25
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * MOJEPANSTWO-API
4
 *
5
 * Copyright © 2017 pudelek.org.pl
6
 *
7
 * @license MIT License (MIT)
8
 *
9
 * For the full copyright and license information, please view source file
10
 * that is bundled with this package in the file LICENSE
11
 *
12
 * @author  Marcin Pudełek <[email protected]>
13
 */
14
declare (strict_types=1);
15
16
namespace mrcnpdlk\MojePanstwo;
17
18
19
use mrcnpdlk\MojePanstwo\Model\ModelAbstract;
20
use mrcnpdlk\MojePanstwo\Model\SearchResponse;
21
use mrcnpdlk\MojePanstwo\Model\SearchResponseItem;
22
use mrcnpdlk\MojePanstwo\Model\SearchResponseLinks;
23
24
class QueryBuilder
25
{
26
    /**
27
     * @var array
28
     */
29
    private $query = [];
30
    /**
31
     * @var string
32
     */
33
    private $sContext;
34
    /**
35
     * @var string
36
     */
37
    private $sPrefixedContext;
38
    /**
39
     * @var string|null
40
     */
41
    private $sReturnedClass;
42
43
    /**
44
     * QueryBuilder constructor.
45
     *
46
     * @param string|null $returnedClass
47
     *
48
     * @throws \mrcnpdlk\MojePanstwo\Exception
49
     */
50 7
    private function __construct(string $returnedClass)
51
    {
52 7
        if (!class_exists($returnedClass)) {
53
            throw new Exception(sprintf('Cannot create QueryBuilder instance. Class [%s] not defined', $returnedClass));
54
        }
55 7
        $reflectionA = new \ReflectionClass($returnedClass);
56 7
        if (!$reflectionA->isSubclassOf(ModelAbstract::class)) {
57
            throw new Exception(sprintf('Cannot create QueryBuilder instance. Class [%s] not extend ModelAbstract', $returnedClass));
58
        }
59
60 7
        $this->query['conditions'] = [];
61 7
        $this->query['order']      = null;
62 7
        $this->query['page']       = null;
63 7
        $this->query['limit']      = null;
64 7
        $this->sReturnedClass      = $returnedClass;
65
        /** @noinspection PhpUndefinedFieldInspection */
66 7
        $this->sContext         = $returnedClass::CONTEXT;
67 7
        $this->sPrefixedContext = '/dane/' . $this->sContext;
68 7
    }
69
70
    /**
71
     * @param string|null $returnedClass
72
     *
73
     * @return \mrcnpdlk\MojePanstwo\QueryBuilder
74
     */
75 7
    static public function create(string $returnedClass)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
76
    {
77 7
        return new QueryBuilder($returnedClass);
78
    }
79
80
    /**
81
     * @param string $layerName
82
     *
83
     * @return $this
84
     */
85
    public function addLayer(string $layerName)
86
    {
87
        $this->query['layers'][] = $layerName;
88
89
        return $this;
90
    }
91
92
    /**
93
     * Find object having ID
94
     *
95
     * @param string $id
96
     *
97
     * @return mixed
98
     */
99 2
    public function find(string $id)
100
    {
101 2
        $res = Api::getInstance()
102 2
                  ->getClient()
103 2
                  ->request($this->sPrefixedContext, $id, $this)
104
        ;
105
106 1
        return new $this->sReturnedClass($res->data ?? null, $res->layers ?? null);
107
    }
108
109
    /**
110
     * Search result
111
     *
112
     * @return SearchResponse
113
     */
114 5
    public function get()
115
    {
116
117 5
        $res    = Api::getInstance()
118 5
                     ->getClient()
119 5
                     ->request($this->sPrefixedContext, null, $this)
120
        ;
121 5
        $oLinks = new SearchResponseLinks(
122 5
            $res->Links->self ?? null,
123 5
            $res->Links->first ?? $res->Links->self ?? null,
124 5
            $res->Links->next ?? null,
125 5
            $res->Links->last ?? null,
126 5
            $res->Links->prev ?? null
127
        );
128 5
        $items  = [];
129 5
        foreach ($res->Dataobject as $i) {
130 5
            $oItem             = new SearchResponseItem();
131 5
            $oItem->id         = intval($i->id);
0 ignored issues
show
Documentation Bug introduced by
The property $id was declared of type string, but intval($i->id) is of type integer. 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...
132 5
            $oItem->dataset    = $i->dataset;
133 5
            $oItem->url        = $i->url;
134 5
            $oItem->mp_url     = $i->mp_url;
135 5
            $oItem->schema_url = $i->schema_url;
136 5
            $oItem->global_id  = intval($i->global_id);
0 ignored issues
show
Documentation Bug introduced by
The property $global_id was declared of type string, but intval($i->global_id) is of type integer. 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...
137 5
            $oItem->slug       = $i->slug;
138 5
            $oItem->score      = $i->score;
139 5
            $oItem->data       = new $this->sReturnedClass($i->data ?? null);
140 5
            $items[]           = $oItem;
141
        }
142 5
        $oResp = new SearchResponse($res->Count, $res->Took, $oLinks, $items);
143
144 5
        return $oResp;
145
    }
146
147
    /**
148
     * @return string
149
     */
150 7
    public function getQuery()
151
    {
152 7
        if (empty($this->query['order'])) {
153 7
            unset($this->query['order']);
154
        }
155 7
        if (empty($this->query['page'])) {
156 7
            unset($this->query['page']);
157
        }
158 7
        if (empty($this->query['limit'])) {
159 2
            unset($this->query['limit']);
160
        }
161
162 7
        return http_build_query($this->query);
163
164
    }
165
166
    /**
167
     * @param int $limit
168
     *
169
     * @return $this
170
     */
171 5
    public function limit(int $limit = 50)
172
    {
173 5
        $this->query['limit'] = $limit;
174
175 5
        return $this;
176
    }
177
178
    /**
179
     * @param string $property
180
     * @param string $order
181
     *
182
     * @return $this
183
     * @todo Zaimplementowac
184
     */
185
    public function orderBy(string $property, string $order = 'asc')
186
    {
187
        if (empty($this->sContext)) {
188
            $this->query['order'] = $property . ' ' . $order;
189
        } else {
190
            $this->query['order'] = $this->sContext . '.' . $property . ' ' . $order;
191
        }
192
193
194
        return $this;
195
    }
196
197
    /**
198
     * @param int $page
199
     *
200
     * @return $this
201
     */
202
    public function page(int $page = 1)
203
    {
204
        $this->query['page'] = $page < 1 ? 1 : $page;
205
206
        return $this;
207
    }
208
209
    /**
210
     * @param string $property
211
     * @param        $value
212
     *
213
     * @return $this
214
     */
215
    public function where(string $property, $value)
216
    {
217
        if (empty($this->sContext)) {
218
            $this->query['conditions'][$property] = $value;
219
        } else {
220
            $this->query['conditions'][sprintf("%s.%s", $this->sContext, $property)] = $value;
221
        }
222
223
        return $this;
224
    }
225
226
}
227