Completed
Push — master ( 681b7e...60706c )
by Pierre
03:16
created

Metro::getQueryResults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 8
ccs 7
cts 7
cp 1
crap 1
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 4
    public function __construct(Container $container)
50
    {
51 4
        parent::__construct($container);
52 4
        $this->modelLines = new Lines($container);
53 4
        $this->modelStations = new Stations($container);
54 4
        $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($this->modelLines);
65 1
        $this->response->setCode(Response::HTTP_OK)->setContent([
66 1
            Response::_ERROR => false,
67 1
            Response::_ERROR_MSG => '',
68 1
            self::_DATAS => $this->getQueryResults($query)
69
        ]);
70 1
        unset($query);
71 1
        return $this;
72
    }
73
74
    /**
75
     * search stations
76
     *
77
     * @return Metro
78
     */
79 1
    final public function stations(): Metro
80
    {
81 1
        $query = $this->search($this->modelStations);
82 1
        $this->response->setCode(Response::HTTP_OK)->setContent([
83 1
            Response::_ERROR => false,
84 1
            Response::_ERROR_MSG => '',
85 1
            self::_DATAS => $this->getQueryResults($query)
86
        ]);
87 1
        unset($query);
88 1
        return $this;
89
    }
90
91
    /**
92
     * get results as array
93
     *
94
     * @param Orm $query
95
     * @return array
96
     */
97 1
    protected function getQueryResults(Orm $query)
98
    {
99 1
        $sql = $query->getSql();
100 1
        return $this->dbCore->fromOrm($query)->run(
101 1
            $sql,
102 1
            $query->getBuilderValues()
103 1
        )->hydrate()
104 1
            ->getRowset();
105
    }
106
107
    /**
108
     * search items from name limiting results amount
109
     *
110
     * @param Orm $model
111
     * @return Orm
112
     */
113
    protected function search(Orm &$model): Orm
114
    {
115
        $input = $this->getFilteredInput();
116
        $name = (isset($input[self::_NAME]))
117
            ? Orm::SQL_WILD . $input[self::_NAME] . Orm::SQL_WILD
118
            : Orm::SQL_WILD;
119
        $field = ($model instanceof Lines) ? Lines::_SRC : Stations::_NAME;
120
        $model->find([Orm::SQL_ALL], [$field . Orm::OP_LIKE => $name]);
121
        if (isset($input[self::_LIMIT])) {
122
            $model->getQuery()->limit(0, (int) $input[self::_LIMIT]);
0 ignored issues
show
Unused Code introduced by
The call to NilPortugues\Sql\QueryBu...ulation\Update::limit() has too many arguments starting with (int)$input[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

122
            $model->getQuery()->/** @scrutinizer ignore-call */ limit(0, (int) $input[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\Delete::limit() has too many arguments starting with (int)$input[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

122
            $model->getQuery()->/** @scrutinizer ignore-call */ limit(0, (int) $input[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...
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

122
            $model->getQuery()->/** @scrutinizer ignore-call */ limit(0, (int) $input[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...
123
        }
124
        return $model;
125
    }
126
127
    /**
128
     * return filtered request params
129
     *
130
     * @return array
131
     */
132
    protected function getFilteredInput(): array
133
    {
134
        return (new Filter($this->getParams(), [
135
            self::_NAME => FILTER_SANITIZE_STRING,
136
            self::_LIMIT => FILTER_SANITIZE_NUMBER_INT,
137
            self::_PAGE => FILTER_SANITIZE_NUMBER_INT,
138
        ]))->process()->toArray();
139
    }
140
}
141