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

MobileDetectExtension   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 94.55%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 1
cbo 5
dl 0
loc 138
ccs 52
cts 55
cp 0.9455
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A loadConfiguration() 0 50 3
B beforeCompile() 0 24 2
A register() 0 6 1
1
<?php
2
/**
3
 * MobileDetectExtension.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     DI
10
 * @since          1.0.0
11
 *
12
 * @date           21.04.14
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\MobileDetect\DI;
18
19
use Nette;
20
use Nette\Bridges;
21
use Nette\DI;
22
use Nette\PhpGenerator as Code;
23
24
use IPub;
25
use IPub\MobileDetect;
26
use IPub\MobileDetect\Events;
27
use IPub\MobileDetect\Helpers;
28
use IPub\MobileDetect\Templating;
29
30
/**
31
 * Mobile device detect extension container
32
 *
33
 * @package        iPublikuj:MobileDetect!
34
 * @subpackage     DI
35
 *
36
 * @author         Adam Kadlec <[email protected]>
37
 */
38 1
final class MobileDetectExtension extends DI\CompilerExtension
39
{
40
	/**
41
	 * @var array
42
	 */
43
	private $defaults = [
44
		'redirect'         => [
45
			'mobile'               => [
46
				'isEnabled'  => FALSE,
47
				'host'       => NULL,
48
				'statusCode' => 301,
49
				'action'     => 'noRedirect',    // redirect/noRedirect/redirectWithoutPath
50
			],
51
			'phone'                => [
52
				'isEnabled'  => FALSE,
53
				'host'       => NULL,
54
				'statusCode' => 301,
55
				'action'     => 'noRedirect',    // redirect/noRedirect/redirectWithoutPath
56
			],
57
			'tablet'               => [
58
				'isEnabled'  => FALSE,
59
				'host'       => NULL,
60
				'statusCode' => 301,
61
				'action'     => 'noRedirect',    // redirect/noRedirect/redirectWithoutPath
62
			],
63
			'detectPhoneAsMobile'  => FALSE,
64
			'detectTabletAsMobile' => FALSE,
65
		],
66
		'switchDeviceView' => [
67
			'saveRefererPath' => TRUE
68
		],
69
		'switchParameterName' => 'device_view',
70
		'deviceViewCookie' => [
71
			'name'        => 'device_view',
72
			'domain'      => NULL,
73
			'expireAfter' => '+1 month',
74
			'path'        => '/',
75
			'secure'      => FALSE,
76
			'httpOnly'    => TRUE,
77
		],
78
		'debugger' => '%debugMode%'
79
	];
80
81
	/**
82
	 * @return void
83
	 */
84
	public function loadConfiguration() : void
85
	{
86
		// Get container builder
87 1
		$builder = $this->getContainerBuilder();
88
		// Get extension configuration
89 1
		$configuration = $this->getConfig($this->defaults);
90
91
		// Install mobile detect service
92 1
		$mobileDetect = $builder->addDefinition($this->prefix('mobileDetect'))
0 ignored issues
show
Deprecated Code introduced by
The method Nette\DI\ServiceDefinition::setClass() has been deprecated.

This method has been deprecated.

Loading history...
93 1
			->setClass(MobileDetect\MobileDetect::class);
94
95 1
		$builder->addDefinition($this->prefix('deviceView'))
0 ignored issues
show
Deprecated Code introduced by
The method Nette\DI\ServiceDefinition::setClass() has been deprecated.

This method has been deprecated.

Loading history...
96 1
			->setClass(Helpers\DeviceView::class)
97 1
			->setArguments(['setSwitchParameterName' => $configuration['switchParameterName']]);
98
99 1
		$builder->addDefinition($this->prefix('cookieSettings'))
0 ignored issues
show
Deprecated Code introduced by
The method Nette\DI\ServiceDefinition::setClass() has been deprecated.

This method has been deprecated.

Loading history...
100 1
			->setClass(Helpers\CookieSettings::class)
101 1
			->setArguments([
102 1
				'name'        => $configuration['deviceViewCookie']['name'],
103 1
				'domain'      => $configuration['deviceViewCookie']['domain'],
104 1
				'expireAfter' => $configuration['deviceViewCookie']['expireAfter'],
105 1
				'path'        => $configuration['deviceViewCookie']['path'],
106 1
				'secure'      => $configuration['deviceViewCookie']['secure'],
107 1
				'httpOnly'    => $configuration['deviceViewCookie']['httpOnly'],
108
			]);
109
110 1
		if ($configuration['debugger'] && interface_exists('Tracy\IBarPanel')) {
111
			$builder->addDefinition($this->prefix('panel'))
0 ignored issues
show
Deprecated Code introduced by
The method Nette\DI\ServiceDefinition::setClass() has been deprecated.

This method has been deprecated.

Loading history...
112
				->setClass('IPub\MobileDetect\Diagnostics\Panel');
113
114
			$mobileDetect->addSetup('?->register(?)', [$this->prefix('@panel'), '@self']);
115
		}
116
117 1
		$builder->addDefinition($this->prefix('onRequestHandler'))
0 ignored issues
show
Deprecated Code introduced by
The method Nette\DI\ServiceDefinition::setClass() has been deprecated.

This method has been deprecated.

Loading history...
118 1
			->setClass(Events\OnRequestHandler::class)
119 1
			->addSetup('$redirectConf', [$configuration['redirect']])
120 1
			->addSetup('$isFullPath', [$configuration['switchDeviceView']['saveRefererPath']]);
121
122 1
		$builder->addDefinition($this->prefix('onResponseHandler'))
0 ignored issues
show
Deprecated Code introduced by
The method Nette\DI\ServiceDefinition::setClass() has been deprecated.

This method has been deprecated.

Loading history...
123 1
			->setClass(Events\OnResponseHandler::class);
124
125
		// Register template helpers
126 1
		$builder->addDefinition($this->prefix('helpers'))
0 ignored issues
show
Deprecated Code introduced by
The method Nette\DI\ServiceDefinition::setClass() has been deprecated.

This method has been deprecated.

Loading history...
127 1
			->setClass(Templating\Helpers::class)
128 1
			->setAutowired(FALSE);
129
130 1
		$application = $builder->getDefinition('application');
131 1
		$application->addSetup('$service->onRequest[] = ?', ['@' . $this->prefix('onRequestHandler')]);
132 1
		$application->addSetup('$service->onResponse[] = ?', ['@' . $this->prefix('onResponseHandler')]);
133 1
	}
134
135
	/**
136
	 * {@inheritdoc}
137
	 */
138
	public function beforeCompile() : void
139
	{
140 1
		parent::beforeCompile();
141
142 1
		$builder = $this->getContainerBuilder();
143
144
		// Install extension latte macros
145 1
		$latteFactory = $builder->getDefinition($builder->getByType(Bridges\ApplicationLatte\ILatteFactory::class) ?: 'nette.latteFactory');
146
147
		$latteFactory
148 1
			->addSetup('IPub\MobileDetect\Latte\Macros::install(?->getCompiler())', ['@self'])
149 1
			->addSetup('addFilter', ['isMobile', [$this->prefix('@helpers'), 'isMobile']])
150 1
			->addSetup('addFilter', ['isPhone', [$this->prefix('@helpers'), 'isPhone']])
151 1
			->addSetup('addFilter', ['isTablet', [$this->prefix('@helpers'), 'isTablet']])
152 1
			->addSetup('addFilter', ['isDevice', [$this->prefix('@helpers'), 'isDevice']])
153 1
			->addSetup('addFilter', ['isOs', [$this->prefix('@helpers'), 'isOs']])
154 1
			->addSetup('addFilter', ['isFullView', [$this->prefix('@helpers'), 'isFullView']])
155 1
			->addSetup('addFilter', ['isMobileView', [$this->prefix('@helpers'), 'isMobileView']])
156 1
			->addSetup('addFilter', ['isPhoneView', [$this->prefix('@helpers'), 'isPhoneView']])
157 1
			->addSetup('addFilter', ['isTabletView', [$this->prefix('@helpers'), 'isTabletView']])
158 1
			->addSetup('addFilter', ['isNotMobileView', [$this->prefix('@helpers'), 'isNotMobileView']])
159 1
			->addSetup('addFilter', ['getMobileDetectService', [$this->prefix('@helpers'), 'getMobileDetectService']])
160 1
			->addSetup('addFilter', ['getDeviceViewService', [$this->prefix('@helpers'), 'getDeviceViewService']]);
161 1
	}
162
163
	/**
164
	 * @param Nette\Configurator $config
165
	 * @param string $extensionName
166
	 *
167
	 * @return void
168
	 */
169
	public static function register(Nette\Configurator $config, string $extensionName = 'mobileDetect')
170
	{
171 1
		$config->onCompile[] = function (Nette\Configurator $config, Nette\DI\Compiler $compiler) use ($extensionName) : void {
172 1
			$compiler->addExtension($extensionName, new MobileDetectExtension());
173 1
		};
174 1
	}
175
}
176