Passed
Push — master ( 7ec869...8ef4a4 )
by Damien
01:57
created

DistanceMatrixResponse::getResponseObject()   A

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
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace DH\NavigationBundle\Provider\GoogleMaps\DistanceMatrix;
4
5
use DH\NavigationBundle\Contract\DistanceMatrix\DistanceMatrixResponseInterface;
6
use DH\NavigationBundle\Helper\FormatHelper;
7
use DH\NavigationBundle\Model\Address;
8
use DH\NavigationBundle\Model\Distance;
9
use DH\NavigationBundle\Model\Duration;
10
use DH\NavigationBundle\Model\Element;
11
use DH\NavigationBundle\Model\Row;
12
use Psr\Http\Message\ResponseInterface;
13
14
class DistanceMatrixResponse implements DistanceMatrixResponseInterface
15
{
16
    /**
17
     * @var string
18
     */
19
    private $status;
20
21
    /**
22
     * @var \stdClass
23
     */
24
    private $responseObject;
25
26
    /**
27
     * @var Address[]|array
28
     */
29
    private $originAddresses;
30
31
    /**
32
     * @var Address[]|array
33
     */
34
    private $destinationAddresses;
35
36
    /**
37
     * @var array|Row[]
38
     */
39
    private $rows;
40
41
    public function __construct(ResponseInterface $response, array $origins, array $destinations)
0 ignored issues
show
Unused Code introduced by
The parameter $destinations is not used and could be removed. ( Ignorable by Annotation )

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

41
    public function __construct(ResponseInterface $response, array $origins, /** @scrutinizer ignore-unused */ array $destinations)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $origins is not used and could be removed. ( Ignorable by Annotation )

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

41
    public function __construct(ResponseInterface $response, /** @scrutinizer ignore-unused */ array $origins, array $destinations)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
42
    {
43
        $responseObject = json_decode($response->getBody()->getContents());
44
        $this->responseObject = $responseObject;
45
        $this->originAddresses = [];
46
        $this->destinationAddresses = [];
47
        $this->rows = [];
48
        $this->initialize();
49
    }
50
51
    private function addOriginAddress(Address $originAddress): void
52
    {
53
        $this->originAddresses[] = $originAddress;
54
    }
55
56
    private function addDestinationAddress(Address $destinationAddress): void
57
    {
58
        $this->destinationAddresses[] = $destinationAddress;
59
    }
60
61
    private function addRow(Row $row): void
62
    {
63
        $this->rows[] = $row;
64
    }
65
66
    /**
67
     * @return mixed
68
     */
69
    public function getStatus(): string
70
    {
71
        return $this->status;
72
    }
73
74
    /**
75
     * @return \stdClass
76
     */
77
    public function getResponseObject(): \stdClass
78
    {
79
        return $this->responseObject;
80
    }
81
82
    /**
83
     * @return array
84
     */
85
    public function getOriginAddresses(): array
86
    {
87
        return $this->originAddresses;
88
    }
89
90
    /**
91
     * @return array
92
     */
93
    public function getDestinationAddresses(): array
94
    {
95
        return $this->destinationAddresses;
96
    }
97
98
    /**
99
     * @return array
100
     */
101
    public function getRows(): array
102
    {
103
        return $this->rows;
104
    }
105
106
    private function initialize(): void
107
    {
108
        $this->status = $this->responseObject->status;
109
110
        foreach ($this->responseObject->origin_addresses as $originAddress) {
111
            $this->addOriginAddress(new Address($originAddress));
112
        }
113
114
        foreach ($this->responseObject->destination_addresses as $destinationAddress) {
115
            $this->addDestinationAddress(new Address($destinationAddress));
116
        }
117
118
        foreach ($this->responseObject->rows as $row) {
119
            $elements = [];
120
            foreach ($row->elements as $element) {
121
                if (0 === strcmp($element->status, Element::STATUS_ZERO_RESULTS)) { //avoid a crash when no route was found
122
                    //todo here happens something strange
123
                    $elements[] = new Element($element->status, new Duration(), new Distance());
124
125
                    continue;
126
                }
127
128
//                $duration = new Duration($element->duration->text, $element->duration->value);
129
//                $distance = new Distance($element->distance->text, $element->distance->value);
130
                $duration = new Duration(FormatHelper::formatTime($element->duration->value), $element->duration->value);
131
                $distance = new Distance(FormatHelper::formatDistance($element->distance->value), $element->distance->value);
132
                $elements[] = new Element($element->status, $duration, $distance);
133
            }
134
            $this->addRow(new Row($elements));
135
        }
136
    }
137
}
138