| Total Complexity | 55 |
| Total Lines | 400 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Complex classes like AssetManager 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 AssetManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class AssetManager |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * @var array<Filesystem> |
||
| 40 | */ |
||
| 41 | protected array $aStorage = []; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Default library URL |
||
| 45 | * |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | private const JS_LIB_URL = 'https://cdn.jsdelivr.net/gh/jaxon-php/[email protected]/dist'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * The constructor |
||
| 52 | * |
||
| 53 | * @param ConfigManager $xConfigManager |
||
| 54 | * @param StorageManager $xStorageManager |
||
| 55 | * @param MinifierInterface $xMinifier |
||
| 56 | */ |
||
| 57 | public function __construct(private ConfigManager $xConfigManager, |
||
| 58 | private StorageManager $xStorageManager, private MinifierInterface $xMinifier) |
||
| 59 | {} |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @return Config |
||
| 63 | */ |
||
| 64 | protected function config(): Config |
||
| 65 | { |
||
| 66 | return $this->xConfigManager->getExportConfig(); |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param string $sAsset "js" or "css" |
||
| 71 | * |
||
| 72 | * @return Filesystem |
||
| 73 | */ |
||
| 74 | protected function _storage(string $sAsset): Filesystem |
||
| 75 | { |
||
| 76 | if($this->config()->hasOption('storage')) |
||
| 77 | { |
||
| 78 | return $this->xStorageManager->get($this->config()->getOption('storage')); |
||
| 79 | } |
||
| 80 | |||
| 81 | $sRootDir = $this->getAssetDir($sAsset); |
||
| 82 | // Fylsystem options: we don't want the root dir to be created if it doesn't exist. |
||
| 83 | $aAdapterOptions = ['lazyRootCreation' => true]; |
||
| 84 | $aDirOptions = [ |
||
| 85 | 'config' => [ |
||
| 86 | 'public_url' => $this->getAssetUri($sAsset), |
||
| 87 | ], |
||
| 88 | ]; |
||
| 89 | return $this->xStorageManager |
||
| 90 | ->adapter('local', $aAdapterOptions) |
||
| 91 | ->make($sRootDir, $aDirOptions); |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param string $sAsset "js" or "css" |
||
| 96 | * |
||
| 97 | * @return Filesystem |
||
| 98 | */ |
||
| 99 | protected function storage(string $sAsset): Filesystem |
||
| 100 | { |
||
| 101 | return $this->aStorage[$sAsset] ??= $this->_storage($sAsset); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param array $aValues |
||
| 106 | * |
||
| 107 | * @return string |
||
| 108 | */ |
||
| 109 | public function makeFileOptions(array $aValues): string |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Get app js options |
||
| 133 | * |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | public function getJsOptions(): string |
||
| 148 | ]); |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Get app js options |
||
| 153 | * |
||
| 154 | * @return string |
||
| 155 | */ |
||
| 156 | public function getCssOptions(): string |
||
| 157 | { |
||
| 158 | return $this->makeFileOptions([ |
||
| 159 | 'options' => $this->config()->getOption('css.options', ''), |
||
| 160 | ]); |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Check if the assets of this plugin shall be included in Jaxon generated code. |
||
| 165 | * |
||
| 166 | * @param Generator|CssGenerator|JsGenerator $xGenerator |
||
| 167 | * |
||
| 168 | * @return bool |
||
| 169 | */ |
||
| 170 | public function shallIncludeAssets(Generator|CssGenerator|JsGenerator $xGenerator): bool |
||
| 171 | { |
||
| 172 | if(!is_subclass_of($xGenerator, AbstractPlugin::class)) |
||
| 173 | { |
||
| 174 | return true; |
||
| 175 | } |
||
| 176 | |||
| 177 | /** @var AbstractPlugin */ |
||
| 178 | $xPlugin = $xGenerator; |
||
| 179 | $sPluginOptionName = 'include.' . $xPlugin->getName(); |
||
| 180 | |||
| 181 | return $this->config()->hasOption($sPluginOptionName) ? |
||
| 182 | $this->config()->getOption($sPluginOptionName) : |
||
| 183 | $this->config()->getOption('include.all', true); |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Get the HTML tags to include Jaxon javascript files into the page |
||
| 188 | * |
||
| 189 | * @return array |
||
| 190 | */ |
||
| 191 | public function getJsLibUrls(): array |
||
| 192 | { |
||
| 193 | $sJsExtension = $this->config()->getOption('minify') ? '.min.js' : '.js'; |
||
| 194 | // The URI for the javascript library files |
||
| 195 | $sJsLibUri = $this->xConfigManager->getOption('js.lib.uri', self::JS_LIB_URL); |
||
| 196 | $sJsLibUri = rtrim($sJsLibUri, '/'); |
||
| 197 | |||
| 198 | // Add component files to the javascript file array. |
||
| 199 | $sChibiUrl = "$sJsLibUri/libs/chibi/chibi$sJsExtension"; |
||
| 200 | $aJsUrls = [ |
||
| 201 | $this->xConfigManager->getOption('js.lib.jq', $sChibiUrl), |
||
| 202 | "$sJsLibUri/jaxon.core$sJsExtension", |
||
| 203 | ]; |
||
| 204 | if($this->xConfigManager->getOption('core.debug.on')) |
||
| 205 | { |
||
| 206 | $sLanguage = $this->xConfigManager->getOption('core.language'); |
||
| 207 | $aJsUrls[] = "$sJsLibUri/jaxon.debug$sJsExtension"; |
||
| 208 | $aJsUrls[] = "$sJsLibUri/lang/jaxon.$sLanguage$sJsExtension"; |
||
| 209 | } |
||
| 210 | |||
| 211 | return $aJsUrls; |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param string $sAsset "js" or "css" |
||
| 216 | * |
||
| 217 | * @return string |
||
| 218 | */ |
||
| 219 | private function getAssetUri(string $sAsset): string |
||
| 220 | { |
||
| 221 | return rtrim($this->config()->hasOption("$sAsset.uri") ? |
||
| 222 | $this->config()->getOption("$sAsset.uri") : |
||
| 223 | $this->config()->getOption('uri', ''), '/'); |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param string $sAsset "js" or "css" |
||
| 228 | * |
||
| 229 | * @return string |
||
| 230 | */ |
||
| 231 | private function getAssetDir(string $sAsset): string |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param Closure $cGetHash |
||
| 240 | * @param string $sAsset "js" or "css" |
||
| 241 | * |
||
| 242 | * @return string |
||
| 243 | */ |
||
| 244 | private function getAssetFile(Closure $cGetHash, string $sAsset): string |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param string $sAsset "js" or "css" |
||
| 255 | * |
||
| 256 | * @return bool |
||
| 257 | */ |
||
| 258 | private function minifyEnabled(string $sAsset): bool |
||
| 259 | { |
||
| 260 | return $this->config()->hasOption("$sAsset.minify") ? |
||
| 261 | $this->config()->getOption("$sAsset.minify") : |
||
| 262 | $this->config()->getOption('minify', false); |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param string $sAsset "js" or "css" |
||
| 267 | * |
||
| 268 | * @return bool |
||
| 269 | */ |
||
| 270 | private function exportEnabled(string $sAsset): bool |
||
| 271 | { |
||
| 272 | return $this->config()->hasOption("$sAsset.export") ? |
||
| 273 | $this->config()->getOption("$sAsset.export") : |
||
| 274 | $this->config()->getOption('export', false); |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @param Filesystem $xStorage |
||
| 279 | * @param string $sFilePath |
||
| 280 | * |
||
| 281 | * @return bool |
||
| 282 | */ |
||
| 283 | private function fileExists(Filesystem $xStorage, string $sFilePath): bool |
||
| 284 | { |
||
| 285 | try |
||
| 286 | { |
||
| 287 | return $xStorage->fileExists($sFilePath); |
||
| 288 | } |
||
| 289 | catch(Throwable $e) |
||
| 290 | { |
||
| 291 | Logger::warning("Unable to check asset file at $sFilePath.", [ |
||
| 292 | 'error' => $e->getMessage(), |
||
| 293 | ]); |
||
| 294 | return false; |
||
| 295 | } |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param Filesystem $xStorage |
||
| 300 | * @param string $sFilePath |
||
| 301 | * @param string $sContent |
||
| 302 | * |
||
| 303 | * @return bool |
||
| 304 | */ |
||
| 305 | private function writeFile(Filesystem $xStorage, string $sFilePath, string $sContent): bool |
||
| 306 | { |
||
| 307 | try |
||
| 308 | { |
||
| 309 | $xStorage->write($sFilePath, $sContent); |
||
| 310 | return true; |
||
| 311 | } |
||
| 312 | catch(Throwable $e) |
||
| 313 | { |
||
| 314 | Logger::warning("Unable to write to asset file at $sFilePath.", [ |
||
| 315 | 'error' => $e->getMessage(), |
||
| 316 | ]); |
||
| 317 | return false; |
||
| 318 | } |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @param string $sAsset "js" or "css" |
||
| 323 | * @param string $sFilePath |
||
| 324 | * @param string $sMinFilePath |
||
| 325 | * |
||
| 326 | * @return bool |
||
| 327 | */ |
||
| 328 | private function minifyAsset(string $sAsset, string $sFilePath, string $sMinFilePath): bool |
||
| 350 | } |
||
| 351 | |||
| 352 | private function getPublicUrl(string $sFilePath, string $sAsset): string |
||
| 353 | { |
||
| 354 | $sUri = $this->getAssetUri($sAsset); |
||
| 355 | return $sUri !== '' ? "$sUri/$sFilePath" : |
||
| 356 | $this->storage($sAsset)->publicUrl($sFilePath); |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Write javascript or css files and return the corresponding URI |
||
| 361 | * |
||
| 362 | * @param Closure $cGetHash |
||
| 363 | * @param Closure $cGetCode |
||
| 364 | * @param string $sAsset "js" or "css" |
||
| 365 | * |
||
| 366 | * @return string |
||
| 367 | */ |
||
| 368 | public function createFiles(Closure $cGetHash, Closure $cGetCode, string $sAsset): string |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Write javascript files and return the corresponding URI |
||
| 412 | * |
||
| 413 | * @param Closure $cGetHash |
||
| 414 | * @param Closure $cGetCode |
||
| 415 | * |
||
| 416 | * @return string |
||
| 417 | */ |
||
| 418 | public function createJsFiles(Closure $cGetHash, Closure $cGetCode): string |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Write javascript files and return the corresponding URI |
||
| 426 | * |
||
| 427 | * @param Closure $cGetHash |
||
| 428 | * @param Closure $cGetCode |
||
| 429 | * |
||
| 430 | * @return string |
||
| 431 | */ |
||
| 432 | public function createCssFiles(Closure $cGetHash, Closure $cGetCode): string |
||
| 436 | } |
||
| 437 | } |
||
| 438 |