DeviceView::getRedirectResponseBySwitchParam()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 0
cts 14
cp 0
rs 9.536
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 20
1
<?php
2
/**
3
 * DeviceView.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        https://www.ipublikuj.eu
7
 * @author         Adam Kadlec <[email protected]>
8
 * @package        iPublikuj:MobileDetect!
9
 * @subpackage     Helpers
10
 * @since          1.0.0
11
 *
12
 * @date           23.04.14
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\MobileDetect\Helpers;
18
19
use Nette;
20
use Nette\Application;
21
use Nette\Http;
22
23
/**
24
 * Device view detector helper
25
 *
26
 * @package        iPublikuj:MobileDetect!
27
 * @subpackage     Helpers
28
 *
29
 * @author         Adam Kadlec <[email protected]>
30
 */
31 1
final class DeviceView
32
{
33
	/**
34
	 * Implement nette smart magic
35
	 */
36 1
	use Nette\SmartObject;
37
38
	public const VIEW_MOBILE = 'mobile';
39
	public const VIEW_PHONE = 'phone';
40
	public const VIEW_TABLET = 'tablet';
41
	public const VIEW_FULL = 'full';
42
	public const VIEW_NOT_MOBILE = 'not_mobile';
43
44
	/**
45
	 * @var Http\IRequest
46
	 */
47
	private $httpRequest;
48
49
	/**
50
	 * @var Http\IResponse
51
	 */
52
	private $httpResponse;
53
54
	/**
55
	 * @var string
56
	 */
57
	private $viewType = self::VIEW_FULL;
58
59
	/**
60
	 * @var CookieSettings
61
	 */
62
	private $cookieSettings;
63
64
	/**
65
	 * @var string
66
	 */
67
	private $switchParameterName = 'device_view';
68
69
	/**
70
	 * @param string $setSwitchParameterName
71
	 * @param CookieSettings $cookieSettings
72
	 * @param Http\IRequest $httpRequest
73
	 * @param Http\IResponse $httpResponse
74
	 */
75
	public function __construct(string $setSwitchParameterName, CookieSettings $cookieSettings, Http\IRequest $httpRequest, Http\IResponse $httpResponse)
76
	{
77 1
		$this->cookieSettings = $cookieSettings;
78 1
		$this->httpRequest = $httpRequest;
79 1
		$this->httpResponse = $httpResponse;
80
81 1
		$this->switchParameterName = $setSwitchParameterName;
82
83 1
		if ($this->httpRequest->getQuery($this->switchParameterName) ?? false) {
84 1
			$this->viewType = $this->httpRequest->getQuery($this->switchParameterName);
85
86 1
		} elseif ($this->httpRequest->getCookie($this->cookieSettings->getName())) {
0 ignored issues
show
Bug introduced by
Consider using $this->cookieSettings->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
87
			$this->viewType = $this->httpRequest->getCookie($this->cookieSettings->getName());
0 ignored issues
show
Bug introduced by
Consider using $this->cookieSettings->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
88
		}
89 1
	}
90
91
	/**
92
	 * Gets the view type for a device
93
	 *
94
	 * @return string
95
	 */
96
	public function getViewType() : string
97
	{
98 1
		return $this->viewType;
99
	}
100
101
	/**
102
	 * Is the device in full view
103
	 *
104
	 * @return bool
105
	 */
106
	public function isFullView() : bool
107
	{
108 1
		return $this->viewType === self::VIEW_FULL;
109
	}
110
111
	/**
112
	 * Is the device a tablet view type
113
	 *
114
	 * @return bool
115
	 */
116
	public function isTabletView() : bool
117
	{
118
		return $this->viewType === self::VIEW_TABLET;
119
	}
120
121
	/**
122
	 * Is the device a phone view type
123
	 *
124
	 * @return bool
125
	 */
126
	public function isPhoneView() : bool
127
	{
128
		return $this->viewType === self::VIEW_PHONE;
129
	}
130
131
	/**
132
	 * Is the device a mobile view type
133
	 *
134
	 * @return bool
135
	 */
136
	public function isMobileView() : bool
137
	{
138 1
		return $this->viewType === self::VIEW_MOBILE || $this->isPhoneView() || $this->isTabletView();
139
	}
140
141
	/**
142
	 * Is not the device a mobile view type (PC, Mac, etc.)
143
	 *
144
	 * @return bool
145
	 */
146
	public function isNotMobileView() : bool
147
	{
148
		return $this->viewType === self::VIEW_NOT_MOBILE;
149
	}
150
151
	/**
152
	 * Sets the tablet view type
153
	 *
154
	 * @return void
155
	 */
156
	public function setTabletView() : void
157
	{
158 1
		$this->viewType = self::VIEW_TABLET;
159 1
	}
160
161
	/**
162
	 * Sets the phone view type
163
	 *
164
	 * @return void
165
	 */
166
	public function setPhoneView() : void
167
	{
168
		$this->viewType = self::VIEW_PHONE;
169
	}
170
171
	/**
172
	 * Sets the mobile view type
173
	 *
174
	 * @return void
175
	 */
176
	public function setMobileView() : void
177
	{
178
		$this->viewType = self::VIEW_MOBILE;
179
	}
180
181
	/**
182
	 * Sets the not mobile view type
183
	 *
184
	 * @return void
185
	 */
186
	public function setNotMobileView() : void
187
	{
188
		$this->viewType = self::VIEW_NOT_MOBILE;
189
	}
190
191
	/**
192
	 * @return string
193
	 */
194
	public function getSwitchParameterName() : string
195
	{
196
		return $this->switchParameterName;
197
	}
198
199
	/**
200
	 * Gets the switch param value from the query string (GET header)
201
	 *
202
	 * @return string
203
	 */
204
	public function getSwitchParameterValue() : string
205
	{
206 1
		return $this->httpRequest->getQuery($this->switchParameterName) ?? self::VIEW_FULL;
207
	}
208
209
	/**
210
	 * Has the Request the switch param in the query string (GET header)
211
	 *
212
	 * @return bool
213
	 */
214
	public function hasSwitchParameter() : bool
215
	{
216 1
		return ($this->httpRequest->getQuery($this->switchParameterName) ?? false) ? true : false;
217
	}
218
219
	/**
220
	 * Gets the RedirectResponse by switch param value
221
	 *
222
	 * @param string $redirectUrl
223
	 *
224
	 * @return Application\Responses\RedirectResponse
225
	 */
226
	public function getRedirectResponseBySwitchParam(string $redirectUrl) : Application\Responses\RedirectResponse
227
	{
228
		$statusCode = 302;
229
230
		switch ($this->getSwitchParameterValue()) {
231
			case self::VIEW_MOBILE:
232
				$this->createCookie(self::VIEW_MOBILE);
233
				break;
234
235
			case self::VIEW_PHONE:
236
				$this->createCookie(self::VIEW_PHONE);
237
				break;
238
239
			case self::VIEW_TABLET:
240
				$this->createCookie(self::VIEW_TABLET);
241
				break;
242
243
			default:
244
				$this->createCookie(self::VIEW_FULL);
245
				break;
246
		}
247
248
		return new Application\Responses\RedirectResponse($redirectUrl, $statusCode);
249
	}
250
251
	/**
252
	 * Modifies the Response for non-mobile devices
253
	 *
254
	 * @return Http\IResponse
255
	 */
256
	public function modifyNotMobileResponse() : Http\IResponse
257
	{
258
		// Create cookie
259
		$this->createCookie(self::VIEW_NOT_MOBILE);
260
261
		return $this->httpResponse;
262
	}
263
264
	/**
265
	 * Modifies the Response for tablet devices
266
	 *
267
	 * @return Http\IResponse
268
	 */
269
	public function modifyTabletResponse() : Http\IResponse
270
	{
271
		// Create cookie
272
		$this->createCookie(self::VIEW_TABLET);
273
274
		return $this->httpResponse;
275
	}
276
277
	/**
278
	 * Modifies the Response for phone devices
279
	 *
280
	 * @return Http\IResponse
281
	 */
282
	public function modifyPhoneResponse() : Http\IResponse
283
	{
284
		// Create cookie
285
		$this->createCookie(self::VIEW_PHONE);
286
287
		return $this->httpResponse;
288
	}
289
290
	/**
291
	 * Modifies the Response for mobile devices
292
	 *
293
	 * @return Http\IResponse
294
	 */
295
	public function modifyMobileResponse() : Http\IResponse
296
	{
297
		// Create cookie
298
		$this->createCookie(self::VIEW_MOBILE);
299
300
		return $this->httpResponse;
301
	}
302
303
	/**
304
	 * Gets the RedirectResponse for phone devices
305
	 *
306
	 * @param string $host    Uri host
307
	 * @param int $statusCode Status code
308
	 *
309
	 * @return Application\Responses\RedirectResponse
310
	 */
311
	public function getPhoneRedirectResponse(string $host, int $statusCode) : Application\Responses\RedirectResponse
312
	{
313
		// Create cookie
314
		$this->createCookie(self::VIEW_PHONE);
315
316
		return new Application\Responses\RedirectResponse($host, $statusCode);
317
	}
318
319
	/**
320
	 * Gets the RedirectResponse for tablet devices
321
	 *
322
	 * @param string $host    Uri host
323
	 * @param int $statusCode Status code
324
	 *
325
	 * @return Application\Responses\RedirectResponse
326
	 */
327
	public function getTabletRedirectResponse(string $host, int $statusCode) : Application\Responses\RedirectResponse
328
	{
329
		// Create cookie
330
		$this->createCookie(self::VIEW_TABLET);
331
332
		return new Application\Responses\RedirectResponse($host, $statusCode);
333
	}
334
335
	/**
336
	 * Gets the RedirectResponse for mobile devices
337
	 *
338
	 * @param string $host    Uri host
339
	 * @param int $statusCode Status code
340
	 *
341
	 * @return Application\Responses\RedirectResponse
342
	 */
343
	public function getMobileRedirectResponse(string $host, int $statusCode) : Application\Responses\RedirectResponse
344
	{
345
		// Create cookie
346
		$this->createCookie(self::VIEW_MOBILE);
347
348
		return new Application\Responses\RedirectResponse($host, $statusCode);
349
	}
350
351
	/**
352
	 * Gets the cookie
353
	 *
354
	 * @param string $cookieValue
355
	 *
356
	 * @return void
357
	 */
358
	private function createCookie(string $cookieValue) : void
359
	{
360
		// Store cookie in response
361
		$this->httpResponse->setCookie(
362
			$this->cookieSettings->getName(),
0 ignored issues
show
Bug introduced by
Consider using $this->cookieSettings->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
363
			$cookieValue,
364
			$this->cookieSettings->getExpiresTime(),
365
			$this->cookieSettings->getPath(),
366
			$this->cookieSettings->getDomain(),
367
			$this->cookieSettings->isSecure(),
368
			$this->cookieSettings->isHttpOnly()
369
		);
370
	}
371
}
372