RoutingResponse::getStatus()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace DH\NavigationBundle\Provider\Here\Routing;
4
5
use DH\NavigationBundle\Contract\Routing\RoutingResponseInterface;
6
use DH\NavigationBundle\Model\Routing\Leg;
7
use DH\NavigationBundle\Model\Routing\Route;
8
use DH\NavigationBundle\Model\Routing\Step;
9
use DH\NavigationBundle\Model\Routing\Summary;
10
use Psr\Http\Message\ResponseInterface;
11
use stdClass;
12
13
class RoutingResponse implements RoutingResponseInterface
14
{
15
    /**
16
     * @var string
17
     */
18
    private $status;
19
20
    /**
21
     * @var stdClass
22
     */
23
    private $responseObject;
24
25
    /**
26
     * @var array
27
     */
28
    private $routes;
29
30
    public function __construct(ResponseInterface $response)
31
    {
32
        $responseObject = json_decode($response->getBody()->getContents());
33
        $this->status = $response->getReasonPhrase();
34
        $this->responseObject = $responseObject;
35
        $this->routes = [];
36
37
        $this->initialize();
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function getStatus(): string
44
    {
45
        return $this->status;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function getResponseObject(): stdClass
52
    {
53
        return $this->responseObject;
54
    }
55
56
    private function initialize(): void
57
    {
58
        // routes, waypoints, legs, steps
59
        foreach ($this->responseObject->response->route as $routeElement) {
60
            $legs = [];
61
62
            foreach ($routeElement->leg as $legElement) {
63
                $steps = [];
64
                foreach ($legElement->maneuver as $stepElement) {
65
                    $steps[] = new Step([
66
                        'position' => (array) $stepElement->position,
67
                        'instruction' => $stepElement->instruction,
68
                        'duration' => $stepElement->travelTime,
69
                        'distance' => $stepElement->length,
70
                    ]);
71
                }
72
73
                $legs[] = new Leg([
74
                    'start' => (array) $legElement->start->originalPosition,
75
                    'end' => (array) $legElement->end->originalPosition,
76
                    'duration' => $legElement->travelTime,
77
                    'distance' => $legElement->length,
78
                    'steps' => $steps,
79
                ]);
80
            }
81
82
            $this->routes[] = new Route([
83
                'legs' => $legs,
84
                'summary' => new Summary((array) $routeElement->summary),
85
            ]);
86
        }
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function getRoutes(): array
93
    {
94
        return $this->routes;
95
    }
96
}
97