Completed
Push — master ( 84255c...a7d7e4 )
by Thomas
14:38
created

ServiceContainer::getTwig()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
rs 8.8571
ccs 0
cts 0
cp 0
cc 2
eloc 14
nc 2
nop 0
crap 6
1
<?php
2
namespace keeko\core\service;
3
4
use \Twig_Environment;
5
use \Twig_SimpleFunction;
6
use keeko\core\auth\AuthManager;
7
use keeko\core\kernel\AbstractKernel;
8
use keeko\core\package\ModuleManager;
9
use keeko\core\package\PackageManager;
10
use keeko\core\preferences\PreferenceLoader;
11
use keeko\core\security\Firewall;
12
use Puli\Repository\Api\ResourceRepository;
13
use Puli\TwigExtension\PuliExtension;
14
use Puli\TwigExtension\PuliTemplateLoader;
15
use Symfony\Component\EventDispatcher\EventDispatcher;
16
use Symfony\Component\Translation\Loader\JsonFileLoader;
17
18
class ServiceContainer {
19
20
	/** @var PackageManager */
21
	private $packageManager;
22
	
23
	/** @var ModuleManager */
24
	private $moduleManager;
25
	
26
	/** @var AuthManager */
27
	private $authManager;
28
	
29
	/** @var PreferenceLoader */
30
	private $preferenceLoader;
31
	
32
	/** @var Firewall */
33
	private $firewall;
34
	
35
	/** @var KeekoTranslator */
36
	private $translator;
37
	
38
	/** @var ResourceRepository */
39
	private $puli;
40
	
41
	/** @var EventDispatcher */
42
	private $dispatcher;
43
	
44
	/** @var AbstractKernel */
45
	private $kernel;
46
	
47
	/** @var Twig_Environment */
48
	private $twig;
49
	
50
	
51
	public function __construct(AbstractKernel $kernel) {
52
		$this->kernel = $kernel;
53
	}
54
	
55
	/**
56
	 * Returns the kernel
57
	 *
58
	 * @return AbstractKernel
59
	 */
60
	public function getKernel() {
61
		return $this->kernel;
62
	}
63
	
64
	/**
65
	 * Returns the event dispatcher
66
	 *
67
	 * @return EventDispatcher
68
	 */
69
	public function getDispatcher() {
70
		if ($this->dispatcher === null) {
71
			$this->dispatcher = new EventDispatcher();
72
		}
73
		
74
		return $this->dispatcher;
75
	}
76
	
77
	/**
78
	 * Returns the package manager
79
	 *
80
	 * @return PackageManager
81
	 */
82
	public function getPackageManager() {
83
		if ($this->packageManager === null) {
84
			$this->packageManager = new PackageManager($this);
85
		}
86
		
87
		return $this->packageManager;
88
	}
89
	
90
	/**
91
	 * Returns the module manager
92
	 *
93
	 * @return ModuleManager
94
	 */
95
	public function getModuleManager() {
96
		if ($this->moduleManager === null) {
97
			$this->moduleManager = new ModuleManager($this);
98
		}
99
		
100
		return $this->moduleManager;
101
	}
102
	
103
	/**
104
	 * Returns the auth manager
105
	 *
106
	 * @return AuthManager
107
	 */
108
	public function getAuthManager() {
109
		if ($this->authManager === null) {
110
			$this->authManager = new AuthManager();
111
		}
112
	
113
		return $this->authManager;
114
	}
115
	
116
	/**
117
	 * Returns the preference loader
118
	 *
119
	 * @return PreferenceLoader
120
	 */
121
	public function getPreferenceLoader() {
122
		if ($this->preferenceLoader === null) {
123
			$this->preferenceLoader = new PreferenceLoader();
124
		}
125
		
126
		return $this->preferenceLoader;
127
	}
128
	
129
	/**
130
	 * Returns the firewall
131
	 *
132
	 * @return Firewall
133
	 */
134
	public function getFirewall() {
135
		if ($this->firewall === null) {
136
			$this->firewall = new Firewall($this);
137
		}
138
		
139
		return $this->firewall;
140
	}
141
	
142
	/**
143
	 * Returns the keeko translation service
144
	 *
145
	 * @return KeekoTranslator
146
	 */
147
	public function getTranslator() {
148
		// TODO: how to get the language
149
		if ($this->translator === null) {
150
			$app = $this->getKernel()->getApplication();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 14 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
151
			$lang = $app->getLocalization()->getLanguage()->getAlpha2();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 13 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
152
			$this->translator = new KeekoTranslator($this, $lang);
153
			$this->translator->addLoader('json', new JsonFileLoader());
154
			$this->translator->setFallbackLocales(['en']);
155
		}
156
		
157
		return $this->translator;
158
	}
159
	
160
	/**
161
	 * Returns an instance to the puli repository
162
	 *
163
	 * @return ResourceRepository
164
	 */
165
	public function getResourceRepository() {
166
		if ($this->puli === null) {
167
			$factoryClass = PULI_FACTORY_CLASS;
168
			$factory = new $factoryClass();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
169
			
170
			$this->puli = $factory->createRepository();
171
		}
172
		
173
		return $this->puli;
174
	}
175
176
	/**
177
	 * Returns twig
178
	 *
179
	 * @return Twig_Environment
180
	 */
181
	public function getTwig() {
182
		if ($this->twig === null) {
183
			$repo = $this->getResourceRepository();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
184
			$loader = new PuliTemplateLoader($repo);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
185
			$this->twig = new Twig_Environment($loader);
186
				
187
			// puli extension
188
			$this->twig->addExtension(new PuliExtension($repo));
189
	
190
			// translator function
191
			$translator = $this->getServiceContainer()->getTranslator();
0 ignored issues
show
Bug introduced by
The method getServiceContainer() does not seem to exist on object<keeko\core\service\ServiceContainer>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
192
			$trans = function($key, $params = [], $domain = null) use ($translator) {
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
193
				return $translator->trans($key, $params, $domain);
194
			};
195
			$this->twig->addFunction(new Twig_SimpleFunction('t', $trans));
196
		
197
			// firewall
198
			$firewall = $this->getServiceContainer()->getFirewall();
0 ignored issues
show
Bug introduced by
The method getServiceContainer() does not seem to exist on object<keeko\core\service\ServiceContainer>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
199
			$this->twig->addFunction(new Twig_SimpleFunction('hasPermission', function ($module, $action) use ($firewall) {
200
				return $firewall->hasPermission($module, $action);
201
			}));
202
		}
203
		
204
		return $this->twig;
205
	}
206
}