Completed
Push — develop ( f419bf...67a3ab )
by Michael
04:12
created

TimezoneDb::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 1 Features 0
Metric Value
c 1
b 1
f 0
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\ZoneInfo;
16
17
/**
18
 * @author Michael Crumm <[email protected]>
19
 */
20
class TimezoneDb extends AbstractHttpProvider
21
{
22
    const ENDPOINT = 'https://api.timezonedb.com/';
23
    const VIP_ENDPOINT = 'https://vip.timezonedb.com/';
24
    const FAIL = 'FAIL';
25
    const SUCCESS = 'OK';
26
27
    /**
28
     * @var string
29
     */
30
    private $apiKey;
31
32
    /**
33
     * @var string
34
     */
35
    private $host;
36
37
    /**
38
     * TimeZoneDB Constructor.
39
     *
40
     * @param string         $apiKey
41
     * @param bool           $premium
42
     * @param HttpClient     $client
43
     * @param MessageFactory $messageFactory
44
     */
45
    public function __construct(
46
        $apiKey,
47
        $premium = false,
48
        HttpClient $client = null,
49
        MessageFactory $messageFactory = null
50
    ) {
51 10
        parent::__construct($client, $messageFactory);
52
53 10
        $this->apiKey = $apiKey;
54 10
        $this->host = (bool) $premium ? static::VIP_ENDPOINT : static::ENDPOINT;
55
    }
56
57
    /**
58
     * Returns the name of this Provider.
59
     *
60
     * @return string
61
     */
62
    public function getName()
63
    {
64 1
        return 'timezone_db';
65
    }
66
67
    /**
68
     * Returns ZoneInfo for the specified location and timestamp.
69
     *
70
     * @param string|float $lat       Coordinate latitude.
71
     * @param string|float $lng       Coordinate longitude.
72
     * @param int          $timestamp UNIX timestamp used to determine Daylight Savings Time.
73
     *
74
     * @return ZoneInfo
75
     */
76
    public function find($lat, $lng, $timestamp = null)
77
    {
78 7
        $result = $this->getResult($lat, $lng, $timestamp);
79
80
        if (static::FAIL === $result->status) {
81 3
            throw new RuntimeException($result->message);
82
        }
83
84 4
        return new ZoneInfo($result->zoneName, $result->timestamp - $result->gmtOffset);
85
    }
86
87
    /**
88
     * Returns the URI for the specified location and timestamp.
89
     *
90
     * @param string|float $lat       Coordinate latitude.
91
     * @param string|float $lng       Coordinate longitude.
92
     * @param int|null     $timestamp Seconds since Jan 1, 1970 UTC.
93
     *
94
     * @return string
95
     */
96
    protected function buildUri($lat, $lng, $timestamp = null)
97
    {
98
        $params = [
99
            'key'    => $this->apiKey,
100
            'lat'    => $lat,
101
            'lng'    => $lng,
102
            'time'   => $timestamp,
103
            'format' => 'json',
104 7
        ];
105
106
        // Remove null values.
107 7
        $params = array_filter($params);
108
109 7
        return $this->host.'?'.http_build_query($params);
110
    }
111
}
112