EntryPointLookup::getIntegrityData()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 5
ccs 2
cts 2
cp 1
crap 2
rs 10
c 0
b 0
f 0
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
	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 1
	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
	 * @throws \Throwable
47
	 */
48 1
	private function getEntryFiles(string $entryName, string $key): array
49
	{
50 1
		$entryData = $this->getEntryData($entryName);
51
52 1
		if (!isset($entryData[$key])) {
53 1
			return [];
54
		}
55
56 1
		$newFiles = array_values(array_diff($entryData[$key], $this->returnedFiles));
57 1
		$this->returnedFiles = array_merge($this->returnedFiles, $newFiles);
58
59 1
		return $newFiles;
60
	}
61
62
	/**
63
	 * @param string $entryName
64
	 *
65
	 * @return array
66
	 * @throws \SixtyEightPublishers\WebpackEncoreBundle\Exception\EntryPointNotFoundException
67
	 * @throws \Throwable
68
	 */
69 1
	private function getEntryData(string $entryName): array
70
	{
71 1
		$entriesData = $this->getEntriesData();
72
73 1
		if (isset($entriesData['entrypoints'][$entryName])) {
74 1
			return $entriesData['entrypoints'][$entryName];
75
		}
76
77 1
		$withoutExtension = substr($entryName, 0, (int) strrpos($entryName, '.'));
78
79 1
		if (!empty($withoutExtension) && isset($entriesData['entrypoints'][$withoutExtension])) {
80 1
			throw new SixtyEightPublishers\WebpackEncoreBundle\Exception\EntryPointNotFoundException(sprintf(
81 1
				'Could not find the entry "%s". Try "%s" instead (without the extension).',
82 1
				$entryName,
83 1
				$withoutExtension
84
			));
85
		}
86
87 1
		throw new SixtyEightPublishers\WebpackEncoreBundle\Exception\EntryPointNotFoundException(sprintf(
88 1
			'Could not find the entry "%s" in "%s". Found: %s.',
89 1
			$entryName,
90 1
			$this->entryPointJsonPath,
91 1
			implode(', ', array_keys($entriesData['entrypoints']))
92
		));
93
	}
94
95
	/**
96
	 * @return array
97
	 * @throws \SixtyEightPublishers\WebpackEncoreBundle\Exception\InvalidStateException
98
	 * @throws \Throwable
99
	 */
100
	private function getEntriesData(): array
101
	{
102 1
		if (NULL !== $this->entriesData) {
103 1
			return $this->entriesData;
104
		}
105
106 1
		if (NULL !== $this->cache && is_array($entriesData = $this->cache->load($this->getBuildName()))) {
107 1
			return $this->entriesData = $entriesData;
108
		}
109
110 1
		if (!file_exists($this->entryPointJsonPath)) {
111 1
			throw new SixtyEightPublishers\WebpackEncoreBundle\Exception\InvalidStateException(sprintf(
112 1
				'Could not find the entrypoints file from Webpack: the file "%s" does not exist.',
113 1
				$this->entryPointJsonPath
114
			));
115
		}
116
117
		try {
118 1
			$this->entriesData = Nette\Utils\Json::decode(file_get_contents($this->entryPointJsonPath), Nette\Utils\Json::FORCE_ARRAY);
119 1
		} catch (Nette\Utils\JsonException $e) {
120 1
			throw new SixtyEightPublishers\WebpackEncoreBundle\Exception\InvalidStateException(sprintf(
121 1
				'The entrypoints file "%s" is not valid JSON.',
122 1
				$this->entryPointJsonPath
123 1
			), 0, $e);
124
		}
125
126 1
		if (!isset($this->entriesData['entrypoints'])) {
127 1
			throw new SixtyEightPublishers\WebpackEncoreBundle\Exception\InvalidStateException(sprintf(
128 1
				'Could not find an "entrypoints" key in the "%s" file.',
129 1
				$this->entryPointJsonPath
130
			));
131
		}
132
133 1
		if (NULL !== $this->cache) {
134 1
			$this->cache->save($this->getBuildName(), $this->entriesData);
135
		}
136
137 1
		return $this->entriesData;
138
	}
139
140
	/*********************** interface \SixtyEightPublishers\WebpackEncoreBundle\EntryPoint\IEntryPointLookup ***********************/
141
142
	/**
143
	 * {@inheritdoc}
144
	 */
145
	public function getBuildName(): string
146
	{
147 1
		return $this->buildName;
148
	}
149
150
	/**
151
	 * {@inheritdoc}
152
	 *
153
	 * @throws \Throwable
154
	 */
155 1
	public function getJsFiles(string $entryName): array
156
	{
157 1
		return $this->getEntryFiles($entryName, 'js');
158
	}
159
160
	/**
161
	 * {@inheritdoc}
162
	 *
163
	 * @throws \Throwable
164
	 */
165 1
	public function getCssFiles(string $entryName): array
166
	{
167 1
		return $this->getEntryFiles($entryName, 'css');
168
	}
169
170
	/**
171
	 * {@inheritdoc}
172
	 */
173
	public function reset(): void
174
	{
175 1
		$this->returnedFiles = [];
176 1
	}
177
178
	/*********************** interface \SixtyEightPublishers\WebpackEncoreBundle\EntryPoint\IIntegrityDataProvider ***********************/
179
180
	/**
181
	 * {@inheritdoc}
182
	 *
183
	 * @throws \Throwable
184
	 */
185
	public function getIntegrityData(): array
186
	{
187 1
		$integrity = $this->getEntriesData()['integrity'] ?? [];
188
189 1
		return is_array($integrity) ? $integrity : [];
190
	}
191
}
192