Passed
Push — master ( 6c64d1...ac3cce )
by Linh
04:24 queued 02:04
created

GoogleDistance::getApiKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Pnlinh\GoogleDistance;
4
5
use Exception;
6
use GuzzleHttp\Client;
7
use Pnlinh\GoogleDistance\Contracts\GoogleDistanceContract;
8
9
class GoogleDistance implements GoogleDistanceContract
10
{
11
    /** @var string */
12
    private $apiUrl = 'https://maps.googleapis.com/maps/api/distancematrix/json';
13
14
    /** @var string */
15
    private $apiKey;
16
17
    /** @var string */
18
    private $origins;
19
20
    /** @var string */
21
    private $destinations;
22
23
    /** @var string */
24
    private $units;
25
26
    /**
27
     * GoogleDistance constructor.
28
     *
29
     * @param string $apiKey
30
     * @param string $units
31
     */
32
    public function __construct(string $apiKey, string $units = 'imperial')
33
    {
34
        $this->apiKey = $apiKey;
35
        $this->units = $units;
36
    }
37
38
    /**
39
     * Get API_KEY.
40
     *
41
     * @return string
42
     */
43
    public function getApiKey(): string
44
    {
45
        return $this->apiKey;
46
    }
47
48
    /**
49
     * Get units.
50
     *
51
     * @return string
52
     */
53
    public function getUnits(): string
54
    {
55
        return $this->units;
56
    }
57
58
    /**
59
     * Set units.
60
     *
61
     * @param $units
62
     *
63
     * @return GoogleDistance
64
     */
65
    public function setUnits($units): self
66
    {
67
        $this->units = $units;
68
69
        return $this;
70
    }
71
72
    /**
73
     * Get origins.
74
     *
75
     * @return string
76
     */
77
    public function getOrigins(): string
78
    {
79
        return $this->origins;
80
    }
81
82
    /**
83
     * Set origins.
84
     *
85
     * @param string $origins
86
     *
87
     * @return GoogleDistance
88
     */
89
    public function setOrigins(string $origins): self
90
    {
91
        $this->origins = $origins;
92
93
        return $this;
94
    }
95
96
    /**
97
     * Get destinations.
98
     *
99
     * @return string
100
     */
101
    public function getDestinations(): string
102
    {
103
        return $this->destinations;
104
    }
105
106
    /**
107
     * Set destinations.
108
     *
109
     * @param string $destinations
110
     *
111
     * @return GoogleDistance
112
     */
113
    public function setDestinations(string $destinations): self
114
    {
115
        $this->destinations = $destinations;
116
117
        return $this;
118
    }
119
120
    /**
121
     * {@inheritDoc}
122
     */
123
    public function calculate(string $origins, string $destinations, ?string $overrideUnits = null): int
124
    {
125
        $client = new Client();
126
127
        try {
128
            $response = $client->get($this->apiUrl, [
129
                'query' => [
130
                    'units'        => $overrideUnits ?? $this->units,
131
                    'origins'      => $origins,
132
                    'destinations' => $destinations,
133
                    'key'          => $this->getApiKey(),
134
                    'random'       => random_int(1, 100),
135
                ],
136
            ]);
137
138
            $statusCode = $response->getStatusCode();
139
140
            if (200 === $statusCode) {
141
                $responseData = json_decode($response->getBody()->getContents());
142
143
                if (isset($responseData->rows[0]->elements[0]->distance)) {
144
                    return $responseData->rows[0]->elements[0]->distance->value;
145
                }
146
            }
147
148
            return -1;
149
        } catch (Exception $e) {
150
            return -1;
151
        }
152
    }
153
}
154