Completed
Pull Request — master (#5)
by Michal
10:41 queued 09:11
created

Manifest::getRevision()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 9.7
c 0
b 0
f 0
cc 3
nc 4
nop 2
crap 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Webrouse\AssetMacro;
5
6
7
use Nette\Utils\Json;
8
use Nette\Utils\JsonException;
9
use Webrouse\AssetMacro\Exceptions\AssetNotFoundException;
10
use Webrouse\AssetMacro\Exceptions\ManifestJsonException;
11
use Webrouse\AssetMacro\Exceptions\RevisionNotFound;
12
13
class Manifest
14
{
15
	/** @var Config */
16
	private $config;
17
18
	/** @var string|null */
19
	private $path;
20
21
	/** @var array */
22
	private $data = [];
23
24
25 1
	public function __construct(Config $config, ?string $path, array $data = null)
26
	{
27 1
		$this->config = $config;
28 1
		$this->path = $path;
29
30 1
		if ($data) {
31 1
			$raw = $data;
32
		} else {
33 1
			assert(file_exists($path));
34
			try {
35 1
				$raw = Json::decode((string) file_get_contents($path), Json::FORCE_ARRAY);
36
			} catch (JsonException $e) {
0 ignored issues
show
Bug introduced by
The class Nette\Utils\JsonException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
37
				throw new ManifestJsonException('Invalid JSON in manifest.', 0, $e);
38
			}
39
		}
40
41
		// Strip optional leading / from source and target path (key and value)
42 1
		foreach ($raw as $key => $value) {
43 1
			$key = ltrim((string) $key, '/');
44 1
			$value = ltrim((string) $value, '/');
45 1
			$this->data[$key] = $value;
46
		}
47 1
	}
48
49
50 1
	public function getAsset(string $path, bool $needed = true): ?Asset
51
	{
52 1
		$revision = self::getRevision($path, $needed);
53
54 1
		if (!file_exists($revision->getAbsolutePath())) {
55 1
			Utils::throwError(
56 1
				new AssetNotFoundException(sprintf("Asset '%s' not found.", $revision->getAbsolutePath())),
57 1
				$this->config->getMissingAssetPolicy(),
58 1
				$needed
59
			);
60
61 1
			return null;
62
		}
63
64 1
		return new Asset($revision);
65
	}
66
67
68 1
	public function getRevision(string $path, bool $needed = true): Revision
69
	{
70
		// Strip optional leading / from path
71 1
		$path = Utils::normalizePath(ltrim($path, '/'));
72 1
		$revision = isset($this->data[$path]) ? $this->data[$path] : null;
73
74
		// Throw error if revision not found in manifest
75 1
		if ($revision === null) {
76 1
			Utils::throwError(
77 1
				new RevisionNotFound(sprintf("Revision for asset '%s' not found in manifest %s.", $path, $this->path)),
78 1
				$this->config->getMissingRevisionPolicy(),
79 1
				$needed
80
			);
81
		}
82
83 1
		return new Revision($this->config, $path, $revision);
84
	}
85
}
86