Completed
Push — master ( 7a5cb9...59abba )
by Hannes
02:54
created

Reader::getBoundsFor()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
ccs 14
cts 14
cp 1
rs 9.3142
cc 3
eloc 14
nc 2
nop 2
crap 3
1
<?php
2
3
/*
4
 * This file is part of the Runalyze DEM Reader.
5
 *
6
 * (c) RUNALYZE <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Runalyze\DEM;
13
14
use Runalyze\DEM\Exception\RuntimeException;
15
use Runalyze\DEM\Provider\ProviderInterface;
16
17
class Reader implements ReaderInterface
18
{
19
    /** @var ProviderInterface[] */
20
    protected $Provider = [];
21
22
    /** @var bool */
23
    protected $InvalidFlag = false;
24
25
    /**
26
     * @param ProviderInterface|null $provider
27
     */
28 5
    public function __construct(ProviderInterface $provider = null)
29
    {
30 5
        if (null !== $provider) {
31 2
            $this->addProvider($provider);
32 2
        }
33 5
    }
34
35
    /**
36
     * @param ProviderInterface $provider
37
     */
38 3
    public function addProvider(ProviderInterface $provider)
39
    {
40 3
        $this->Provider[] = $provider;
41 3
    }
42
43
    /**
44
     * @return int
45
     */
46 1
    public function numberOfProviders()
47
    {
48 1
        return count($this->Provider);
49
    }
50
51
    /**
52
     * @return bool
53
     */
54 1
    public function hasProviders()
55
    {
56 1
        return !empty($this->Provider);
57
    }
58
59
    /**
60
     * @param  array $latitudeLongitudes array(array($lat, $lng), ...)
61
     * @return bool
62
     */
63 3
    public function hasDataFor(array $latitudeLongitudes)
64
    {
65 3
        foreach ($this->Provider as $provider) {
66 2
            if ($provider->hasDataFor($latitudeLongitudes)) {
67 2
                return true;
68
            }
69 2
        }
70
71 1
        return false;
72
    }
73
74
    /**
75
     * @param  array            $latitudes
76
     * @param  array            $longitudes
77
     * @return array            elevations [m] can be false if nothing retrieved
78
     * @throws RuntimeException
79
     */
80 4
    public function getElevations(array $latitudes, array $longitudes)
81
    {
82 4
        $boundaries = $this->getBoundsFor($latitudes, $longitudes);
83
84 4
        foreach ($this->Provider as $provider) {
85 3
            if ($provider->hasDataFor($boundaries)) {
86 2
                return $provider->getElevations($latitudes, $longitudes);
87
            }
88 3
        }
89
90 2
        throw new RuntimeException('No provider can handle the given locations.');
91
    }
92
93
    /**
94
     * @param  float[] $latitudes
95
     * @param  float[] $longitudes
96
     * @return array   array(array($lat, $lng), ...)
97
     */
98 4
    protected function getBoundsFor(array $latitudes, array $longitudes)
99
    {
100 4
        $filteredLatitudes = array_filter($latitudes);
101 4
        $filteredLongitudes = array_filter($longitudes);
102
103 4
        if (empty($filteredLatitudes) || empty($filteredLongitudes)) {
104 1
            return [];
105
        }
106
107 4
        $minLatitude = min($filteredLatitudes);
108 4
        $maxLatitude = max($filteredLatitudes);
109 4
        $minLongitude = min($filteredLongitudes);
110 4
        $maxLongitude = max($filteredLongitudes);
111
112
        return [
113 4
            [$minLatitude, $minLongitude],
114 4
            [$minLatitude, $maxLongitude],
115 4
            [$maxLatitude, $minLongitude],
116 4
            [$maxLatitude, $maxLongitude],
117 4
        ];
118
    }
119
}
120