Elevation::getBySampledPath()   A
last analyzed

Complexity

Conditions 6
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 13.776

Importance

Changes 2
Bugs 0 Features 2
Metric Value
eloc 9
c 2
b 0
f 2
dl 0
loc 17
ccs 4
cts 10
cp 0.4
rs 9.2222
cc 6
nc 3
nop 2
crap 13.776
1
<?php
2
/**
3
 * Copyright (c) 2018 - present
4
 * Google Maps PHP - Elevation.php
5
 * author: Roberto Belotti - [email protected]
6
 * web : robertobelotti.com, github.com/biscolab
7
 * Initial version created on: 28/9/2018
8
 * MIT license: https://github.com/biscolab/google-maps-php/blob/master/LICENSE
9
 */
10
11
namespace Biscolab\GoogleMaps\Api;
12
13
use Biscolab\GoogleMaps\Abstracts\Api;
14
use Biscolab\GoogleMaps\Exception\InvalidArgumentException;
15
use Biscolab\GoogleMaps\Fields\GoogleMapsRequestFields;
16
use Biscolab\GoogleMaps\Http\GoogleMapsResultsCollection;
17
use Biscolab\GoogleMaps\Http\Result\ElevationResultsCollection;
18
use Biscolab\GoogleMaps\Object\LatLng;
19
use Biscolab\GoogleMaps\Object\Path;
20
21
/**
22
 * Class Elevation
23
 * @package Biscolab\GoogleMaps\Api
24
 *
25
 * @since   0.3.0
26
 * @see     https://developers.google.com/maps/documentation/elevation/start
27
 */
28
class Elevation extends Api
29
{
30
31
	/**
32
	 * @var string
33
	 */
34
	const SERVICE_ENDPOINT = 'elevation';
35
36
	/**
37
	 * @var string
38
	 */
39
	protected $result_collection_type = ElevationResultsCollection::class;
40
41
	/**
42
	 * Positional Requests
43
	 *
44
	 * @param LatLng|string|array $locations
45
	 * This parameter takes either a single location or multiple locations passed as an array or as an encoded polyline
46
	 *
47
	 * @return GoogleMapsResultsCollection
48
	 *
49
	 * @since 0.3.0
50
	 */
51
	public function getByLocations($locations): GoogleMapsResultsCollection
52
	{
53
54
		$locations = $this->parseLocations($locations);
55
56
		return $this->callApi([
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->callApi(ar...CATIONS => $locations)) could return the type Biscolab\GoogleMaps\Http\GoogleMapsResult which is incompatible with the type-hinted return Biscolab\GoogleMaps\Http...leMapsResultsCollection. Consider adding an additional type-check to rule them out.
Loading history...
57
			GoogleMapsRequestFields::LOCATIONS => $locations
58
		]);
59
	}
60
61
	/**
62
	 * @param array|string $locations
63
	 *
64
	 * @return string
65
	 *
66
	 * @since   0.3.0
67
	 */
68 2
	public function parseLocations($locations): string
69
	{
70
71 2
		if ($locations instanceof Path) {
0 ignored issues
show
introduced by
$locations is never a sub-type of Biscolab\GoogleMaps\Object\Path.
Loading history...
72
			$locations = $locations->toArray();
73
		}
74
75 2
		if (is_array($locations)) {
76
			$locations = implode('|', array_map(function ($item) {
77
78 1
				return (string)$item;
79 1
			}, $locations));
80
		}
81
82 2
		return (string)$locations;
83
	}
84
85
	/**
86
	 * Sampled Path Requests
87
	 *
88
	 * @param array|string $path
89
	 * This parameter takes either a multiple locations passed as an array or as an encoded polyline
90
	 *
91
	 * @param int          $samples
92
	 * This will be the number of results as well
93
	 *
94
	 * @throws InvalidArgumentException
95
	 * @return GoogleMapsResultsCollection
96
	 *
97
	 * @since 0.4.0
98
	 */
99 3
	public function getBySampledPath($path, int $samples): GoogleMapsResultsCollection
100
	{
101
102 3
		if ((is_array($path) && count($path) < 2) ||
103 3
			$path instanceof Path && $path->count() < 2) {
104 3
			throw new InvalidArgumentException('The number of items provided in the path must be greater than 1 (One)');
105
		}
106
107
		if ($samples <= 0) {
108
			throw new InvalidArgumentException('The number of samples must be greater than 0 (Zero)');
109
		}
110
111
		$path = $this->parseLocations($path);
112
113
		return $this->callApi([
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->callApi(ar...::SAMPLES => $samples)) could return the type Biscolab\GoogleMaps\Http\GoogleMapsResult which is incompatible with the type-hinted return Biscolab\GoogleMaps\Http...leMapsResultsCollection. Consider adding an additional type-check to rule them out.
Loading history...
114
			GoogleMapsRequestFields::PATH    => $path,
115
			GoogleMapsRequestFields::SAMPLES => $samples,
116
		]);
117
	}
118
119
}