| Total Complexity | 60 |
| Total Lines | 418 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | 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 |
||
| 21 | class Asset implements \ArrayAccess |
||
| 22 | { |
||
| 23 | /** @var Builder */ |
||
| 24 | protected $builder; |
||
| 25 | /** @var Config */ |
||
| 26 | protected $config; |
||
| 27 | /** @var array */ |
||
| 28 | protected $data = []; |
||
| 29 | /** @var bool */ |
||
| 30 | protected $fingerprinted = false; |
||
| 31 | /** @var bool */ |
||
| 32 | protected $compiled = false; |
||
| 33 | /** @var bool */ |
||
| 34 | protected $minified = false; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Creates an Asset from file. |
||
| 38 | * |
||
| 39 | * $options[ |
||
| 40 | * 'fingerprint' => false, |
||
| 41 | * 'minify' => false, |
||
| 42 | * ]; |
||
| 43 | * |
||
| 44 | * @param Builder $builder |
||
| 45 | * @param string|array $path |
||
| 46 | * @param array|null $options |
||
| 47 | */ |
||
| 48 | public function __construct(Builder $builder, $path, array $options = null) |
||
| 49 | { |
||
| 50 | $this->builder = $builder; |
||
| 51 | $this->config = $builder->getConfig(); |
||
| 52 | $path = is_array($path) ? $path : [$path]; |
||
| 53 | |||
| 54 | // handles options |
||
| 55 | $fingerprint = (bool) $this->config->get('assets.fingerprint.enabled'); |
||
| 56 | $minify = (bool) $this->config->get('assets.minify.enabled'); |
||
| 57 | $filename = ''; |
||
| 58 | extract(is_array($options) ? $options : [], EXTR_IF_EXISTS); |
||
| 59 | |||
| 60 | // load file(s) |
||
| 61 | $prevType = ''; |
||
| 62 | $prevExt = ''; |
||
| 63 | foreach ($path as $p) { |
||
| 64 | $file = $this->loadFile($p); |
||
| 65 | |||
| 66 | // same type only |
||
| 67 | if (!empty($prevType) && $prevType != $file['type']) { |
||
| 68 | throw new Exception(\sprintf('Asset bundle works only for files with the same type.')); |
||
| 69 | } |
||
| 70 | // same extension only |
||
| 71 | if (!empty($prevExt) && $prevExt != $file['ext']) { |
||
| 72 | throw new Exception(\sprintf('Asset bundle works only for files with the same extension.')); |
||
| 73 | } |
||
| 74 | |||
| 75 | // set data |
||
| 76 | $this->data['file'] = $file['filepath']; |
||
| 77 | $this->data['path'] = $file['path']; |
||
| 78 | $this->data['ext'] = $file['ext']; |
||
| 79 | $this->data['type'] = $file['type']; |
||
| 80 | $this->data['subtype'] = $file['subtype']; |
||
| 81 | $this->data['size'] = $file['size']; |
||
| 82 | $this->data['source'] = $file['content']; |
||
| 83 | $this->data['content'] .= $file['content']; |
||
| 84 | |||
| 85 | $prevType = $file['type']; |
||
| 86 | $prevExt = $file['ext']; |
||
| 87 | } |
||
| 88 | // path to the bundled file |
||
| 89 | if (count($path) > 1) { |
||
| 90 | $this->data['path'] = $filename; |
||
| 91 | if (empty($filename)) { |
||
| 92 | switch ($this->data['ext']) { |
||
| 93 | case 'scss': |
||
| 94 | case 'css': |
||
| 95 | $this->data['path'] = 'styles.'.$file['ext']; |
||
| 96 | break; |
||
| 97 | case 'js': |
||
| 98 | $this->data['path'] = 'scripts.'.$file['ext']; |
||
| 99 | break; |
||
| 100 | default: |
||
| 101 | throw new Exception(\sprintf('Asset bundle supports "%s" files only.', 'scss, css and js')); |
||
| 102 | break; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | // fingerprinting |
||
| 108 | if ($fingerprint) { |
||
| 109 | $this->fingerprint(); |
||
| 110 | } |
||
| 111 | // compiling |
||
| 112 | if ((bool) $this->config->get('assets.compile.enabled')) { |
||
| 113 | $this->compile(); |
||
| 114 | } |
||
| 115 | // minifying |
||
| 116 | if ($minify) { |
||
| 117 | $this->minify(); |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Returns Asset path. |
||
| 123 | * |
||
| 124 | * @return string |
||
| 125 | */ |
||
| 126 | public function __toString(): string |
||
| 127 | { |
||
| 128 | return $this->data['path']; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Fingerprints a file. |
||
| 133 | * |
||
| 134 | * @return self |
||
| 135 | */ |
||
| 136 | public function fingerprint(): self |
||
| 137 | { |
||
| 138 | if ($this->fingerprinted) { |
||
| 139 | return $this; |
||
| 140 | } |
||
| 141 | |||
| 142 | $fingerprint = hash('md5', $this->data['source']); |
||
| 143 | $this->data['path'] = preg_replace( |
||
| 144 | '/\.'.$this->data['ext'].'$/m', |
||
| 145 | ".$fingerprint.".$this->data['ext'], |
||
| 146 | $this->data['path'] |
||
| 147 | ); |
||
| 148 | |||
| 149 | $this->fingerprinted = true; |
||
| 150 | |||
| 151 | return $this; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Compiles a SCSS. |
||
| 156 | * |
||
| 157 | * @return self |
||
| 158 | */ |
||
| 159 | public function compile(): self |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Minifying a CSS or a JS. |
||
| 208 | * |
||
| 209 | * @return self |
||
| 210 | */ |
||
| 211 | public function minify(): self |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Implements \ArrayAccess. |
||
| 260 | */ |
||
| 261 | public function offsetSet($offset, $value) |
||
| 262 | { |
||
| 263 | if (!is_null($offset)) { |
||
| 264 | $this->data[$offset] = $value; |
||
| 265 | } |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Implements \ArrayAccess. |
||
| 270 | */ |
||
| 271 | public function offsetExists($offset) |
||
| 272 | { |
||
| 273 | return isset($this->data[$offset]); |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Implements \ArrayAccess. |
||
| 278 | */ |
||
| 279 | public function offsetUnset($offset) |
||
| 280 | { |
||
| 281 | unset($this->data[$offset]); |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Implements \ArrayAccess. |
||
| 286 | */ |
||
| 287 | public function offsetGet($offset) |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Hashing content of an asset with the specified algo, sha384 by default. |
||
| 294 | * Used for SRI (Subresource Integrity). |
||
| 295 | * |
||
| 296 | * @see https://developer.mozilla.org/fr/docs/Web/Security/Subresource_Integrity |
||
| 297 | * |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | public function getIntegrity(string $algo = 'sha384'): string |
||
| 301 | { |
||
| 302 | return \sprintf('%s-%s', $algo, base64_encode(hash($algo, $this->data['content'], true))); |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Returns the width of an image. |
||
| 307 | * |
||
| 308 | * @return false|int |
||
| 309 | */ |
||
| 310 | public function getWidth() |
||
| 311 | { |
||
| 312 | if (false === $size = $this->getImageSize()) { |
||
| 313 | return false; |
||
| 314 | } |
||
| 315 | |||
| 316 | return $size[0]; |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Returns the height of an image. |
||
| 321 | * |
||
| 322 | * @return false|int |
||
| 323 | */ |
||
| 324 | public function getHeight() |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Returns MPF3 file infos. |
||
| 335 | * |
||
| 336 | * @see https://github.com/wapmorgan/Mp3Info |
||
| 337 | * |
||
| 338 | * @return Mp3Info |
||
| 339 | */ |
||
| 340 | public function getAudio(): Mp3Info |
||
| 341 | { |
||
| 342 | return new Mp3Info($this->data['file']); |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Saves file. |
||
| 347 | * Note: a file from `static/` with the same name will be overridden. |
||
| 348 | * |
||
| 349 | * @throws Exception |
||
| 350 | * |
||
| 351 | * @return void |
||
| 352 | */ |
||
| 353 | public function save(): void |
||
| 354 | { |
||
| 355 | $file = Util::joinFile($this->config->getOutputPath(), $this->data['path']); |
||
| 356 | if (!$this->builder->getBuildOptions()['dry-run']) { |
||
| 357 | try { |
||
| 358 | Util::getFS()->dumpFile($file, $this->data['content']); |
||
| 359 | } catch (\Symfony\Component\Filesystem\Exception\IOException $e) { |
||
| 360 | throw new Exception(\sprintf('Can\'t save asset "%s"', $this->data['path'])); |
||
| 361 | } |
||
| 362 | } |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Load file data. |
||
| 367 | * |
||
| 368 | * @param string $path |
||
| 369 | * |
||
| 370 | * @return array |
||
| 371 | */ |
||
| 372 | private function loadFile(string $path): array |
||
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Try to find a static file (in site or theme(s)) if exists or returns false. |
||
| 398 | * |
||
| 399 | * @param string $path |
||
| 400 | * |
||
| 401 | * @return string|false |
||
| 402 | */ |
||
| 403 | private function findFile(string $path) |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Returns image size informations. |
||
| 423 | * |
||
| 424 | * See https://www.php.net/manual/function.getimagesize.php |
||
| 425 | * |
||
| 426 | * @return false|array |
||
| 427 | */ |
||
| 428 | private function getImageSize() |
||
| 439 | } |
||
| 440 | } |
||
| 441 |