Completed
Push — master ( c7b4fd...c91e7e )
by Pierre
03:04
created

Metro::lines()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 3
b 0
f 0
nc 1
nop 0
dl 0
loc 10
ccs 0
cts 7
cp 0
crap 2
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
14
final class Metro extends AbstractApi implements IApi
15
{
16
    /**
17
     * Lines model
18
     *
19
     * @var Lines
20
     */
21
    protected $modelLines;
22
23
24
    /**
25
     * Stations model
26
     *
27
     * @var Stations
28
     */
29
    protected $modelStations;
30
31
    /**
32
     * db core
33
     *
34
     * @var Core
35
     */
36
    protected $dbCore;
37
38
    /**
39
     * instanciate
40
     *
41
     * @param Container $container
42
     */
43 1
    public function __construct(Container $container)
44
    {
45 1
        parent::__construct($container);
46 1
        $this->modelLines = new Lines($container);
47 1
        $this->modelStations = new Stations($container);
48 1
        $this->dbCore = new Core($container);
49
    }
50
51
    /**
52
     * search lines
53
     *
54
     * @return Metro
55
     */
56
    final public function lines(): Metro
57
    {
58
        $this->response->setCode(Response::HTTP_OK)->setContent([
59
            Response::_ERROR => false,
60
            Response::_ERROR_MSG => '',
61
            'datas' => $this->getQueryResults(
62
                $this->modelLines->find(['*'], [])
63
            )
64
        ]);
65
        return $this;
66
    }
67
68
    /**
69
     * search stations
70
     *
71
     * @return Metro
72
     */
73
    final public function stations(): Metro
74
    {
75
        $this->response->setCode(Response::HTTP_OK)->setContent([
76
            Response::_ERROR => false,
77
            Response::_ERROR_MSG => '',
78
            'datas' => $this->getQueryResults(
79
                $this->modelStations->find(['*'], [])
80
            )
81
        ]);
82
        return $this;
83
    }
84
85
    /**
86
     * get query results
87
     *
88
     * @param Orm $query
89
     * @return array
90
     */
91
    protected function getQueryResults(Orm $query)
92
    {
93
        return $this->dbCore->fromOrm($query)->run(
94
            $query->getSql(),
95
            $query->getBuilderValues()
96
        )->hydrate()
97
            ->getRowset();
98
    }
99
}
100