Completed
Push — master ( 954a1b...9c6327 )
by Pierre
34:02
created

Metro::stations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 4
b 0
f 0
nc 1
nop 0
dl 0
loc 15
ccs 10
cts 10
cp 1
crap 1
rs 9.9
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
            $this->modelLines
70 1
        );
71 1
        $this->response->setCode(Response::HTTP_OK)->setContent([
72 1
            Response::_ERROR => false,
73 1
            Response::_ERROR_MSG => '',
74
            self::_DATAS => $this->getQueryResults($query)
75 1
        ]);
76 1
        unset($query);
77
        return $this;
78
    }
79
80
    /**
81
     * search stations
82
     *
83
     * @return Metro
84 1
     */
85
    final public function stations(): Metro
86 1
    {
87 1
        $query = $this->search(
88 1
            $this->getFilteredInput(),
89 1
            Stations::_NAME,
90
            Orm::OP_LIKE,
91 1
            $this->modelStations
92 1
        );
93 1
        $this->response->setCode(Response::HTTP_OK)->setContent([
94 1
            Response::_ERROR => false,
95
            Response::_ERROR_MSG => '',
96 1
            self::_DATAS => $this->getQueryResults($query)
97 1
        ]);
98
        unset($query);
99
        return $this;
100
    }
101
102
    /**
103
     * get results as array
104
     *
105
     * @param Orm $query
106 1
     * @return array
107
     */
108 1
    protected function getQueryResults(Orm $query)
109 1
    {
110 1
        return $this->dbCore
111 1
            ->fromOrm($query)
112 1
            ->run($$query->getSql(), $query->getBuilderValues())
113
            ->hydrate()
114
            ->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 1
     * @param Orm $model
124
     * @return Orm
125 1
     */
126 1
    protected function search(array $inputs, string $searchKey, string $operator, Orm &$model): Orm
127 1
    {
128 1
        $where = [];
129 1
        if (isset($inputs[$searchKey])) {
130 1
            $searchValue = $inputs[$searchKey];
131 1
            if ($operator === Orm::OP_LIKE) {
132
                $where = [
133
                    $searchKey . $operator => Orm::SQL_WILD . $searchValue . Orm::SQL_WILD
134 1
                ];
135
            } else {
136
                $where = [$searchKey => $searchValue];
137
            }
138
        }
139
        $model->find([Orm::SQL_ALL], $where);
140
        if ([] === $where && !isset($inputs[self::_LIMIT])) {
141
            $inputs[self::_LIMIT] = 10;
142 1
        }
143
        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 1
            }
149
        }
150
        return $model;
151
    }
152
153
    /**
154
     * return filtered request params
155
     *
156
     * @return array
157
     */
158
    protected function getFilteredInput(): array
159
    {
160
        return (new Filter($this->getParams(), [
161
            Lines::_HSRC => FILTER_SANITIZE_STRING,
162
            Stations::_NAME => FILTER_SANITIZE_STRING,
163
            self::_LIMIT => FILTER_SANITIZE_NUMBER_INT,
164
            self::_PAGE => FILTER_SANITIZE_NUMBER_INT,
165
        ]))->process()->toArray();
166
    }
167
}
168