Passed
Push — master ( e649db...77f910 )
by Julien
04:20
created

Manager::outputCss()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 2
c 2
b 1
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * This file is part of the Zemit Framework.
4
 *
5
 * (c) Zemit Team <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE.txt
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Zemit\Assets;
12
13
use Phalcon\Assets\Collection;
14
15
/**
16
 * Class Manager
17
 * {@inheritDoc}
18
 *
19
 * @author Julien Turbide <[email protected]>
20
 * @copyright Zemit Team <[email protected]>
21
 *
22
 * @since 1.0
23
 * @version 1.0
24
 *
25
 * @package Zemit\Assets
26
 */
27
class Manager extends \Phalcon\Assets\Manager
28
{
29
    /**
30
     * Version of your app (ex. 1.0.0)
31
     * @var string Set the version to be added in the asset path
32
     */
33
    protected string $version;
34
    
35
    /**
36
     *
37
     * @var bool true to automatically add the file time to the asset path
38
     */
39
    protected bool $fileTime;
40
    
41
    /**
42
     * Minify Javascript
43
     * @var bool true automatically minify javascript files
44
     */
45
    protected bool $minifyJS;
46
    
47
    /**
48
     * Minify CSS
49
     * @var bool true automatically minify stylesheet files
50
     */
51
    protected bool $minifyCSS;
52
    
53
    /**
54
     * Force the version manually
55
     * Version will be added to the assets path
56
     *
57
     * @param string $version
58
     */
59
    public function setVersion($version)
60
    {
61
        $this->version = $version;
62
    }
63
    
64
    /**
65
     * Get the version if forced
66
     * @return string Version
67
     */
68
    public function getVersion()
69
    {
70
        return $this->version;
71
    }
72
    
73
    /**
74
     * Set File Time
75
     * @param $fileTime True to enable
76
     */
77
    public function setFileTime(bool $fileTime) : void
78
    {
79
        $this->fileTime = $fileTime ? true : false;
80
    }
81
    
82
    /**
83
     * Get File Time
84
     * @return bool True if enabled
85
     */
86
    public function getFileTime()
87
    {
88
        return $this->fileTime ? true : false;
89
    }
90
    
91
    /**
92
     * Set minify JS
93
     * @todo to be removed
94
     * @deprecated You should use webpack or something else
95
     * @param $minifyJS True to enable
96
     */
97
    public function setMinifyJS(bool $minifyJS) : void
98
    {
99
        $this->minifyJS = $minifyJS ? true : false;
100
    }
101
    
102
    /**
103
     * @return bool
104
     * @todo to be removed
105
     * @deprecated You should use webpack or something else
106
     */
107
    public function getMinifyJS() : bool
108
    {
109
        return $this->minifyJS ? true : false;
110
    }
111
    
112
    /**
113
     * @todo to be removed
114
     * @deprecated You should use webpack or something else
115
     * @param $minifyCSS
116
     */
117
    public function setMinifyCSS(bool $minifyCSS) : void
118
    {
119
        $this->minifyCSS = $minifyCSS ? true : false;
120
    }
121
    
122
    /**
123
     * @todo to be removed
124
     * @deprecated You should use webpack or something else
125
     * @return bool
126
     */
127
    public function getMinifyCSS() : bool
128
    {
129
        return $this->minifyCSS ? true : false;
130
    }
131
    
132
    /**
133
     * @param string|null $collectionName
134
     *
135
     * @return string
136
     */
137
    public function outputJs(?string $collectionName = 'js'): string
138
    {
139
        $this->setCollectionVersion($collectionName);
140
        
141
        return parent::outputJs($collectionName);
142
    }
143
    
144
    /**
145
     * @param null $collectionName
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $collectionName is correct as it would always require null to be passed?
Loading history...
146
     *
147
     * @return string|null
148
     */
149
    public function outputCss(?string $collectionName = 'css'): string
150
    {
151
        $this->setCollectionVersion($collectionName);
152
        
153
        return parent::outputCss($collectionName);
154
    }
155
    
156
    /**
157
     * Add version to the collection
158
     *
159
     * @param string|null $collectionName
160
     *
161
     * @return Collection
162
     */
163
    public function setCollectionVersion(?string $collectionName = null): Collection
164
    {
165
        $collection = $this->exists($collectionName) ? $this->get($collectionName) : false;
0 ignored issues
show
Bug introduced by
It seems like $collectionName can also be of type null; however, parameter $id of Phalcon\Assets\Manager::exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

165
        $collection = $this->exists(/** @scrutinizer ignore-type */ $collectionName) ? $this->get($collectionName) : false;
Loading history...
Bug introduced by
It seems like $collectionName can also be of type null; however, parameter $id of Phalcon\Assets\Manager::get() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

165
        $collection = $this->exists($collectionName) ? $this->get(/** @scrutinizer ignore-type */ $collectionName) : false;
Loading history...
166
        if ($collection) {
167
            $collection = $this->get($collectionName);
168
            if ($collection) {
0 ignored issues
show
introduced by
$collection is of type Phalcon\Assets\Collection, thus it always evaluated to true.
Loading history...
169
                $version = $this->getVersion();
170
                if (empty($version)) {
171
                    $collection->setVersion($version);
172
                }
173
                else {
174
                    $collection->setAutoVersion(true);
175
                }
176
            }
177
        }
178
        
179
        return $collection;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $collection could return the type false which is incompatible with the type-hinted return Phalcon\Assets\Collection. Consider adding an additional type-check to rule them out.
Loading history...
180
    }
181
    
182
    /**
183
     * Add version to a path
184
     *
185
     * @todo to be removed or check if phalcon team implemented it according to our needs
186
     * @deprecated Now natively supported by phalcon asset collection itself
187
     *
188
     * @param $path
189
     * @param $version
190
     * @param false $addFileMTimeToPath
191
     *
192
     * @return string
193
     */
194
    private static function _addVersionToPath($path, $version, $addFileMTimeToPath = false)
0 ignored issues
show
Unused Code introduced by
The method _addVersionToPath() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
195
    {
196
        if ($addFileMTimeToPath) {
0 ignored issues
show
introduced by
The condition $addFileMTimeToPath is always false.
Loading history...
197
            $path = self::_addFileMtimeToPath($path);
198
        }
199
        if (!empty($version)) {
200
            $path = explode('.', $path);
201
            $ext = array_pop($path);
202
            $path = implode('.', $path) . '.' . $version . '.' . $ext;
203
        }
204
        
205
        return $path;
206
    }
207
    
208
    /**
209
     * Add File Mime Time to the path
210
     *
211
     * @deprecated
212
     *
213
     * @param $filepath
214
     *
215
     * @return string
216
     */
217
    private static function _addFileMtimeToPath(string $filepath)
218
    {
219
        $path = $filepath;
220
        if (file_exists($filepath)) {
221
            $path = explode('.', $filepath);
222
            $ext = array_pop($path);
223
            $path = implode('.', $path) . '.' . date('Ymdhis', filemtime($filepath)) . '.' . $ext;
224
        }
225
        
226
        return $path;
227
    }
228
}
229