Completed
Pull Request — master (#1)
by Tomáš
02:50
created

Config::getDefaults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SixtyEightPublishers\Asset\DI;
6
7
use Nette;
8
9
final class Config
10
{
11
	use Nette\SmartObject;
12
13
	private const MAIN_PACKAGE_DEFAULT = [
14
		'base_path' => '',
15
		'base_urls' => [],
16
		'version' => NULL,
17
		'version_format' => '%s?%s',
18
		'version_strategy' => NULL,
19
		'json_manifest_path' => NULL,
20
		'packages' => [],
21
	];
22
23
	private const PACKAGE_DEFAULTS = [
24
		'base_path' => NULL,
25
		'base_urls' => [],
26
		'version' => NULL,
27
		'version_format' => NULL,
28
		'version_strategy' => NULL,
29
		'json_manifest_path' => NULL,
30
	];
31
32
	/**
33
	 * @return array
34
	 */
35
	public function getDefaults(): array
36
	{
37 1
		return self::MAIN_PACKAGE_DEFAULT;
38
	}
39
40
	/**
41
	 * @param \Nette\DI\CompilerExtension $extension
42
	 *
43
	 * @return array
44
	 * @throws \Nette\Utils\AssertionException
45
	 */
46 1
	public function getConfig(Nette\DI\CompilerExtension $extension): array
47
	{
48 1
		$config = $this->mergeConfig($extension, $this->getDefaults(), $extension->getConfig());
49
50 1
		$config = $this->validatePackage($config, TRUE);
51
52 1
		foreach ($config['packages'] as $name => $package) {
53 1
			Nette\Utils\Validators::assert($package, 'array');
54
55 1
			$config['packages'][(string) $name] = $this->validatePackage(
56 1
				$this->mergeConfig($extension, self::PACKAGE_DEFAULTS, $package),
57 1
				FALSE
58
			);
59
		}
60
61 1
		return $config;
62
	}
63
64
	/**
65
	 * @param \Nette\DI\CompilerExtension $extension
66
	 * @param array                       $defaults
67
	 * @param array                       $config
68
	 *
69
	 * @return array
70
	 */
71 1
	private function mergeConfig(Nette\DI\CompilerExtension $extension, array $defaults, array $config): array
72
	{
73
		/** @noinspection PhpInternalEntityUsedInspection */
74 1
		return $extension->validateConfig(
75 1
			Nette\DI\Helpers::expand($defaults, $extension->getContainerBuilder()->parameters),
76 1
			$config
77
		);
78
	}
79
80
	/**
81
	 * @param array $package
82
	 * @param bool  $isDefault
83
	 *
84
	 * @return array
85
	 * @throws \Nette\Utils\AssertionException
86
	 */
87 1
	private function validatePackage(array $package, bool $isDefault): array
88
	{
89 1
		Nette\Utils\Validators::assertField($package, 'version_strategy', 'string|null|' . Nette\DI\Statement::class);
90 1
		Nette\Utils\Validators::assertField($package, 'version', 'string|null');
91 1
		Nette\Utils\Validators::assertField($package, 'version_format', TRUE === $isDefault ? 'string' : 'string|null');
92 1
		Nette\Utils\Validators::assertField($package, 'json_manifest_path', 'string|null');
93 1
		Nette\Utils\Validators::assertField($package, 'base_path', 'string|null');
94 1
		Nette\Utils\Validators::assertField($package, 'base_urls', 'string|string[]');
95
96 1
		if (is_string($package['base_urls'])) {
97 1
			$package['base_urls'] = [ $package['base_urls'] ];
98
		}
99
100 1
		if (isset($package['version_strategy'], $package['version'])) {
101
			throw new \LogicException('You cannot use both "version_strategy" and "version" at the same time under "assets" packages.');
102
		}
103
104 1
		if (isset($package['version_strategy'], $package['json_manifest_path'])) {
105
			throw new \LogicException('You cannot use both "version_strategy" and "json_manifest_path" at the same time under "assets" packages.');
106
		}
107
108 1
		if (isset($package['version'], $package['json_manifest_path'])) {
109
			throw new \LogicException('You cannot use both "version" and "json_manifest_path" at the same time under "assets" packages.');
110
		}
111
112 1
		return $package;
113
	}
114
}
115