Completed
Push — master ( db493f...681b7e )
by Pierre
06:39 queued 03:40
created

Metro::lines()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 4
b 0
f 0
nc 1
nop 0
dl 0
loc 10
ccs 8
cts 8
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
    /**
18
     * Lines model
19
     *
20
     * @var Lines
21
     */
22
    protected $modelLines;
23
24
25
    /**
26
     * Stations model
27
     *
28
     * @var Stations
29
     */
30
    protected $modelStations;
31
32
    /**
33
     * db core
34
     *
35
     * @var Core
36
     */
37
    protected $dbCore;
38
39
    /**
40
     * instanciate
41
     *
42
     * @param Container $container
43
     */
44 4
    public function __construct(Container $container)
45
    {
46 4
        parent::__construct($container);
47 4
        $this->modelLines = new Lines($container);
48 4
        $this->modelStations = new Stations($container);
49 4
        $this->dbCore = new Core($container);
50
    }
51
52
    /**
53
     * search lines
54
     *
55
     * @return Metro
56
     */
57 1
    final public function lines(): Metro
58
    {
59 1
        $query = $this->search($this->modelLines);
60 1
        $this->response->setCode(Response::HTTP_OK)->setContent([
61 1
            Response::_ERROR => false,
62 1
            Response::_ERROR_MSG => '',
63 1
            'datas' => $this->getQueryResults($query)
64
        ]);
65 1
        unset($query);
66 1
        return $this;
67
    }
68
69
    /**
70
     * search stations
71
     *
72
     * @return Metro
73
     */
74 1
    final public function stations(): Metro
75
    {
76 1
        $query = $this->search($this->modelStations);
77 1
        $this->response->setCode(Response::HTTP_OK)->setContent([
78 1
            Response::_ERROR => false,
79 1
            Response::_ERROR_MSG => '',
80 1
            'datas' => $this->getQueryResults($query)
81
        ]);
82 1
        unset($query);
83 1
        return $this;
84
    }
85
86
    /**
87
     * get results as array
88
     *
89
     * @param Orm $query
90
     * @return array
91
     */
92 1
    protected function getQueryResults(Orm $query)
93
    {
94 1
        $sql = $query->getSql();
95 1
        return $this->dbCore->fromOrm($query)->run(
96 1
            $sql,
97 1
            $query->getBuilderValues()
98 1
        )->hydrate()
99 1
            ->getRowset();
100
    }
101
102
    /**
103
     * search items from name limiting results amount
104
     *
105
     * @param Orm $model
106
     * @return Orm
107
     */
108
    protected function search(Orm &$model): Orm
109
    {
110
        $input = $this->getFilteredInput();
111
        $name = (isset($input['name']))
112
            ? '%' . $input['name'] . '%'
113
            : '%';
114
        $model->find(['*'], ['src#' => $name]);
115
        if (isset($input['limit'])) {
116
            $model->getQuery()->limit(0, (int) $input['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['limit']. ( Ignorable by Annotation )

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

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

116
            $model->getQuery()->/** @scrutinizer ignore-call */ limit(0, (int) $input['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)$input['limit']. ( Ignorable by Annotation )

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

116
            $model->getQuery()->/** @scrutinizer ignore-call */ limit(0, (int) $input['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...
117
        }
118
        return $model;
119
    }
120
121
    /**
122
     * return filtered request params
123
     *
124
     * @return array
125
     */
126
    protected function getFilteredInput(): array
127
    {
128
        return (new Filter($this->getParams(), [
129
            'name' => FILTER_SANITIZE_STRING,
130
            'limit' => FILTER_SANITIZE_NUMBER_INT,
131
            'page' => FILTER_SANITIZE_NUMBER_INT,
132
        ]))->process()->toArray();
133
    }
134
}
135