Completed
Push — master ( 03d8b8...64d4be )
by Denis
05:05
created

Manager::getPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * JBZoo Assets
4
 *
5
 * This file is part of the JBZoo CCK package.
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @package   Assets
10
 * @license   MIT
11
 * @copyright Copyright (C) JBZoo.com,  All rights reserved.
12
 * @link      https://github.com/JBZoo/Assets
13
 * @author    Sergey Kalistratov <[email protected]>
14
 */
15
16
namespace JBZoo\Assets;
17
18
use JBZoo\Assets\Asset\Asset;
19
use JBZoo\Data\Data;
20
use JBZoo\Path\Path;
21
use JBZoo\Utils\Arr;
22
23
/**
24
 * Class Manager
25
 * @package JBZoo\Assets
26
 */
27
class Manager
28
{
29
    /**
30
     * @var Factory
31
     */
32
    protected $_factory;
33
34
    /**
35
     * @var Collection
36
     */
37
    protected $_collection;
38
39
    /**
40
     * @var array
41
     */
42
    protected $_queued = [];
43
44
    /**
45
     * @var Path
46
     */
47
    protected $_path;
48
49
    /**
50
     * @var array
51
     */
52
    protected $_default = [
53
        'debug' => false,
54
        'less'  => [],
55
    ];
56
57
    /**
58
     * Manager constructor.
59
     *
60
     * @param Path  $path
61
     * @param array $params
62
     */
63
    public function __construct(Path $path, $params = [])
64
    {
65
        $this->_params = new Data(array_merge($this->_default, $params));
0 ignored issues
show
Bug introduced by SmetDenis
The property _params does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
66
67
        $this->_path       = $path;
68
        $this->_factory    = new Factory($this);
69
        $this->_collection = new Collection();
70
    }
71
72
    /**
73
     * @return Data
74
     */
75
    public function getParams()
76
    {
77
        return $this->_params;
78
    }
79
80
    /**
81
     * @param string $key
82
     * @param mixed  $value
83
     */
84
    public function setParam($key, $value)
85
    {
86
        $this->_params->set($key, $value);
87
    }
88
89
    /**
90
     * @return Path
91
     */
92
    public function getPath()
93
    {
94
        return $this->_path;
95
    }
96
97
    /**
98
     * Adds a registered asset or a new asset to the queue.
99
     *
100
     * @param string       $alias
101
     * @param string|null  $source
102
     * @param string|array $dependencies
103
     * @param string|array $options
104
     * @return $this
105
     */
106
    public function add($alias, $source = null, $dependencies = [], $options = [])
107
    {
108
        if ($source !== null) {
109
            $asset = $this->_factory->create($alias, $source, $dependencies, $options);
110
            $this->_collection->add($asset);
111
112
        } else {
113
            $asset = $this->_collection->get($alias);
114
        }
115
116
        if ($asset) {
117
            $this->_queued[$alias] = true;
118
        }
119
120
        return $this;
121
    }
122
123
    /**
124
     * Removes an asset from the queue.
125
     *
126
     * @param string $alias
127
     * @return $this
128
     */
129
    public function remove($alias)
130
    {
131
        unset($this->_queued[$alias]);
132
        return $this;
133
    }
134
135
    /**
136
     * Registers an asset.
137
     *
138
     * @param string       $alias
139
     * @param null|string  $source
140
     * @param array        $dependencies
141
     * @param string|array $options
142
     * @return $this
143
     */
144
    public function register($alias, $source = null, $dependencies = [], $options = [])
145
    {
146
        $asset = $this->_factory->create($alias, $source, $dependencies, $options);
147
        $this->_collection->add($asset);
148
149
        return $this;
150
    }
151
152
    /**
153
     * Unregisters an asset from collection.
154
     *
155
     * @param string $alias
156
     * @return $this
157
     */
158
    public function unregister($alias)
159
    {
160
        $this->_collection->remove($alias);
161
        $this->remove($alias);
162
        return $this;
163
    }
164
165
    /**
166
     * Get asset collections.
167
     *
168
     * @return Collection
169
     */
170
    public function getCollection()
171
    {
172
        return $this->_collection;
173
    }
174
175
    /**
176
     * Get asset collections.
177
     *
178
     * @return Factory
179
     */
180
    public function getFactory()
181
    {
182
        return $this->_factory;
183
    }
184
185
    /**
186
     * Build assets.
187
     *
188
     * @param array $filters
189
     * @return array
190
     */
191
    public function build(array $filters = [])
192
    {
193
        $assets = [];
194
        foreach (array_keys($this->_queued) as $alias) {
195
            $this->_resolveDependencies($this->_collection->get($alias), $assets);
0 ignored issues
show
Bug introduced by SmetDenis
It seems like $this->_collection->get($alias) can be null; however, _resolveDependencies() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
196
        }
197
198
        /** @var Asset $asset */
199
        $result = [
200
            Asset::TYPE_JS_FILE  => [],
201
            Asset::TYPE_CSS_FILE => [],
202
            Asset::TYPE_JS_CODE  => [],
203
            Asset::TYPE_CSS_CODE => [],
204
        ];
205
        foreach ($assets as $asset) {
206
207
            $source = $asset->load($filters);
208
209
            if (Asset::TYPE_CALLBACK === $source[0]) {
210
                continue;
211
            }
212
213
            if (Asset::TYPE_COLLECTION === $source[0]) {
214
                $source = $source[1];
215
            } else {
216
                $source = array($source);
217
            }
218
219
            foreach ($source as $sourceItem) {
220
221
                $type = $sourceItem[0];
222
                $src  = $sourceItem[1];
223
224
                if ($src && !Arr::in($src, $result[$type])) {
225
                    $result[$type][] = $src;
226
                }
227
            }
228
        }
229
230
        return $result;
231
    }
232
233
    /**
234
     * Resolves asset dependencies.
235
     *
236
     * @param Asset|null $asset
237
     * @param Asset[]    $resolved
238
     * @param Asset[]    $unresolved
239
     * @return Asset[]
240
     * @throws Exception
241
     */
242
    protected function _resolveDependencies(Asset $asset, &$resolved = [], &$unresolved = [])
243
    {
244
        $unresolved[$asset->getAlias()] = $asset;
245
246
        foreach ($asset->getDependencies() as $dependency) {
247
            if (!Arr::key($dependency, $resolved)) {
248
249
                if (isset($unresolved[$dependency])) {
250
                    throw new Exception(sprintf(
251
                        'Circular asset dependency "%s > %s" detected.',
252
                        $asset->getAlias(),
253
                        $dependency
254
                    ));
255
                }
256
257
                if ($dep = $this->_collection->get($dependency)) {
0 ignored issues
show
Bug introduced by cheren
Are you sure the assignment to $dep is correct as $this->_collection->get($dependency) (which targets JBZoo\Assets\Collection::get()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
258
                    $this->_resolveDependencies($dep, $resolved, $unresolved);
259
                }
260
            }
261
        }
262
263
        $resolved[$asset->getAlias()] = $asset;
264
        unset($unresolved[$asset->getAlias()]);
265
266
        return $resolved;
267
    }
268
}
269