EntryPointLookup   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 52
dl 0
loc 164
ccs 49
cts 49
cp 1
rs 10
c 0
b 0
f 0
wmc 20

9 Methods

Rating   Name   Duplication   Size   Complexity  
B getEntriesData() 0 31 7
A __construct() 0 5 1
A getEntryFiles() 0 12 2
A getJsFiles() 0 3 1
A getBuildName() 0 3 1
A getEntryData() 0 23 4
A getCssFiles() 0 3 1
A getIntegrityData() 0 5 2
A reset() 0 3 1
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 EntryPointLookup implements IEntryPointLookup, IIntegrityDataProvider
11
{
12 1
	use Nette\SmartObject;
13
14
	/** @var string  */
15
	private $buildName;
16
17
	/** @var string  */
18
	private $entryPointJsonPath;
19
20
	/** @var \Nette\Caching\Cache|NULL  */
21
	private $cache;
22
23
	/** @var NULL|array */
24
	private $entriesData;
25
26
	/** @var array  */
27
	private $returnedFiles = [];
28
29
	/**
30
	 * @param string                    $buildName
31
	 * @param string                    $entryPointJsonPath
32
	 * @param \Nette\Caching\Cache|NULL $cache
33
	 */
34
	public function __construct(string $buildName, string $entryPointJsonPath, ?Nette\Caching\Cache $cache = NULL)
35
	{
36 1
		$this->buildName = $buildName;
37 1
		$this->entryPointJsonPath = $entryPointJsonPath;
38 1
		$this->cache = $cache;
39 1
	}
40
41
	/**
42
	 * @param string $entryName
43
	 * @param string $key
44
	 *
45
	 * @return array
46
	 */
47
	private function getEntryFiles(string $entryName, string $key): array
48
	{
49 1
		$entryData = $this->getEntryData($entryName);
50
51 1
		if (!isset($entryData[$key])) {
52 1
			return [];
53
		}
54
55 1
		$newFiles = array_values(array_diff($entryData[$key], $this->returnedFiles));
56 1
		$this->returnedFiles = array_merge($this->returnedFiles, $newFiles);
57
58 1
		return $newFiles;
59
	}
60
61
	/**
62
	 * @param string $entryName
63
	 *
64
	 * @return array
65
	 * @throws \SixtyEightPublishers\WebpackEncoreBundle\Exception\EntryPointNotFoundException
66
	 */
67
	private function getEntryData(string $entryName): array
68
	{
69 1
		$entriesData = $this->getEntriesData();
70
71 1
		if (isset($entriesData['entrypoints'][$entryName])) {
72 1
			return $entriesData['entrypoints'][$entryName];
73
		}
74
75 1
		$withoutExtension = substr($entryName, 0, (int) strrpos($entryName, '.'));
76
77 1
		if (!empty($withoutExtension) && isset($entriesData['entrypoints'][$withoutExtension])) {
78 1
			throw new SixtyEightPublishers\WebpackEncoreBundle\Exception\EntryPointNotFoundException(sprintf(
79 1
				'Could not find the entry "%s". Try "%s" instead (without the extension).',
80 1
				$entryName,
81 1
				$withoutExtension
82
			));
83
		}
84
85 1
		throw new SixtyEightPublishers\WebpackEncoreBundle\Exception\EntryPointNotFoundException(sprintf(
86 1
			'Could not find the entry "%s" in "%s". Found: %s.',
87 1
			$entryName,
88 1
			$this->entryPointJsonPath,
89 1
			implode(', ', array_keys($entriesData['entrypoints']))
90
		));
91
	}
92
93
	/**
94
	 * @return array
95
	 * @throws \SixtyEightPublishers\WebpackEncoreBundle\Exception\InvalidStateException
96
	 */
97
	private function getEntriesData(): array
98
	{
99 1
		if (NULL !== $this->entriesData) {
100 1
			return $this->entriesData;
101
		}
102
103 1
		if (NULL !== $this->cache && is_array($entriesData = $this->cache->load($this->getBuildName()))) {
104 1
			return $this->entriesData = $entriesData;
105
		}
106
107 1
		if (!file_exists($this->entryPointJsonPath)) {
108 1
			throw new SixtyEightPublishers\WebpackEncoreBundle\Exception\InvalidStateException(sprintf(
109 1
				'Could not find the entrypoints file from Webpack: the file "%s" does not exist.',
110 1
				$this->entryPointJsonPath
111
			));
112
		}
113
114 1
		$this->entriesData = Nette\Utils\Json::decode(file_get_contents($this->entryPointJsonPath), Nette\Utils\Json::FORCE_ARRAY);
115
116 1
		if (!isset($this->entriesData['entrypoints'])) {
117 1
			throw new SixtyEightPublishers\WebpackEncoreBundle\Exception\InvalidStateException(sprintf(
118 1
				'Could not find an "entrypoints" key in the "%s" file',
119 1
				$this->entryPointJsonPath
120
			));
121
		}
122
123 1
		if (NULL !== $this->cache) {
124 1
			$this->cache->save($this->getBuildName(), $this->entriesData);
125
		}
126
127 1
		return $this->entriesData;
128
	}
129
130
	/*********************** interface \SixtyEightPublishers\WebpackEncoreBundle\EntryPoint\IEntryPointLookup ***********************/
131
132
	/**
133
	 * {@inheritdoc}
134
	 */
135
	public function getBuildName(): string
136
	{
137 1
		return $this->buildName;
138
	}
139
140
	/**
141
	 * {@inheritdoc}
142
	 */
143
	public function getJsFiles(string $entryName): array
144
	{
145 1
		return $this->getEntryFiles($entryName, 'js');
146
	}
147
148
	/**
149
	 * {@inheritdoc}
150
	 */
151
	public function getCssFiles(string $entryName): array
152
	{
153 1
		return $this->getEntryFiles($entryName, 'css');
154
	}
155
156
	/**
157
	 * {@inheritdoc}
158
	 */
159
	public function reset(): void
160
	{
161 1
		$this->returnedFiles = [];
162 1
	}
163
164
	/*********************** interface \SixtyEightPublishers\WebpackEncoreBundle\EntryPoint\IIntegrityDataProvider ***********************/
165
166
	/**
167
	 * {@inheritdoc}
168
	 */
169
	public function getIntegrityData(): array
170
	{
171 1
		$integrity = $this->getEntriesData()['integrity'] ?? [];
172
173 1
		return is_array($integrity) ? $integrity : [];
174
	}
175
}
176