Completed
Pull Request — develop (#24)
by Michael
01:48
created

GoogleMaps::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * This file is part of the Teazee package.
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @license    MIT License
9
 */
10
namespace Teazee\Provider;
11
12
use Http\Client\HttpClient;
13
use Http\Message\MessageFactory;
14
use RuntimeException;
15
use Teazee\Model\ZoneInfo;
16
17
/**
18
 * @author Michael Crumm <[email protected]>
19
 */
20
class GoogleMaps extends AbstractHttpProvider
21
{
22
    const ENDPOINT = 'https://maps.googleapis.com/maps/api/timezone/json';
23
24
    /**
25
     * @var array
26
     */
27
    private $errorStatus = [
28
        'INVALID_REQUEST',
29
        'OVER_QUERY_LIMIT',
30
        'REQUEST_DENIED',
31
        'UNKNOWN_ERROR',
32
        'ZERO_RESULTS',
33
    ];
34
35
    /**
36
     * @var string
37
     */
38
    private $apiKey;
39
40
    /**
41
     * Google constructor.
42
     *
43
     * @param string         $apiKey
44
     * @param HttpClient     $client
45
     * @param MessageFactory $messageFactory
46
     */
47
    public function __construct($apiKey = null, HttpClient $client = null, MessageFactory $messageFactory = null)
48
    {
49 7
        $this->apiKey = $apiKey;
50
51 7
        parent::__construct($client, $messageFactory);
52
    }
53
54
    /**
55
     * Returns the name of this provider.
56
     *
57
     * @return string
58
     */
59
    public function getName()
60
    {
61 1
        return 'googlemaps';
62
    }
63
64
    /**
65
     * Returns ZoneInfo for the specified location and timestamp.
66
     *
67
     * @param string|float $lat       Coordinate latitude.
68
     * @param string|float $lng       Coordinate longitude.
69
     * @param int          $timestamp UNIX timestamp used to determine Daylight Savings Time.
70
     *
71
     * @throws RuntimeException
72
     *
73
     * @return ZoneInfo
74
     */
75
    public function find($lat, $lng, $timestamp = null)
76
    {
77 4
        $timestamp = $timestamp ?: $this->getCurrentTimestamp();
78 4
        $query = $this->buildQuery($lat, $lng, $timestamp);
79 4
        $response = $this->getResponse($query);
80 4
        $data = json_decode($response->getBody()->getContents());
81
82
        if (in_array($data->status, $this->errorStatus)) {
83 1
            $message = isset($data->errorMessage) ? $data->errorMessage : $data->status;
84 1
            throw new RuntimeException($message);
85
        }
86
87
        return $this->returnResult(array_merge($this->getDefaults(), [
88
            'id'        => $data->timeZoneId,
89
            'dst'       => $data->dstOffset !== 0,
90
            'timestamp' => $timestamp,
91
            'utcOffset' => $data->rawOffset,
92 3
        ]));
93
    }
94
95
    /**
96
     * Returns the URI for the specified location and timestamp.
97
     *
98
     * @param string|float $lat       Coordinate latitude.
99
     * @param string|float $lng       Coordinate longitude.
100
     * @param int          $timestamp UNIX timestamp used to determine Daylight Savings Time.
101
     *
102
     * @return string
103
     */
104
    private function buildQuery($lat, $lng, $timestamp)
105
    {
106
        $params = [
107
            'key'       => $this->apiKey,
108
            'location'  => implode(',', [$lat, $lng]),
109
            'timestamp' => $timestamp,
110 4
        ];
111
112
        // Remove null values.
113 4
        $params = array_filter($params);
114
115 4
        return static::ENDPOINT.'?'.http_build_query($params);
116
    }
117
}
118