ZippyBusProvider::getDirectionStops()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 9
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Itmedia\ZippyBusBundle;
6
7
use Itmedia\ZippyBusBundle\Client\ZippyBusClient;
8
use Itmedia\ZippyBusBundle\Factory\ScheduleObjectFromArrayFactory;
9
use Itmedia\ZippyBusBundle\Schedule\City;
10
use Itmedia\ZippyBusBundle\Schedule\ScheduleDate;
11
use Itmedia\ZippyBusBundle\Schedule\Direction;
12
use Itmedia\ZippyBusBundle\Schedule\Route;
13
use Itmedia\ZippyBusBundle\Schedule\Stop;
14
15
class ZippyBusProvider
16
{
17
18
    /**
19
     * @var ZippyBusClient
20
     */
21
    private $client;
22
23
    /**
24
     * @var ScheduleObjectFromArrayFactory
25
     */
26
    private $factory;
27
28
29
    public function __construct(ZippyBusClient $client, ScheduleObjectFromArrayFactory $factory)
30
    {
31
        $this->client = $client;
32
        $this->factory = $factory;
33
    }
34
35
36
    public function getCity(int $id): City
37
    {
38
        $content = $this->client->get('cities/{id}?include=currentVersions', [
39
            'id' => (string)$id
40
        ]);
41
42
        return $this->factory->createCity($content);
43
    }
44
45
46
    /**
47
     * @param City $city
48
     * @param ScheduleDate $date
49
     * @return Route[]
50
     */
51
    public function getRoutes(City $city, ScheduleDate $date): array
52
    {
53
        $content = $this->client->get('routes', [], [
54
            'query' => [
55
                'versionId' => $city->getVersion(),
56
                'include' => 'directions'
57
            ]
58
        ]);
59
60
        $routes = [];
61
        foreach ($content['list'] ?? [] as $routeArray) {
62
            $routes[] = $this->factory->createRoute($routeArray, $date);
63
        }
64
65
        usort($routes, function (Route $routeA, Route $routeB) {
66
            return strnatcmp($routeA->getName(), $routeB->getName());
67
        });
68
69
        return $routes;
70
    }
71
72
73
    /**
74
     * @param Direction $direction
75
     * @return Stop[]
76
     */
77
    public function getDirectionStops(Direction $direction): array
78
    {
79
        $content = $this->client->get('stops', [], [
80
            'query' => [
81
                'directionId' => $direction->getId(),
82
                'include' => 'schedule',
83
                'scheduleInclude' => 'partialMinutes'
84
            ]
85
        ]);
86
87
88
        $stops = [];
89
        foreach ($content['list'] ?? [] as $stopArray) {
90
            $stops[] = $this->factory->createStop($stopArray);
91
        }
92
93
94
        return $stops;
95
    }
96
}
97