Completed
Push — master ( 69eb71...121335 )
by Michal
02:13
created

Extension::beforeCompile()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 30
Code Lines 21

Duplication

Lines 16
Ratio 53.33 %

Code Coverage

Tests 21
CRAP Score 3

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 16
loc 30
ccs 21
cts 21
cp 1
rs 8.8571
cc 3
eloc 21
nc 3
nop 0
crap 3
1
<?php
2
3
namespace Webrouse\AssetMacro\DI;
4
5
use Nette\DI\CompilerExtension;
6
use Webrouse\AssetMacro\AssetMacro;
7
use Webrouse\AssetMacro\Exceptions\UnexpectedValueException;
8
9
10
class Extension extends CompilerExtension
11
{
12
13
	/**
14
	 * Default configuration
15
	 * @var array
16
	 */
17
	public $defaults = [
18
		'wwwDir' => '%wwwDir%',
19
		'versions' => AssetMacro::VERSIONS_AUTODETECT,
20
		'autodetectPaths' => [
21
			'busters.json',
22
			'versions.json',
23
			'rev-manifest.json',
24
		],
25
		'missingAsset' => 'exception', // exception, notice, or ignore
26
		'missingVersion' => 'ignore',  // exception, notice, or ignore
27
	];
28
29
30
	public function beforeCompile()
31
	{
32 1
		$config = $this->getContainerBuilder()->expand($this->validateConfig($this->defaults));
33
34 1
		$errorHandling = ['exception', 'notice', 'ignore'];
35 1 View Code Duplication
		if ( ! in_array($config['missingAsset'], $errorHandling)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
36 1
			throw new UnexpectedValueException(sprintf(
37 1
				"Unexpected value '%s' of '%s' configuration key. Allowed values: %s.",
38 1
				$config['missingAsset'],
39 1
				$this->prefix('missingAsset'),
40 1
				implode(', ', $errorHandling)
41
			));
42
		}
43 1 View Code Duplication
		if ( ! in_array($config['missingVersion'], $errorHandling)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
44 1
			throw new UnexpectedValueException(sprintf(
45 1
				"Unexpected value '%s' of '%s' configuration key. Allowed values: %s.",
46 1
				$config['missingVersion'],
47 1
				$this->prefix('missingVersion'),
48 1
				implode(', ', $errorHandling)
49
			));
50
		}
51
52 1
		$builder = $this->getContainerBuilder();
53 1
		$builder->getDefinition('latte.latteFactory')
54 1
			->addSetup("?->addProvider(?, ?)", ['@self', AssetMacro::CONFIG_PROVIDER, $config])
55 1
			->addSetup("?->onCompile[] = function(\$engine) { " .
56 1
				AssetMacro::class . "::install(\$engine->getCompiler()); }",
57 1
				['@self']
58
			);
59 1
	}
60
61
}
62