Completed
Push — master ( 9c6327...08d2de )
by Pierre
34:09 queued 31:15
created

Metro::search()   B

Complexity

Conditions 8
Paths 24

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 8.0155

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 17
c 1
b 0
f 0
nc 24
nop 4
dl 0
loc 25
ccs 15
cts 16
cp 0.9375
crap 8.0155
rs 8.4444
1
<?php
2
3
namespace App\Controllers\Api\V1;
4
5
use Nymfonya\Component\Container;
6
use Nymfonya\Component\Http\Response;
7
use App\Interfaces\Controllers\IApi;
8
use App\Reuse\Controllers\AbstractApi;
9
use App\Model\Repository\Metro\Lines;
10
use App\Model\Repository\Metro\Stations;
11
use App\Component\Db\Core;
12
use App\Component\Model\Orm\Orm;
13
use App\Component\Filter;
14
use NilPortugues\Sql\QueryBuilder\Manipulation\Select;
15
16
final class Metro extends AbstractApi implements IApi
17
{
18
    const _NAME = 'name';
19
    const _LIMIT = 'limit';
20
    const _PAGE = 'page';
21
    const _DATAS = 'datas';
22
23
    /**
24
     * Lines model
25
     *
26
     * @var Lines
27
     */
28
    protected $modelLines;
29
30
31
    /**
32
     * Stations model
33
     *
34
     * @var Stations
35
     */
36
    protected $modelStations;
37
38
    /**
39
     * db core
40
     *
41
     * @var Core
42
     */
43
    protected $dbCore;
44
45
    /**
46
     * instanciate
47
     *
48
     * @param Container $container
49
     */
50 6
    public function __construct(Container $container)
51
    {
52 6
        parent::__construct($container);
53 6
        $this->modelLines = new Lines($container);
54 6
        $this->modelStations = new Stations($container);
55 6
        $this->dbCore = new Core($container);
56
    }
57
58
    /**
59
     * search lines
60
     *
61
     * @return Metro
62
     */
63 1
    final public function lines(): Metro
64
    {
65 1
        $query = $this->search(
66 1
            $this->getFilteredInput(),
67 1
            Lines::_HSRC,
68 1
            '',
69 1
            $this->modelLines
70
        );
71 1
        $this->response->setCode(Response::HTTP_OK)->setContent([
72 1
            Response::_ERROR => false,
73 1
            Response::_ERROR_MSG => '',
74 1
            self::_DATAS => $this->getQueryResults($query)
75
        ]);
76 1
        unset($query);
77 1
        return $this;
78
    }
79
80
    /**
81
     * search stations
82
     *
83
     * @return Metro
84
     */
85 1
    final public function stations(): Metro
86
    {
87 1
        $query = $this->search(
88 1
            $this->getFilteredInput(),
89 1
            Stations::_NAME,
90 1
            Orm::OP_LIKE,
91 1
            $this->modelStations
92
        );
93 1
        $this->response->setCode(Response::HTTP_OK)->setContent([
94 1
            Response::_ERROR => false,
95 1
            Response::_ERROR_MSG => '',
96 1
            self::_DATAS => $this->getQueryResults($query)
97
        ]);
98 1
        unset($query);
99 1
        return $this;
100
    }
101
102
    /**
103
     * get results as array
104
     *
105
     * @param Orm $query
106
     * @return array
107
     */
108 1
    protected function getQueryResults(Orm $query)
109
    {
110 1
        return $this->dbCore
111 1
            ->fromOrm($query)
112 1
            ->run($query->getSql(), $query->getBuilderValues())
113 1
            ->hydrate()
114 1
            ->getRowset();
115
    }
116
117
    /**
118
     * search items by search key and value limiting results amount
119
     *
120
     * @param array $inputs
121
     * @param string $searchKey
122
     * @param string $operator
123
     * @param Orm $model
124
     * @return Orm
125
     */
126 1
    protected function search(array $inputs, string $searchKey, string $operator, Orm &$model): Orm
127
    {
128 1
        $where = [];
129 1
        if (isset($inputs[$searchKey])) {
130 1
            $searchValue = $inputs[$searchKey];
131 1
            if ($operator === Orm::OP_LIKE) {
132
                $where = [
133 1
                    $searchKey . $operator => Orm::SQL_WILD . $searchValue . Orm::SQL_WILD
134
                ];
135
            } else {
136 1
                $where = [$searchKey => $searchValue];
137
            }
138
        }
139 1
        $model->find([Orm::SQL_ALL], $where);
140 1
        if ([] === $where && !isset($inputs[self::_LIMIT])) {
141
            $inputs[self::_LIMIT] = 10;
142
        }
143 1
        if (isset($inputs[self::_LIMIT])) {
144 1
            if ($model->getQuery() instanceof Select) {
145 1
                $page = isset($inputs[self::_PAGE]) ? (int) $inputs[self::_PAGE] : 0;
146 1
                $offset = $page * (int) $inputs[self::_LIMIT];
147 1
                $model->getQuery()->limit($offset, (int) $inputs[self::_LIMIT]);
148
            }
149
        }
150 1
        return $model;
151
    }
152
153
    /**
154
     * return filtered request params
155
     *
156
     * @return array
157
     */
158 1
    protected function getFilteredInput(): array
159
    {
160 1
        return (new Filter($this->getParams(), [
161 1
            Lines::_HSRC => FILTER_SANITIZE_STRING,
162 1
            Stations::_NAME => FILTER_SANITIZE_STRING,
163 1
            self::_LIMIT => FILTER_SANITIZE_NUMBER_INT,
164 1
            self::_PAGE => FILTER_SANITIZE_NUMBER_INT,
165 1
        ]))->process()->toArray();
166
    }
167
}
168