AssetsCollection::push()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Enjoys\AssetsCollector;
4
5
use Psr\Log\LoggerInterface;
6
use Psr\Log\NullLogger;
7
8
class AssetsCollection
9
{
10
    /**
11
     * @var array<string, array<string, array<string, Asset>>>
12
     */
13
    private array $assets = [];
14
15
16
    public function __construct(private readonly LoggerInterface $logger = new NullLogger())
17
    {
18
    }
19 17
20
    public function add(Asset $asset, string $group): void
21 17
    {
22
        if (!$asset->isValid()) {
23
            $this->logger->notice(sprintf('Path invalid: %s', $asset->getOrigPath()));
24 17
            return;
25
        }
26 17
27 3
        if ($this->has($asset, $group)) {
28 3
            $this->logger->notice(sprintf('Duplicate path: %s', $asset->getOrigPath()));
29
            return;
30
        }
31 17
32 1
33 1
        $this->assets[$asset->getType()->value][$group][$asset->getId()] = $asset;
34
    }
35
36
    public function has(Asset $asset, string $group): bool
37 17
    {
38
        if (isset($this->assets[$asset->getType()->value][$group][$asset->getId()])) {
39
            return true;
40 17
        }
41
        return false;
42 17
    }
43 1
44
    /**
45 17
     * @param AssetType $type
46
     * @param string $group
47
     * @return Asset[]
48
     */
49
    public function get(AssetType $type, string $group): array
50
    {
51
        if (!isset($this->assets[$type->value][$group])) {
52
            return [];
53 15
        }
54
        return $this->assets[$type->value][$group];
55 15
    }
56 1
57
    /**
58 15
     * @psalm-suppress MixedPropertyTypeCoercion
59
     */
60
    public function push(AssetsCollection $collection): void
61
    {
62
        $this->assets = array_merge_recursive_distinct($this->getAssets(), $collection->getAssets());
63
    }
64
65 13
    /**
66
     * @psalm-suppress MixedPropertyTypeCoercion
67 13
     */
68
    public function unshift(AssetsCollection $collection): void
69
    {
70
        $this->assets = array_merge_recursive_distinct($collection->getAssets(), $this->getAssets());
71
    }
72
73
74 1
    /**
75
     * @return array<string, array<string, array<string, Asset>>>
76 1
     */
77
    public function getAssets(): array
78
    {
79
        return $this->assets;
80 14
    }
81
}
82