Passed
Push — master ( 82aaad...ac7049 )
by Roberto
04:19
created

Api   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 16
dl 0
loc 90
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getResultsCollections() 0 7 1
A getServiceConfig() 0 4 1
A getServiceEndpoint() 0 3 1
A createRequest() 0 3 1
A __construct() 0 4 1
A getGoogleMapsApi() 0 3 1
A setGoogleMapsApi() 0 5 1
1
<?php
2
/**
3
 * Copyright (c) 2018 - present
4
 * Google Maps PHP - Api.php
5
 * author: Roberto Belotti - [email protected]
6
 * web : robertobelotti.com, github.com/biscolab
7
 * Initial version created on: 5/9/2018
8
 * MIT license: https://github.com/biscolab/google-maps-php/blob/master/LICENSE
9
 */
10
11
namespace Biscolab\GoogleMaps\Abstracts;
12
13
use Biscolab\GoogleMaps\Enum\GoogleMapsApiConfigFields;
14
use Biscolab\GoogleMaps\GoogleMapsApi;
15
use Biscolab\GoogleMaps\Http\GoogleMapsRequest;
16
use Biscolab\GoogleMaps\Http\GoogleMapsResultsCollection;
17
18
/**
19
 * Class Api
20
 * @package Biscolab\GoogleMaps\Abstracts
21
 */
22
abstract class Api {
23
24
	/**
25
	 * @var string
26
	 */
27
	const SERVICE_ENDPOINT = null;
28
29
	/**
30
	 * @var GoogleMapsApi
31
	 */
32
	protected $google_maps_api = null;
33
34
	/**
35
	 * @var string
36
	 */
37
	protected $result_collection = '';
38
39
	/**
40
	 * Api constructor.
41
	 *
42
	 * @param array $config
43
	 */
44
	public function __construct(array $config = []) {
45
46
		$service_config = $this->getServiceConfig($config);
47
		$this->setGoogleMapsApi(new GoogleMapsApi($service_config));
48
	}
49
50
	/**
51
	 * @param array $config
52
	 *
53
	 * @return array
54
	 */
55
	protected function getServiceConfig(array $config = []): array {
56
57
		return array_merge($config, [
58
			GoogleMapsApiConfigFields::SERVICE_ENDPOINT => $this->getServiceEndpoint()
59
		]);
60
	}
61
62
	/**
63
	 * @return string
64
	 */
65
	public function getServiceEndpoint(): string {
66
67
		return static::SERVICE_ENDPOINT;
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::SERVICE_ENDPOINT returns the type null which is incompatible with the type-hinted return string.
Loading history...
68
	}
69
70
	/**
71
	 * @return GoogleMapsApi
72
	 */
73
	public function getGoogleMapsApi(): GoogleMapsApi {
74
75
		return $this->google_maps_api;
76
	}
77
78
	/**
79
	 * @param GoogleMapsApi $google_maps_api
80
	 *
81
	 * @return Api
82
	 */
83
	public function setGoogleMapsApi(GoogleMapsApi $google_maps_api): Api {
84
85
		$this->google_maps_api = $google_maps_api;
86
87
		return $this;
88
	}
89
90
	/**
91
	 * @param array $params
92
	 *
93
	 * @return mixed
94
	 */
95
	public function createRequest(array $params): GoogleMapsRequest {
96
97
		return new GoogleMapsRequest($params);
98
	}
99
100
	/**
101
	 * @param GoogleMapsRequest $request
102
	 *
103
	 * @return GoogleMapsResultsCollection
104
	 */
105
	public function getResultsCollections(GoogleMapsRequest $request): GoogleMapsResultsCollection {
106
107
		$results = $this->getGoogleMapsApi()->get($request)->getResults();
108
109
		$result_collection_class = $this->result_collection;
110
111
		return new $result_collection_class($results);
112
	}
113
}