| Total Complexity | 41 |
| Total Lines | 316 |
| Duplicated Lines | 0 % |
| Changes | 11 | ||
| Bugs | 1 | Features | 0 |
Complex classes like Asset often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Asset, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class Asset implements \ArrayAccess |
||
| 21 | { |
||
| 22 | /** @var Builder */ |
||
| 23 | protected $builder; |
||
| 24 | /** @var Config */ |
||
| 25 | protected $config; |
||
| 26 | /** @var string[] */ |
||
| 27 | protected $pathinfo = []; |
||
| 28 | /** @var array */ |
||
| 29 | protected $data = []; |
||
| 30 | /** @var bool */ |
||
| 31 | protected $compiled = false; |
||
| 32 | /** @var bool */ |
||
| 33 | protected $versioned = false; |
||
| 34 | /** @var bool */ |
||
| 35 | protected $minified = false; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Creates an Asset from file. |
||
| 39 | * |
||
| 40 | * $options[ |
||
| 41 | * 'minify' => true, |
||
| 42 | * 'version' => true, |
||
| 43 | * 'attributes' => ['title' => 'Titre'], |
||
| 44 | * ]; |
||
| 45 | * |
||
| 46 | * @param Builder $builder |
||
| 47 | * @param string $path |
||
| 48 | * @param array|null $options |
||
| 49 | */ |
||
| 50 | public function __construct(Builder $builder, string $path, array $options = null) |
||
| 51 | { |
||
| 52 | $this->builder = $builder; |
||
| 53 | $this->config = $builder->getConfig(); |
||
| 54 | $path = '/'.ltrim($path, '/'); |
||
| 55 | |||
| 56 | if (false === $filePath = $this->findFile($path)) { |
||
| 57 | throw new Exception(sprintf('Asset file "%s" doesn\'t exist.', $path)); |
||
| 58 | } |
||
| 59 | |||
| 60 | $this->pathinfo = pathinfo($path); |
||
| 61 | |||
| 62 | // handles options |
||
| 63 | $minify = (bool) $this->config->get('assets.minify.auto'); |
||
| 64 | $version = (bool) $this->config->get('assets.version.auto'); |
||
| 65 | $attributes = null; |
||
| 66 | extract(is_array($options) ? $options : [], EXTR_IF_EXISTS); |
||
| 67 | |||
| 68 | // set data |
||
| 69 | $this->data['file'] = $filePath; |
||
| 70 | $this->data['path'] = $path; |
||
| 71 | $this->data['ext'] = $this->pathinfo['extension']; |
||
| 72 | $this->data['type'] = explode('/', mime_content_type($filePath))[0]; |
||
| 73 | $this->data['content'] = file_get_contents($filePath); |
||
| 74 | $this->data['attributes'] = $attributes; |
||
| 75 | |||
| 76 | // compiling |
||
| 77 | if ((bool) $this->config->get('assets.sass.auto')) { |
||
| 78 | $this->compile(); |
||
| 79 | } |
||
| 80 | // minifying |
||
| 81 | if ($minify) { |
||
| 82 | $this->minify(); |
||
| 83 | } |
||
| 84 | // versionning |
||
| 85 | if ($version) { |
||
| 86 | $this->version(); |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Returns Asset path. |
||
| 92 | * |
||
| 93 | * @return string |
||
| 94 | */ |
||
| 95 | public function __toString(): string |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Compiles a SCSS. |
||
| 102 | * |
||
| 103 | * @return self |
||
| 104 | */ |
||
| 105 | public function compile(): self |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Minifying a CSS or a JS. |
||
| 154 | * |
||
| 155 | * @return self |
||
| 156 | */ |
||
| 157 | public function minify(): self |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Versions a file. |
||
| 214 | * |
||
| 215 | * @return self |
||
| 216 | */ |
||
| 217 | public function version(): self |
||
| 218 | { |
||
| 219 | if ($this->versioned) { |
||
| 220 | return $this; |
||
| 221 | } |
||
| 222 | |||
| 223 | $data = $this->data; |
||
| 224 | |||
| 225 | $version = $this->builder->time; |
||
| 226 | if ($this->config->get('assets.version.strategy') == 'static') { |
||
| 227 | $version = $this->config->get('assets.version.value'); |
||
| 228 | } |
||
| 229 | |||
| 230 | // dirname/filename.version.ext |
||
| 231 | // ie: /styles.v1.css |
||
| 232 | $this->data['path'] = \sprintf( |
||
| 233 | '%s.%s.%s', |
||
| 234 | Util::joinPath($this->pathinfo['dirname'], $this->pathinfo['filename']), |
||
| 235 | $version, |
||
| 236 | $this->data['ext'] |
||
| 237 | ); |
||
| 238 | |||
| 239 | if ($this->exists($this->data['path'])) { |
||
| 240 | return $this; |
||
| 241 | } |
||
| 242 | |||
| 243 | $this->save($data['path']); |
||
| 244 | |||
| 245 | $this->versioned = true; |
||
| 246 | |||
| 247 | return $this; |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Implements \ArrayAccess. |
||
| 252 | */ |
||
| 253 | public function offsetSet($offset, $value) |
||
| 254 | { |
||
| 255 | if (!is_null($offset)) { |
||
| 256 | $this->data[$offset] = $value; |
||
| 257 | } |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Implements \ArrayAccess. |
||
| 262 | */ |
||
| 263 | public function offsetExists($offset) |
||
| 264 | { |
||
| 265 | return isset($this->data[$offset]); |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Implements \ArrayAccess. |
||
| 270 | */ |
||
| 271 | public function offsetUnset($offset) |
||
| 272 | { |
||
| 273 | unset($this->data[$offset]); |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Implements \ArrayAccess. |
||
| 278 | */ |
||
| 279 | public function offsetGet($offset) |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Try to find a static file (in site or theme(s)) if exists or returns false. |
||
| 286 | * |
||
| 287 | * @param string $path |
||
| 288 | * |
||
| 289 | * @return string|false |
||
| 290 | */ |
||
| 291 | private function findFile(string $path) |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Saves file (and deletes previous file). |
||
| 311 | * |
||
| 312 | * @param string $previousPath |
||
| 313 | * |
||
| 314 | * @return void |
||
| 315 | */ |
||
| 316 | private function save(string $previousPath = null): void |
||
| 317 | { |
||
| 318 | if (!$this->builder->getBuildOptions()['dry-run']) { |
||
| 319 | Util::getFS()->dumpFile(Util::joinFile($this->config->getOutputPath(), $this->data['path']), $this->data['content']); |
||
| 320 | if (!empty($previousPath)) { |
||
| 321 | Util::getFS()->remove(Util::joinFile($this->config->getOutputPath(), $previousPath)); |
||
| 322 | } |
||
| 323 | } |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Checks if file is already saved/writen. |
||
| 328 | * |
||
| 329 | * @param string $path |
||
| 330 | * |
||
| 331 | * @return bool |
||
| 332 | */ |
||
| 333 | private function exists(string $path): bool |
||
| 336 | } |
||
| 337 | } |
||
| 338 |