Passed
Pull Request — master (#15)
by Enjoys
02:34
created

Assets   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 93.94%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 33
c 3
b 0
f 1
dl 0
loc 110
ccs 31
cts 33
cp 0.9394
rs 10
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getEnvironment() 0 3 1
A getLogger() 0 3 1
A add() 0 27 4
A get() 0 4 1
A getResults() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Enjoys\AssetsCollector;
6
7
use Enjoys\AssetsCollector\CollectStrategy\StrategyFactory;
8
use Enjoys\AssetsCollector\Exception\NotAllowedMethods;
9
use Enjoys\AssetsCollector\Render\RenderFactory;
10
use Psr\Log\LoggerInterface;
11
12
class Assets
13
{
14
    public const NAMESPACE_COMMON = 'common';
15
16
    public const RENDER_HTML = 'html';
17
18
    public const STRATEGY_ONE_FILE = 0;
19
    public const STRATEGY_MANY_FILES = 1;
20
21
    /*
22
     * @var AssetsCollection
23
     */
24
    private AssetsCollection $assetsCollection;
25
26
    /**
27
     * @var Environment
28
     */
29
    private Environment $environment;
30
31
    /**
32
     * @var LoggerInterface
33
     */
34
    private LoggerInterface $logger;
35
36 15
    public function __construct(Environment $environment)
37
    {
38 15
        $this->environment = $environment;
39 15
        $this->logger = $this->environment->getLogger();
40 15
        $this->assetsCollection = new AssetsCollection($this->environment);
41
    }
42
43
    /**
44
     * @param string $type
45
     * @param string|array $paths
46
     * @param string $namespace
47
     * @param string $method
48
     * @return $this
49
     */
50 15
    public function add(string $type, $paths, string $namespace = self::NAMESPACE_COMMON, string $method = 'push'): Assets
51
    {
52 15
        $collection = new AssetsCollection($this->environment);
53
        /** @var array|string $path */
54 15
        foreach ((array)$paths as $path) {
55 15
            $params = [];
56 15
            if (is_array($path)) {
57 4
                $params = $path;
58
59
                /** @var string $path */
60 4
                $path = array_shift($params);
61
            }
62
63
            /** @var array<string, array|bool|null|string> $params */
64 15
            $collection->add(
65 15
                new Asset($type, $path, $params),
66 15
                $namespace
67 15
            );
68
        }
69
70 15
        if (!in_array($method, ['push', 'unshift'], true)) {
71 1
            throw new NotAllowedMethods('Allowed methods only `push` and `unshift`');
72
        }
73
74 14
        $this->assetsCollection->$method($collection);
75
76 14
        return $this;
77
    }
78
79
    /**
80
     * @param string $type
81
     * @param string $namespace
82
     * @return string
83
     * @throws \Exception
84
     */
85 13
    public function get(string $type, string $namespace = self::NAMESPACE_COMMON): string
86
    {
87 13
        $paths = $this->getResults($type, $this->assetsCollection->get($type, $namespace));
88 13
        return RenderFactory::getRender(\strtolower($type), $this->environment)->getResult($paths);
89
    }
90
91
92
    /**
93
     * @param string $type
94
     * @param array<Asset> $assetsCollection
95
     * @return array
96
     */
97 13
    private function getResults(string $type, array $assetsCollection): array
98
    {
99 13
        $strategy = StrategyFactory::getStrategy(
100 13
            $this->environment,
101 13
            $assetsCollection,
102 13
            $type
103 13
        );
104
105 13
        return $strategy->getResult();
106
    }
107
108
    /**
109
     * @return Environment
110
     */
111 1
    public function getEnvironment(): Environment
112
    {
113 1
        return $this->environment;
114
    }
115
116
    /**
117
     * @return LoggerInterface
118
     */
119
    public function getLogger(): LoggerInterface
120
    {
121
        return $this->logger;
122
    }
123
}
124