Completed
Push — master ( 832032...ed9960 )
by Pierre
03:01
created

Metro::search()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 10
ccs 8
cts 8
cp 1
crap 3
rs 10
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
15
final class Metro extends AbstractApi implements IApi
16
{
17
    const _NAME = 'name';
18
    const _LIMIT = 'limit';
19
    const _PAGE = 'page';
20
    const _DATAS = 'datas';
21
22
    /**
23
     * Lines model
24
     *
25
     * @var Lines
26
     */
27
    protected $modelLines;
28
29
30
    /**
31
     * Stations model
32
     *
33
     * @var Stations
34
     */
35
    protected $modelStations;
36
37
    /**
38
     * db core
39
     *
40
     * @var Core
41
     */
42
    protected $dbCore;
43
44
    /**
45
     * instanciate
46
     *
47
     * @param Container $container
48
     */
49 6
    public function __construct(Container $container)
50
    {
51 6
        parent::__construct($container);
52 6
        $this->modelLines = new Lines($container);
53 6
        $this->modelStations = new Stations($container);
54 6
        $this->dbCore = new Core($container);
55
    }
56
57
    /**
58
     * search lines
59
     *
60
     * @return Metro
61
     */
62 1
    final public function lines(): Metro
63
    {
64 1
        $query = $this->search(
65 1
            $this->getFilteredInput(),
66 1
            $this->modelLines,
67 1
            Lines::_SRC
68
        );
69 1
        $this->response->setCode(Response::HTTP_OK)->setContent([
70 1
            Response::_ERROR => false,
71 1
            Response::_ERROR_MSG => '',
72 1
            self::_DATAS => $this->getQueryResults($query)
73
        ]);
74 1
        unset($query);
75 1
        return $this;
76
    }
77
78
    /**
79
     * search stations
80
     *
81
     * @return Metro
82
     */
83 1
    final public function stations(): Metro
84
    {
85 1
        $query = $this->search(
86 1
            $this->getFilteredInput(),
87 1
            $this->modelStations,
88 1
            Stations::_NAME
89
        );
90 1
        $this->response->setCode(Response::HTTP_OK)->setContent([
91 1
            Response::_ERROR => false,
92 1
            Response::_ERROR_MSG => '',
93 1
            self::_DATAS => $this->getQueryResults($query)
94
        ]);
95 1
        unset($query);
96 1
        return $this;
97
    }
98
99
    /**
100
     * get results as array
101
     *
102
     * @param Orm $query
103
     * @return array
104
     */
105 1
    protected function getQueryResults(Orm $query)
106
    {
107 1
        $sql = $query->getSql();
108 1
        return $this->dbCore->fromOrm($query)->run(
109 1
            $sql,
110 1
            $query->getBuilderValues()
111 1
        )->hydrate()->getRowset();
112
    }
113
114
    /**
115
     * search items by search key and value limiting results amount
116
     *
117
     * @param array $inputs
118
     * @param Orm $model
119
     * @param string $searchKey
120
     * @return Orm
121
     */
122 1
    protected function search(array $inputs, Orm &$model, string $searchKey): Orm
123
    {
124 1
        $searchValue = (isset($inputs[$searchKey]))
125 1
            ? Orm::SQL_WILD . $inputs[$searchKey] . Orm::SQL_WILD
126 1
            : Orm::SQL_WILD;
127 1
        $model->find([Orm::SQL_ALL], [$searchKey . Orm::OP_LIKE => $searchValue]);
128 1
        if (isset($inputs[self::_LIMIT])) {
129 1
            $model->getQuery()->limit(0, (int) $inputs[self::_LIMIT]);
0 ignored issues
show
Bug introduced by
The method limit() does not exist on NilPortugues\Sql\QueryBuilder\Manipulation\Insert. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

129
            $model->getQuery()->/** @scrutinizer ignore-call */ limit(0, (int) $inputs[self::_LIMIT]);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Unused Code introduced by
The call to NilPortugues\Sql\QueryBu...ulation\Delete::limit() has too many arguments starting with (int)$inputs[self::_LIMIT]. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

129
            $model->getQuery()->/** @scrutinizer ignore-call */ limit(0, (int) $inputs[self::_LIMIT]);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Unused Code introduced by
The call to NilPortugues\Sql\QueryBu...ulation\Update::limit() has too many arguments starting with (int)$inputs[self::_LIMIT]. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

129
            $model->getQuery()->/** @scrutinizer ignore-call */ limit(0, (int) $inputs[self::_LIMIT]);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
130
        }
131 1
        return $model;
132
    }
133
134
    /**
135
     * return filtered request params
136
     *
137
     * @return array
138
     */
139 1
    protected function getFilteredInput(): array
140
    {
141 1
        return (new Filter($this->getParams(), [
142 1
            self::_NAME => FILTER_SANITIZE_STRING,
143 1
            self::_LIMIT => FILTER_SANITIZE_NUMBER_INT,
144 1
            self::_PAGE => FILTER_SANITIZE_NUMBER_INT,
145 1
        ]))->process()->toArray();
146
    }
147
}
148