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

Factory::create()   C

Complexity

Conditions 11
Paths 48

Size

Total Lines 35
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 35
rs 5.2653
cc 11
eloc 21
nc 48
nop 4

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Utils\FS;
21
22
/**
23
 * Class Factory
24
 * @package JBZoo\Assets
25
 */
26
class Factory
27
{
28
    /**
29
     * @var array
30
     */
31
    protected $_customTypes = [
32
        Asset::TYPE_CSS_CODE => 'CssCode',
33
        Asset::TYPE_JS_CODE  => 'JsCode',
34
    ];
35
36
    /**
37
     * @var Manager
38
     */
39
    protected $_manager;
40
41
    /**
42
     * Factory constructor.
43
     * @param Manager $manager
44
     */
45
    public function __construct(Manager $manager)
46
    {
47
        $this->_manager = $manager;
48
    }
49
50
    /**
51
     * @return Manager
52
     */
53
    public function getManager()
54
    {
55
        return $this->_manager;
56
    }
57
58
    /**
59
     * Create asset instance.
60
     *
61
     * @param string       $alias
62
     * @param mixed        $source
63
     * @param string|array $dependencies
64
     * @param string|array $options
65
     * @throws Exception
66
     * @return Asset
67
     */
68
    public function create($alias, $source, $dependencies = [], $options = [])
69
    {
70
        $assetType = isset($options['type']) ? $options['type'] : '';
71
72
        if (isset($this->_customTypes[$assetType])) {
73
            $assetType = $this->_customTypes[$assetType];
74
75
        } elseif (is_callable($source)) {
76
            $assetType = 'Callback';
77
78
        } elseif (is_string($source)) {
79
80
            $ext = strtolower(FS::ext($source));
81
            if ($ext === 'js') {
82
                $assetType = 'JsFile';
83
84
            } elseif ($ext === 'css') {
85
                $assetType = 'CssFile';
86
87
            } elseif ($ext === 'less') {
88
                $assetType = 'LessFile';
89
            }
90
91
        } elseif (is_array($source)) {
92
            $assetType = 'Collection';
93
        }
94
95
        $className = __NAMESPACE__ . '\\Asset\\' . $assetType;
96
        if (class_exists($className)) {
97
            $options = is_array($options) ? new Data($options) : $options;
98
            return new $className($this->getManager(), $alias, $source, $dependencies, $options);
99
        }
100
101
        throw new Exception('Undefined asset type');
102
    }
103
104
    /**
105
     * @param string $type
106
     * @param string $className
107
     */
108
    public function registerType($type, $className)
109
    {
110
        $this->_customTypes[$type] = $className;
111
    }
112
}
113