Assets::get()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 2
b 0
f 0
nc 1
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Enjoys\AssetsCollector;
6
7
use Enjoys\AssetsCollector\Exception\NotAllowedMethods;
8
use Psr\Log\LoggerInterface;
9
10
class Assets
11
{
12
    public const GROUP_COMMON = 'common';
13
14
    /*
15
     * @var AssetsCollection
16
     */
17
    private AssetsCollection $assetsCollection;
18
19
    /**
20
     * @var LoggerInterface
21
     */
22
    private LoggerInterface $logger;
23
24
    public function __construct(private readonly Environment $environment)
25
    {
26
        $this->logger = $this->environment->getLogger();
27
        $this->assetsCollection = new AssetsCollection($this->logger);
28
    }
29
30
    /**
31
     * @param AssetType $type
32
     * @param array|string $paths
33
     * @param string $group
34
     * @param string $method
35
     * @return $this
36 15
     */
37
    public function add(
38 15
        AssetType $type,
39 15
        array|string $paths,
40 15
        string $group = self::GROUP_COMMON,
41
        string $method = 'push'
42
    ): Assets {
43
        $collection = new AssetsCollection($this->logger);
44
        /** @var array|string $path */
45
        foreach ((array)$paths as $path) {
46
            $params = [];
47
            if (is_array($path)) {
48
                $params = $path;
49
50 15
                /** @var string $path */
51
                $path = array_shift($params);
52 15
            }
53
54 15
            /** @var array<string, array|bool> $params */
55 15
            $collection->add(
56 15
                new Asset($type, $path, $params),
57 5
                $group
58
            );
59
        }
60 5
61
        if (!in_array($method, ['push', 'unshift'], true)) {
62
            throw new NotAllowedMethods('Allowed methods only `push` and `unshift`');
63
        }
64 15
65 15
        $this->assetsCollection->$method($collection);
66 15
67 15
        return $this;
68
    }
69
70 15
    public function get(AssetType $type, string $group = self::GROUP_COMMON): string
71 1
    {
72
        return $this->environment->getRenderer($type)->render(
73
            $this->environment->getStrategy()->getAssets(
74 14
                $type,
75
                $this->assetsCollection->get($type, $group),
76 14
                $this->environment
77
            )
78
        );
79
    }
80
81
    /**
82
     * @todo: maybe remove
83
     */
84
    public function getEnvironment(): Environment
85 13
    {
86
        return $this->environment;
87 13
    }
88 13
89
}
90