JsonManifestVersioned   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 13
c 1
b 0
f 0
dl 0
loc 52
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A applyVersion() 0 7 2
A version() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tleckie\Assets\Versioned;
6
7
use InvalidArgumentException;
8
use JsonException;
9
use function file_get_contents;
10
use function is_file;
11
use function json_decode;
12
use function sprintf;
13
14
/**
15
 * Class JsonManifestVersioned
16
 *
17
 * @package  Tleckie\Assets\Versioned
18
 * @category JsonManifestVersioned
19
 * @author   Teodoro Leckie Westberg <[email protected]>
20
 */
21
class JsonManifestVersioned implements VersionedInterface
22
{
23
    /**
24
     * @var string
25
     */
26
    protected string $format = '';
27
28
    /**
29
     * @var array
30
     */
31
    protected array $data;
32
33
    /**
34
     * JsonManifestVersioned constructor.
35
     *
36
     * @param string $manifestPath
37
     * @throws JsonException|InvalidArgumentException
38
     */
39
    public function __construct(string $manifestPath)
40
    {
41
        if (!is_file($manifestPath)) {
42
            throw new InvalidArgumentException(
43
                sprintf('File [%s] not found.', $manifestPath)
44
            );
45
        }
46
47
        $this->data = json_decode(
48
            file_get_contents($manifestPath),
49
            true,
50
            2,
51
            JSON_THROW_ON_ERROR
52
        );
53
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58
    public function version(): string
59
    {
60
        return '';
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66
    public function applyVersion(string $asset): string
67
    {
68
        if (isset($this->data[$asset])) {
69
            $asset = $this->data[$asset];
70
        }
71
72
        return $asset;
73
    }
74
}
75