Completed
Push — master ( 579af5...b29473 )
by Oleg
07:53
created

Asset::__construct()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 24
rs 8.5125
c 1
b 1
f 0
cc 6
eloc 12
nc 12
nop 1
1
<?php /** MicroAsset */
2
3
namespace Micro\Web;
4
5
use Micro\Base\Autoload;
6
use Micro\Base\Exception;
7
use Micro\File\FileHelper;
8
use Micro\Mvc\Views\IView;
9
10
/**
11
 * Asset class file.
12
 *
13
 * @author Oleg Lunegov <[email protected]>
14
 * @link https://github.com/lugnsk/micro
15
 * @copyright Copyright &copy; 2013 Oleg Lunegov
16
 * @license /LICENSE
17
 * @package Micro
18
 * @subpackage Web
19
 * @version 1.0
20
 * @since 1.0
21
 */
22
class Asset
23
{
24
    /** @var string $sourcePath Full-path to source asset dir */
25
    public $sourcePath;
26
27
    /** @var bool $isHead Is a publish into head block */
28
    public $isHead = true;
29
    /** @var array $js JavaScript files links */
30
    public $js = [];
31
    /** @var array $css CSS files links */
32
    public $css = [];
33
    /** @var array $required Required assets */
34
    public $required = [];
35
    /** @var array $excludes Excludes extensions */
36
    public $excludes = [];
37
38
    /** @var IView $view View for install current asset */
39
    protected $view;
40
    /** @var string $hash Unique directory to publish into assets dir */
41
    protected $hash;
42
    /** @var string $publishPath Publish path */
43
    protected $publishPath;
44
    /** @var array $published Published required extends */
45
    private $published = [];
46
47
48
    /**
49
     * Constructor asset
50
     *
51
     * @access public
52
     *
53
     * @param IView $view
54
     *
55
     * @result void
56
     * @throws \Micro\Base\Exception
57
     */
58
    public function __construct(IView $view)
59
    {
60
        $this->view = $view;
61
62
        if (!$this->sourcePath) {
63
            $this->sourcePath = dirname(Autoload::getClassPath(get_class($this)));
64
        }
65
66
        $this->hash = md5($this->sourcePath);
67
68
        $this->publishPath = '/' . (($dir = $view->container->assetsDirName) ? $dir : 'assets') . '/' . $this->hash;
0 ignored issues
show
Bug introduced by
Accessing container on the interface Micro\Mvc\Views\IView suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
69
70
        $web = $this->view->container->kernel->getWebDir();
0 ignored issues
show
Bug introduced by
Accessing container on the interface Micro\Mvc\Views\IView suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
71
72
        if (!file_exists($this->sourcePath)) {
73
            throw new Exception('Asset dir not exists: ' . $this->sourcePath);
74
        }
75
76
        if (!@mkdir($web . $this->publishPath, 0777) && !is_dir($web . $this->publishPath)) {
77
            throw new Exception('Could not access to publish dir: ' . $this->publishPath);
78
        }
79
80
        FileHelper::recurseCopyIfEdited($this->sourcePath, $web . $this->publishPath, $this->excludes);
81
    }
82
83
    /**
84
     * Send asset into view
85
     *
86
     * @access public
87
     * @return void
88
     */
89
    public function publish()
90
    {
91
        foreach ($this->required AS $require) {
92
            if (!in_array($require, $this->published, true) && class_exists($require)) {
93
                $this->published[] = $require;
94
                /** @var Asset $require */
95
                $require = new $require($this->view);
96
                $require->publish();
97
            }
98
        }
99
100 View Code Duplication
        if ($this->js) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->js of type 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...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
            if (is_string($this->js)) {
102
                $this->js = [$this->js];
103
            }
104
            foreach ($this->js AS $script) {
105
                $this->view->registerScriptFile($this->publishPath . $script, $this->isHead);
106
            }
107
        }
108 View Code Duplication
        if ($this->css) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->css of type 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...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
            if (is_string($this->css)) {
110
                $this->css = [$this->css];
111
            }
112
            foreach ($this->css AS $style) {
113
                $this->view->registerCssFile($this->publishPath . $style, $this->isHead);
114
            }
115
        }
116
    }
117
}
118