Extension::afterCompile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 9
cts 9
cp 1
rs 9.6666
cc 1
eloc 8
nc 1
nop 1
crap 1
1
<?php
2
namespace l10nNetteTranslator\ApplicationDI;
3
4
use Nette\DI\CompilerExtension;
5
use Nette\InvalidStateException;
6
use Nette\PhpGenerator\ClassType;
7
8
class Extension extends CompilerExtension {
9 12
	public function loadConfiguration() {
10 12
		$config = $this->getConfig();
11
12 12
		if (empty($config['languages']) || !is_array($config['languages'])) {
13 9
			throw new InvalidStateException("Languages must be set, must be array and can't be empty");
14
		}
15
16
		/** @var \Nette\DI\ContainerBuilder $builder */
17 3
		$builder = $this->getContainerBuilder();
18
19 3
		$translator = $builder->addDefinition('l10n_nette_translator.translator');
20 3
		$translator->setClass('l10nNetteTranslator\Translator');
21
22 3
		foreach ($config['languages'] as $language) {
23 3
			if (empty($language['lang'])) {
24 1
				throw new InvalidStateException('Key "lang" must be set');
25
			}
26
27 2
			$lang = $builder->literal("new $language[lang]");
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $language instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
28 2
			$plural = isset($language['plural']) ? $builder->literal("new $language[plural]") : $lang;
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $language instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
29 2
			$default = !empty($language['default']);
30
31 2
			$translator->addSetup('addLanguageAndPlural', [$lang, $plural, $default]);
32 2
		}
33
34 2
		if (!empty($config['storage'])) {
35 1
			$translator->addSetup('setStorage', [$config['storage']]);
36 1
		}
37
38 2
		$panel = $builder->addDefinition('l10n_nette_translator.panel');
39 2
		$panel->setClass('l10nNetteTranslator\Panel');
40
41 2
		$processor = $builder->addDefinition('l10n_nette_translator.processor');
42 2
		$processor->setClass('l10nNetteTranslator\TranslatorProcessor');
43 2
	}
44
45 1
	public function afterCompile(ClassType $class) {
46 1
		$initialize = $class->getMethod('initialize');
47 1
		$initialize->addBody('$this->getService("tracy.bar")->addPanel($this->getService("l10n_nette_translator.panel"));');
48 1
		$initialize->addBody('$response = $this->getService("l10n_nette_translator.processor")->run();');
49 1
		$initialize->addBody('if($response instanceof Nette\Application\IResponse) {');
50 1
		$initialize->addBody(' $response->send($this->getByType("Nette\Http\IRequest"), $this->getByType("Nette\Http\IResponse"));');
51 1
		$initialize->addBody(' exit();');
52 1
		$initialize->addBody('}');
53 1
	}
54
}
55