Completed
Push — master ( d33c99...5d6517 )
by Roberto
03:55 queued 10s
created

Api::responseHasNewPage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 2
nc 2
nop 0
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\Fields\GoogleMapsRequestFields;
15
use Biscolab\GoogleMaps\GoogleMapsApi;
16
use Biscolab\GoogleMaps\Http\GoogleMapsRequest;
17
use Biscolab\GoogleMaps\Http\GoogleMapsResponse;
18
use Biscolab\GoogleMaps\Http\GoogleMapsResultsCollection;
19
20
/**
21
 * Class Api
22
 * @package Biscolab\GoogleMaps\Abstracts
23
 */
24
abstract class Api
25
{
26
27
	/**
28
	 * @var string
29
	 */
30
	const SERVICE_ENDPOINT = null;
31
32
	/**
33
	 * @var GoogleMapsApi
34
	 */
35
	protected $google_maps_api = null;
36
37
	/**
38
	 * @var string
39
	 */
40
	protected $result_collection = '';
41
42
	/**
43
	 * @var GoogleMapsResponse
44
	 */
45
	protected $response;
46
47
	/**
48
	 * @var GoogleMapsRequest
49
	 */
50
	protected $request;
51
52
	/**
53
	 * Api constructor.
54
	 *
55
	 * @param array $config
56
	 */
57
	public function __construct(array $config = [])
58
	{
59
60
		$service_config = $this->getServiceConfig($config);
61
		$this->setGoogleMapsApi(new GoogleMapsApi($service_config));
62
	}
63
64
	/**
65
	 * @param array $config
66
	 *
67
	 * @return array
68
	 */
69
	protected function getServiceConfig(array $config = []): array
70
	{
71
72
		return array_merge($config, [
73
			GoogleMapsApiConfigFields::SERVICE_ENDPOINT => $this->getServiceEndpoint()
74
		]);
75
	}
76
77
	/**
78
	 * @return string
79
	 */
80
	public function getServiceEndpoint(): string
81
	{
82
83
		return static::SERVICE_ENDPOINT ?? '';
84
	}
85
86
	/**
87
	 * @param array       $params
88
	 * @param null|string $endpoint
89
	 *
90
	 * @return GoogleMapsResultsCollection
91
	 */
92
	public function callApi(array $params, ?string $endpoint = null): GoogleMapsResultsCollection
93
	{
94
95
		$this->createRequest($params, $endpoint);
96
97
		return $this->getResultsCollections();
98
	}
99
100
	/**
101
	 * @param array       $params
102
	 * @param null|string $endpoint since 0.5.0
103
	 *
104
	 * @return GoogleMapsRequest
105
	 */
106
	public function createRequest(array $params, ?string $endpoint = null): GoogleMapsRequest
107
	{
108
109
		$this->request = new GoogleMapsRequest($params, $endpoint);;
110
111
		return $this->request;
112
	}
113
114
	/**
115
	 * @return GoogleMapsResultsCollection
116
	 */
117
	public function getResultsCollections(): GoogleMapsResultsCollection
118
	{
119
120
		$results = $this->getResponse()->getResults();
121
122
		$result_collection_class = $this->result_collection;
123
124
		return new $result_collection_class($results);
125
	}
126
127
	/**
128
	 * @return GoogleMapsResponse
129
	 */
130
	public function getResponse(): GoogleMapsResponse
131
	{
132
133
		$this->response = $this->getGoogleMapsApi()->get($this->request);
134
135
		return $this->response;
136
	}
137
138
	/**
139
	 * @return GoogleMapsApi
140
	 */
141
	public function getGoogleMapsApi(): GoogleMapsApi
142
	{
143
144
		return $this->google_maps_api;
145
	}
146
147
	/**
148
	 * @param GoogleMapsApi $google_maps_api
149
	 *
150
	 * @return Api
151
	 */
152
	public function setGoogleMapsApi(GoogleMapsApi $google_maps_api): Api
153
	{
154
155
		$this->google_maps_api = $google_maps_api;
156
157
		return $this;
158
	}
159
160
	/**
161
	 * @return GoogleMapsResultsCollection
162
	 */
163
	public function getNextPage(): GoogleMapsResultsCollection
164
	{
165
166
		if ($this->responseHasNewPage()) {
167
			$this->request->setParam(GoogleMapsRequestFields::NEXT_PAGE_TOKEN, $this->response->getNextPageToken());
168
		}
169
170
		return $this->getResultsCollections();
171
	}
172
173
	/**
174
	 * @return bool
175
	 */
176
	public function responseHasNewPage(): bool
177
	{
178
179
		return ($this->response instanceof GoogleMapsResponse) ? $this->response->getNextPageToken() : false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->response i...NextPageToken() : false returns the type string which is incompatible with the type-hinted return boolean.
Loading history...
introduced by
$this->response is always a sub-type of Biscolab\GoogleMaps\Http\GoogleMapsResponse.
Loading history...
180
	}
181
}