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

Chain::find()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 18
ccs 4
cts 4
cp 1
rs 9.2
cc 4
eloc 10
nc 3
nop 3
crap 4
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 RuntimeException;
13
use Teazee\Exception\ChainNoResult;
14
use Teazee\ZoneInfo;
15
16
/**
17
 * Query multiple providers until a result is found.
18
 *
19
 * @author Michael Crumm <[email protected]>
20
 */
21
class Chain implements Provider
22
{
23
    /**
24
     * @var Provider[]
25
     */
26
    private $providers = [];
27
28
    /**
29
     * Chain constructor.
30
     *
31
     * @param array $providers
32
     */
33
    public function __construct(array $providers = [])
34
    {
35 7
        $this->providers = $providers;
36
    }
37
38
    /**
39
     * Returns the name of this provider.
40
     *
41
     * @return string
42
     */
43
    public function getName()
44
    {
45 1
        return 'chain';
46
    }
47
48
    /**
49
     * Finds a TimeZone for a location on the surface of the earth.
50
     *
51
     * @param string|float $lat       Coordinate latitude.
52
     * @param string|float $lng       Coordinate longitude.
53
     * @param int|null     $timestamp Seconds since Jan 1, 1970 UTC.
54
     *
55
     * @return ZoneInfo
56
     */
57
    public function find($lat, $lng, $timestamp = null)
58
    {
59 5
        $exceptions = [];
60
61
        /** @var Provider $provider */
62
        foreach ($this->providers as $provider) {
63
            try {
64 4
                return $provider->find($lat, $lng, $timestamp);
65
            } catch (RuntimeException $ex) {
66 2
                $exceptions[] = $ex;
67
            }
68
        }
69
70
        throw new ChainNoResult(
71
            sprintf('No provider could find %f,%f%s', $lat, $lng, $timestamp ? ' @'.$timestamp : ''),
72
            $exceptions
73 2
        );
74
    }
75
}
76