Completed
Push — master ( 121335...68b8eb )
by Michal
03:32
created

Extension   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 3
c 2
b 1
f 0
lcom 1
cbo 1
dl 0
loc 55
ccs 19
cts 19
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A beforeCompile() 0 16 1
A validateChoices() 0 11 2
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
		$choices = ['exception', 'notice', 'ignore'];
35 1
		$this->validateChoices('missingAsset', $choices);
36 1
		$this->validateChoices('missingVersion', $choices);
37
38 1
		$builder = $this->getContainerBuilder();
39 1
		$builder->getDefinition('latte.latteFactory')
40 1
			->addSetup("?->addProvider(?, ?)", ['@self', AssetMacro::CONFIG_PROVIDER, $config])
41 1
			->addSetup("?->onCompile[] = function(\$engine) { " .
42 1
				AssetMacro::class . "::install(\$engine->getCompiler()); }",
43 1
				['@self']
44
			);
45 1
	}
46
47
48
	/**
49
	 * @param string $key
50
	 * @param array $choices
51
	 */
52 1
	private function validateChoices($key, array $choices)
53
	{
54 1
		if ( ! in_array($this->config[$key], $choices)) {
55 1
			throw new UnexpectedValueException(sprintf(
56 1
				"Unexpected value '%s' of '%s' configuration key. Allowed values: %s.",
57 1
				$this->config[$key],
58 1
				$this->prefix($key),
59 1
				implode(', ', $choices)
60
			));
61
		}
62 1
	}
63
64
}
65