Passed
Push — master ( 9edc41...f34eb1 )
by Adam
03:33
created

DeviceView   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 341
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 25.76%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 31
c 2
b 0
f 0
lcom 2
cbo 5
dl 0
loc 341
ccs 17
cts 66
cp 0.2576
rs 9.8

23 Methods

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