Completed
Push — master ( 177c94...81f7e0 )
by Adam
02:19
created

Macros::macroIsMobileView()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 2
cp 0
rs 9.4285
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
/**
3
 * Macros.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     Latte
10
 * @since          1.0.0
11
 *
12
 * @date           22.04.14
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\MobileDetect\Latte;
18
19
use Nette;
20
21
use Latte;
22
use Latte\Compiler;
23
use Latte\MacroNode;
24
use Latte\PhpWriter;
25
use Latte\Macros\MacroSet;
26
27
use IPub;
28
use IPub\MobileDetect\Exceptions;
29
30
/**
31
 * Mobile detect latte macros definition
32
 *
33
 * @package        iPublikuj:MobileDetect!
34
 * @subpackage     Latte
35
 *
36
 * @author         Adam Kadlec <[email protected]>
37
 */
38 1
final class Macros extends MacroSet
39
{
40
	/**
41
	 * Register latte macros
42
	 *
43
	 * @param Compiler $compiler
44
	 *
45
	 * @return static
46
	 */
47
	public static function install(Compiler $compiler)
48
	{
49 1
		$me = new static($compiler);
50
51
		/**
52
		 * {isMobile /}, {isNotMobile /}
53
		 */
54 1
		$me->addMacro('isMobile', [$me, 'macroIsMobile'], '}');
55 1
		$me->addMacro('isNotMobile', [$me, 'macroIsNotMobile'], '}');
56
57
		/**
58
		 * {isPhone /}, {isNotPhone /}
59
		 */
60 1
		$me->addMacro('isPhone', [$me, 'macroIsPhone'], '}');
61 1
		$me->addMacro('isNotPhone', [$me, 'macroIsNotPhone'], '}');
62
63
		/**
64
		 * {isTablet /}, {isNotTablet /}
65
		 */
66 1
		$me->addMacro('isTablet', [$me, 'macroIsTablet'], '}');
67 1
		$me->addMacro('isNotTablet', [$me, 'macroIsNotTablet'], '}');
68
69
		/**
70
		 * {isMobileDevice 'device_name'}
71
		 */
72 1
		$me->addMacro('isMobileDevice', [$me, 'macroIsDevice'], '}');
73
74
		/**
75
		 * {isMobileOs 'device_name'}
76
		 */
77 1
		$me->addMacro('isMobileOs', [$me, 'macroIsOS'], '}');
78
79
		/**
80
		 * {isFullView /}, {isMobileView /}, {isTabletView /}, {isNotMobileView /}
81
		 */
82 1
		$me->addMacro('isFullView', [$me, 'macroIsFullView'], '}');
83 1
		$me->addMacro('isMobileView', [$me, 'macroIsMobileView'], '}');
84 1
		$me->addMacro('isPhoneView', [$me, 'macroIsPhoneView'], '}');
85 1
		$me->addMacro('isTabletView', [$me, 'macroIsTabletView'], '}');
86 1
		$me->addMacro('isNotMobileView', [$me, 'macroIsNotMobileView'], '}');
87
88 1
		return $me;
89
	}
90
91
	/**
92
	 * {isMobile /}
93
	 *
94
	 * @param MacroNode $node
95
	 * @param PhpWriter $writer
96
	 *
97
	 * @return string
98
	 */
99
	public function macroIsMobile(MacroNode $node, PhpWriter $writer) : string
100
	{
101 1
		return $writer->write('
102
			$_resultMD = property_exists($this, "filters") ? %escape(call_user_func($this->filters->isMobile)) : $template->getMobileDetectService()->isMobile() && !$template->getMobileDetectService()->isTablet();
103
			if ($_resultMD) {
104 1
			');
105
	}
106
107
	/**
108
	 * {isNotMobile /}
109
	 *
110
	 * @param MacroNode $node
111
	 * @param PhpWriter $writer
112
	 *
113
	 * @return string
114
	 */
115
	public function macroIsNotMobile(MacroNode $node, PhpWriter $writer) : string
116
	{
117
		return $writer->write('
118
			$_resultMD = property_exists($this, "filters") ? %escape(!call_user_func($this->filters->isMobile)) : !$template->getMobileDetectService()->isMobile();
119
			if ($_resultMD) {
120
			');
121
	}
122
123
	/**
124
	 * {isPhone /}
125
	 *
126
	 * @param MacroNode $node
127
	 * @param PhpWriter $writer
128
	 *
129
	 * @return string
130
	 */
131
	public function macroIsPhone(MacroNode $node, PhpWriter $writer) : string
132
	{
133 1
		return $writer->write('
134
			$_resultMD = property_exists($this, "filters") ? %escape(call_user_func($this->filters->isMobile) && !call_user_func($this->filters->isTablet)) : $template->getMobileDetectService()->isMobile() && !$template->getMobileDetectService()->isTablet();
135
			if ($_resultMD) {
136 1
			');
137
	}
138
139
	/**
140
	 * {isNotPhone /}
141
	 *
142
	 * @param MacroNode $node
143
	 * @param PhpWriter $writer
144
	 *
145
	 * @return string
146
	 */
147
	public function macroIsNotPhone(MacroNode $node, PhpWriter $writer) : string
148
	{
149
		return $writer->write('
150
			$_resultMD = property_exists($this, "filters") ? %escape((call_user_func($this->filters->isMobile) && call_user_func($this->filters->isTablet)) || !call_user_func($this->filters->isMobile)) : ($template->getMobileDetectService()->isMobile() && $template->getMobileDetectService()->isTablet()) || !$template->getMobileDetectService()->isMobile();
151
			if ($_resultMD) {
152
			');
153
	}
154
155
	/**
156
	 * {isTablet /}
157
	 *
158
	 * @param MacroNode $node
159
	 * @param PhpWriter $writer
160
	 *
161
	 * @return string
162
	 */
163
	public function macroIsTablet(MacroNode $node, PhpWriter $writer) : string
164
	{
165 1
		return $writer->write('
166
			$_resultMD = property_exists($this, "filters") ? %escape(call_user_func($this->filters->isTablet)) : $template->getMobileDetectService()->isTablet();
167
			if ($_resultMD) {
168 1
			');
169
	}
170
171
	/**
172
	 * {isNotTablet /}
173
	 *
174
	 * @param MacroNode $node
175
	 * @param PhpWriter $writer
176
	 *
177
	 * @return string
178
	 */
179
	public function macroIsNotTablet(MacroNode $node, PhpWriter $writer) : string
180
	{
181
		return $writer->write('
182
			$_resultMD = property_exists($this, "filters") ? %escape(!call_user_func($this->filters->isTablet)) : !$template->getMobileDetectService()->isTablet();
183
			if ($_resultMD) {
184
			');
185
	}
186
187
	/**
188
	 * @param MacroNode $node
189
	 * @param PhpWriter $writer
190
	 *
191
	 * @return string
192
	 *
193
	 * @throws Exceptions\CompileException
194
	 */
195 View Code Duplication
	public function macroIsDevice(MacroNode $node, PhpWriter $writer) : string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
196
	{
197 1
		$arguments = self::prepareMacroArguments($node->args);
198
199 1
		if ($arguments['device'] === NULL) {
200
			throw new Exceptions\CompileException('Please provide device name.');
201
		}
202
203
		// Create magic method name
204 1
		$magicMethodName = 'is' . ucfirst(strtolower((string) $arguments["device"]));
205
206 1
		return $writer->write('
207 1
			$_resultMD = property_exists($this, "filters") ? %escape(call_user_func($this->filters->isDevice, "' . $arguments['device'] . '")) : $template->getMobileDetectService()->'. $magicMethodName.'();
208
			if ($_resultMD) {
209 1
			');
210
	}
211
212
	/**
213
	 * @param MacroNode $node
214
	 * @param PhpWriter $writer
215
	 *
216
	 * @return string
217
	 *
218
	 * @throws Exceptions\CompileException
219
	 */
220 View Code Duplication
	public function macroIsOS(MacroNode $node, PhpWriter $writer) : string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
221
	{
222 1
		$arguments = self::prepareMacroArguments($node->args);
223
224 1
		if ($arguments['os'] === NULL) {
225
			throw new Exceptions\CompileException('Please provide OS name.');
226
		}
227
228
		// Create magic method name
229 1
		$magicMethodName = 'is' . ucfirst(strtolower((string) $arguments["os"]));
230
231 1
		return $writer->write('
232 1
			$_resultMD = property_exists($this, "filters") ? %escape(call_user_func($this->filters->isOs, "' . $arguments['os'] . '")) : $template->getMobileDetectService()->'. $magicMethodName.'();
233
			if ($_resultMD) {
234 1
			');
235
	}
236
237
	/**
238
	 * {isFullView /}
239
	 *
240
	 * @param MacroNode $node
241
	 * @param PhpWriter $writer
242
	 *
243
	 * @return string
244
	 */
245
	public function macroIsFullView(MacroNode $node, PhpWriter $writer) : string
246
	{
247
		return $writer->write('
248
			$_resultMD = property_exists($this, "filters") ? %escape(!call_user_func($this->filters->isFullView)) : $template->getDeviceViewService()->isFullView();
249
			if ($_resultMD) {
250
			');
251
	}
252
253
	/**
254
	 * {isMobileView /}
255
	 *
256
	 * @param MacroNode $node
257
	 * @param PhpWriter $writer
258
	 *
259
	 * @return string
260
	 */
261
	public function macroIsMobileView(MacroNode $node, PhpWriter $writer) : string
262
	{
263
		return $writer->write('
264
			$_resultMD = property_exists($this, "filters") ? %escape(!call_user_func($this->filters->isMobileView)) : $template->getDeviceViewService()->isMobileView();
265
			if ($_resultMD) {
266
			');
267
	}
268
269
	/**
270
	 * {isPhoneView /}
271
	 *
272
	 * @param MacroNode $node
273
	 * @param PhpWriter $writer
274
	 *
275
	 * @return string
276
	 */
277
	public function macroIsPhoneView(MacroNode $node, PhpWriter $writer) : string
278
	{
279
		return $writer->write('
280
			$_resultMD = property_exists($this, "filters") ? %escape(!call_user_func($this->filters->isPhoneView)) : $template->getDeviceViewService()->isPhoneView();
281
			if ($_resultMD) {
282
			');
283
	}
284
285
	/**
286
	 * {isTabletView /}
287
	 *
288
	 * @param MacroNode $node
289
	 * @param PhpWriter $writer
290
	 *
291
	 * @return string
292
	 */
293
	public function macroIsTabletView(MacroNode $node, PhpWriter $writer) : string
294
	{
295
		return $writer->write('
296
			$_resultMD = property_exists($this, "filters") ? %escape(!call_user_func($this->filters->isTabletView)) : $template->getDeviceViewService()->isTabletView();
297
			if ($_resultMD) {
298
			');
299
	}
300
301
	/**
302
	 * {isNotMobileView /}
303
	 *
304
	 * @param MacroNode $node
305
	 * @param PhpWriter $writer
306
	 *
307
	 * @return string
308
	 */
309
	public function macroIsNotMobileView(MacroNode $node, PhpWriter $writer) : string
310
	{
311
		return $writer->write('
312
			$_resultMD = property_exists($this, "filters") ? %escape(!call_user_func($this->filters->isNotMobileView)) : $template->getDeviceViewService()->isNotMobileView();
313
			if ($_resultMD) {
314
			');
315
	}
316
317
	/**
318
	 * @param string $macro
319
	 *
320
	 * @return array
321
	 */
322
	public static function prepareMacroArguments($macro) : array
323
	{
324 1
		$arguments = array_map(function ($value) {
325 1
			return trim($value);
326 1
		}, explode(',', $macro));
327
328 1
		$device = $os = $arguments[0];
329
330
		return [
331 1
			'device' => $device,
332 1
			'os'     => $os,
333
		];
334
	}
335
}
336