EntryPointLookupProvider::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SixtyEightPublishers\WebpackEncoreBundle\EntryPoint;
6
7
use Nette;
8
use SixtyEightPublishers;
9
10 1
final class EntryPointLookupProvider implements IEntryPointLookupProvider
11
{
12
	use Nette\SmartObject;
13
14
	/** @var \SixtyEightPublishers\WebpackEncoreBundle\EntryPoint\IEntryPointLookup[] */
15
	private $lookups = [];
16
17
	/** @var string|NULL  */
18
	private $defaultName;
19
20
	/**
21
	 * @param \SixtyEightPublishers\WebpackEncoreBundle\EntryPoint\IEntryPointLookup[] $lookups
22
	 * @param string|null                                                              $defaultName
23
	 */
24 1
	public function __construct(array $lookups, ?string $defaultName = NULL)
25
	{
26 1
		foreach ($lookups as $lookup) {
27 1
			$this->add($lookup);
28
		}
29
30 1
		$this->defaultName = $defaultName;
31 1
	}
32
33
	/**
34
	 * @param \SixtyEightPublishers\WebpackEncoreBundle\EntryPoint\IEntryPointLookup $lookup
35
	 *
36
	 * @return void
37
	 */
38 1
	private function add(IEntryPointLookup $lookup): void
39
	{
40 1
		$this->lookups[$lookup->getBuildName()] = $lookup;
41 1
	}
42
43
	/************** interface \SixtyEightPublishers\WebpackEncoreBundle\EntryPoint\IEntryPointLookupProvider **************/
44
45
	/**
46
	 * {@inheritdoc}
47
	 */
48 1
	public function getEntryPointLookup(string $buildName = NULL): IEntryPointLookup
49
	{
50 1
		$buildName = $buildName ?? $this->defaultName;
51
52 1
		if (NULL === $buildName) {
53 1
			throw new SixtyEightPublishers\WebpackEncoreBundle\Exception\EntryPointNotFoundException('There is no default build configured: please pass an argument to getEntryPointLookup().');
54
		}
55
56 1
		if (!isset($this->lookups[$buildName])) {
57 1
			throw new SixtyEightPublishers\WebpackEncoreBundle\Exception\EntryPointNotFoundException(sprintf(
58 1
				'The build "%s" is not configured.',
59 1
				$buildName
60
			));
61
		}
62
63 1
		return $this->lookups[$buildName];
64
	}
65
}
66