Passed
Pull Request — master (#48)
by Arman
03:56
created

AssetManager   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 36
c 3
b 0
f 0
dl 0
loc 141
rs 10
wmc 19

7 Methods

Rating   Name   Duplication   Size   Complexity  
A url() 0 3 1
A registerAsset() 0 3 1
B publish() 0 21 7
A register() 0 4 2
A dump() 0 9 4
A getInstance() 0 7 2
A setPosition() 0 6 2
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.6.0
13
 */
14
15
namespace Quantum\Libraries\Asset;
16
17
use Quantum\Exceptions\AssetException;
18
19
/**
20
 * Class AssetManager
21
 * @package Quantum\Libraries\Asset
22
 */
23
class AssetManager
24
{
25
26
    /**
27
     * CSS assets store
28
     */
29
    const CSS_STORE = 1;
30
31
    /**
32
     * JS assets store
33
     */
34
    const JS_STORE = 2;
35
36
    /**
37
     * Asset storage
38
     * @var array[][]
39
     */
40
    private $storage = [];
41
42
    /**
43
     * Published assets
44
     * @var array
45
     */
46
    private $published = [];
47
48
    /**
49
     * Asset templates
50
     * @var string[]
51
     */
52
    private $templates = [
53
        self::CSS_STORE => '<link rel="stylesheet" type="text/css" href="{%1}">',
54
        self::JS_STORE => '<script src="{%1}"></script>',
55
    ];
56
57
    /**
58
     * Asset instance
59
     * @var \Quantum\Libraries\Asset\AssetManager|null
60
     */
61
    private static $instance = null;
62
63
    /**
64
     * AssetManager instance
65
     * @return \Quantum\Libraries\Asset\AssetManager|null
66
     */
67
    public static function getInstance(): ?AssetManager
68
    {
69
        if (self::$instance == null) {
70
            self::$instance = new self();
71
        }
72
73
        return self::$instance;
74
    }
75
76
    /**
77
     * Registers assets
78
     * @param \Quantum\Libraries\Asset\Asset[] $assets
79
     */
80
    public function register(array $assets)
81
    {
82
        foreach ($assets as $asset) {
83
            $this->registerAsset($asset);
84
        }
85
    }
86
87
    /**
88
     * Dumps the assets
89
     * @param int $type
90
     * @throws \Quantum\Exceptions\AssetException
91
     */
92
    public function dump(int $type)
93
    {
94
        if (empty($this->published)) {
95
            $this->publish();
96
        }
97
98
        if (count($this->published[$type])) {
99
            foreach ($this->published[$type] as $path) {
100
                echo _message($this->templates[$type], $this->url($path)) . PHP_EOL;
101
            }
102
        }
103
    }
104
105
    /**
106
     * Asset url
107
     * @param string $path
108
     * @return string
109
     */
110
    public function url(string $path): string
111
    {
112
        return base_url() . '/assets/' . $path;
113
    }
114
115
    /**
116
     * Publishes assets
117
     * @throws \Quantum\Exceptions\AssetException
118
     */
119
    private function publish()
120
    {
121
        if ($this->storage) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->storage of type array<mixed,array<mixed,array>> is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
122
            foreach ($this->storage as $asset) {
123
                if ($asset->getPosition() != -1) {
124
                    if (isset($this->published[$asset->getType()][$asset->getPosition()])) {
125
                        throw AssetException::positionInUse($asset->getPosition(), $asset->getPath());
126
                    }
127
128
                    $this->published[$asset->getType()][$asset->getPosition()] = $asset->getPath();
129
                }
130
            }
131
132
            foreach ($this->storage as $asset) {
133
                if ($asset->getPosition() == -1) {
134
                    $this->setPosition($asset->getType(), 0, $asset->getPath());
135
                }
136
            }
137
138
            ksort($this->published[self::CSS_STORE]);
139
            ksort($this->published[self::JS_STORE]);
140
        }
141
    }
142
143
    /**
144
     * Registers an asset
145
     * @param \Quantum\Libraries\Asset\Asset $asset
146
     */
147
    private function registerAsset(Asset $asset)
148
    {
149
        $this->storage[] = $asset;
150
    }
151
152
    /**
153
     * Sets the Position
154
     * @param int $type
155
     * @param int $index
156
     * @param string $value
157
     */
158
    private function setPosition(int $type, int $index, string $value)
159
    {
160
        if (isset($this->published[$type][$index])) {
161
            $this->setPosition($type, $index + 1, $value);
162
        } else {
163
            $this->published[$type][$index] = $value;
164
        }
165
    }
166
167
}
168