| Total Complexity | 61 |
| Total Lines | 371 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Configuration 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 Configuration, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class Configuration |
||
| 10 | { |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Debug option that is passed to Assetic. |
||
| 14 | */ |
||
| 15 | protected bool $debug = false; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Combine option giving the opportunity not to combine the assets in debug mode. |
||
| 19 | */ |
||
| 20 | protected bool $combine = true; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Should build assets on request. |
||
| 24 | */ |
||
| 25 | protected bool $buildOnRequest = true; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Full path to public directory where assets will be generated. |
||
| 29 | */ |
||
| 30 | protected ?string $webPath = null; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Full path to cache directory. |
||
| 34 | */ |
||
| 35 | protected ?string $cachePath = null; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Is cache enabled. |
||
| 39 | */ |
||
| 40 | protected bool $cacheEnabled = false; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The base url. |
||
| 44 | * |
||
| 45 | * By default this value is set from "\Laminas\Http\PhpEnvironment\Request::getBaseUrl()" |
||
| 46 | * |
||
| 47 | * Example: |
||
| 48 | * <code> |
||
| 49 | * http://example.com/ |
||
| 50 | * </code> |
||
| 51 | */ |
||
| 52 | protected ?string $baseUrl = null; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The base path. |
||
| 56 | * |
||
| 57 | * By default this value is set from "\Laminas\Http\PhpEnvironment\Request::getBasePath()" |
||
| 58 | * |
||
| 59 | * Example: |
||
| 60 | * <code> |
||
| 61 | * <baseUrl>/~jdo/ |
||
| 62 | * </code> |
||
| 63 | */ |
||
| 64 | protected ?string $basePath = null; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Asset will be save on disk, only when it's modification time was changed |
||
| 68 | */ |
||
| 69 | protected bool $writeIfChanged = true; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Default options. |
||
| 73 | */ |
||
| 74 | protected array $default = [ |
||
| 75 | 'assets' => [], |
||
| 76 | 'options' => [], |
||
| 77 | ]; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Map of routes names and assets configuration. |
||
| 81 | */ |
||
| 82 | protected array $routes = []; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Map of modules names and assets configuration. |
||
| 86 | */ |
||
| 87 | protected array $modules = []; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Map of controllers names and assets configuration. |
||
| 91 | */ |
||
| 92 | protected array $controllers = []; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Map of strategies that will be choose to setup Assetic\AssetInterface |
||
| 96 | * for particular Laminas\View\Renderer\RendererInterface |
||
| 97 | */ |
||
| 98 | protected array $rendererToStrategy = []; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * List of error types occurring in EVENT_DISPATCH_ERROR that will use |
||
| 102 | * this module to render assets. |
||
| 103 | */ |
||
| 104 | protected array $acceptableErrors = []; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Umask |
||
| 108 | */ |
||
| 109 | protected ?int $umask = null; |
||
| 110 | |||
| 111 | public function __construct(?iterable $config = null) |
||
| 112 | { |
||
| 113 | if (null !== $config) { |
||
| 114 | if ($config instanceof \Traversable) { |
||
| 115 | $this->processArray(Stdlib\ArrayUtils::iteratorToArray($config)); |
||
| 116 | } else { |
||
| 117 | $this->processArray($config); |
||
| 118 | } |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | public function isDebug(): bool |
||
| 123 | { |
||
| 124 | return $this->debug; |
||
| 125 | } |
||
| 126 | |||
| 127 | public function setDebug(bool $flag): void |
||
| 128 | { |
||
| 129 | $this->debug = $flag; |
||
| 130 | } |
||
| 131 | |||
| 132 | public function isCombine(): bool |
||
| 133 | { |
||
| 134 | return $this->combine; |
||
| 135 | } |
||
| 136 | |||
| 137 | public function setCombine(bool $flag): void |
||
| 138 | { |
||
| 139 | $this->combine = $flag; |
||
| 140 | } |
||
| 141 | |||
| 142 | public function setWebPath(?string $path): void |
||
| 143 | { |
||
| 144 | $this->webPath = $path; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @throws Exception\RuntimeException |
||
| 149 | */ |
||
| 150 | public function getWebPath(string $file = null): ?string |
||
| 151 | { |
||
| 152 | if (null === $this->webPath) { |
||
| 153 | throw new Exception\RuntimeException('Web path is not set'); |
||
| 154 | } |
||
| 155 | |||
| 156 | if (null !== $file) { |
||
| 157 | return rtrim($this->webPath, '/\\') . '/' . ltrim($file, '/\\'); |
||
| 158 | } |
||
| 159 | |||
| 160 | return $this->webPath; |
||
| 161 | } |
||
| 162 | |||
| 163 | public function setCachePath(?string $path): void |
||
| 164 | { |
||
| 165 | $this->cachePath = $path; |
||
| 166 | } |
||
| 167 | |||
| 168 | public function getCachePath(): ?string |
||
| 169 | { |
||
| 170 | return $this->cachePath; |
||
| 171 | } |
||
| 172 | |||
| 173 | public function setCacheEnabled(bool $cacheEnabled): void |
||
| 174 | { |
||
| 175 | $this->cacheEnabled = $cacheEnabled; |
||
| 176 | } |
||
| 177 | |||
| 178 | public function getCacheEnabled(): bool |
||
| 179 | { |
||
| 180 | return $this->cacheEnabled; |
||
| 181 | } |
||
| 182 | |||
| 183 | public function setDefault(array $default): void |
||
| 184 | { |
||
| 185 | if (!isset($default['assets'])) { |
||
| 186 | $default['assets'] = []; |
||
| 187 | } |
||
| 188 | |||
| 189 | if (!isset($default['options'])) { |
||
| 190 | $default['options'] = []; |
||
| 191 | } |
||
| 192 | |||
| 193 | $this->default = $default; |
||
| 194 | } |
||
| 195 | |||
| 196 | public function getDefault(): array |
||
| 197 | { |
||
| 198 | return $this->default; |
||
| 199 | } |
||
| 200 | |||
| 201 | public function setRoutes(array $routes): void |
||
| 202 | { |
||
| 203 | $this->routes = $routes; |
||
| 204 | } |
||
| 205 | |||
| 206 | public function getRoutes(): array |
||
| 207 | { |
||
| 208 | return $this->routes; |
||
| 209 | } |
||
| 210 | |||
| 211 | public function getRoute(string $name, array $default = null): ?array |
||
| 226 | } |
||
| 227 | |||
| 228 | public function setControllers(array $controllers): void |
||
| 229 | { |
||
| 230 | $this->controllers = $controllers; |
||
| 231 | } |
||
| 232 | |||
| 233 | public function getControllers(): array |
||
| 234 | { |
||
| 235 | return $this->controllers; |
||
| 236 | } |
||
| 237 | |||
| 238 | public function getController(string $name, array $default = null): ?array |
||
| 239 | { |
||
| 240 | return array_key_exists($name, $this->controllers) ? $this->controllers[$name] : $default; |
||
| 241 | } |
||
| 242 | |||
| 243 | public function setModules(array $modules): void |
||
| 244 | { |
||
| 245 | $this->modules = []; |
||
| 246 | foreach ($modules as $name => $options) { |
||
| 247 | $this->addModule($name, $options); |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | public function addModule(string $name, array $options): void |
||
| 252 | { |
||
| 253 | $lowername = strtolower($name); |
||
| 254 | $this->modules[$lowername] = $options; |
||
| 255 | } |
||
| 256 | |||
| 257 | public function getModules(): array |
||
| 258 | { |
||
| 259 | return $this->modules; |
||
| 260 | } |
||
| 261 | |||
| 262 | public function getModule(string $name, array $default = null): ?array |
||
| 263 | { |
||
| 264 | $lowername = strtolower($name); |
||
| 265 | return array_key_exists($lowername, $this->modules) ? $this->modules[$lowername] : $default; |
||
| 266 | } |
||
| 267 | |||
| 268 | public function detectBaseUrl(): bool |
||
| 269 | { |
||
| 270 | return (null === $this->baseUrl || 'auto' === $this->baseUrl); |
||
| 271 | } |
||
| 272 | |||
| 273 | public function setBaseUrl(?string $baseUrl): void |
||
| 274 | { |
||
| 275 | if (null !== $baseUrl && 'auto' !== $baseUrl) { |
||
| 276 | $baseUrl = rtrim($baseUrl, '/') . '/'; |
||
| 277 | } |
||
| 278 | $this->baseUrl = $baseUrl; |
||
| 279 | } |
||
| 280 | |||
| 281 | public function getBaseUrl(): ?string |
||
| 282 | { |
||
| 283 | return $this->baseUrl; |
||
| 284 | } |
||
| 285 | |||
| 286 | public function setBasePath(?string $basePath): void |
||
| 287 | { |
||
| 288 | if (null !== $basePath) { |
||
| 289 | $basePath = trim($basePath, '/'); |
||
| 290 | $basePath = $basePath . '/'; |
||
| 291 | } |
||
| 292 | $this->basePath = $basePath; |
||
| 293 | } |
||
| 294 | |||
| 295 | public function getBasePath(): ?string |
||
| 298 | } |
||
| 299 | |||
| 300 | protected function processArray(iterable $config): void |
||
| 301 | { |
||
| 302 | foreach ($config as $key => $value) { |
||
| 303 | $setter = $this->assembleSetterNameFromConfigKey($key); |
||
| 304 | $this->{$setter}($value); |
||
| 305 | } |
||
| 306 | } |
||
| 307 | |||
| 308 | protected function assembleSetterNameFromConfigKey(string $key): string |
||
| 309 | { |
||
| 310 | $parts = explode('_', $key); |
||
| 311 | $parts = array_map('ucfirst', $parts); |
||
| 312 | $setter = 'set' . implode('', $parts); |
||
| 313 | if (!method_exists($this, $setter)) { |
||
| 314 | throw new Exception\BadMethodCallException( |
||
| 315 | 'The configuration key "' . $key . '" does not ' |
||
| 316 | . 'have a matching ' . $setter . ' setter method ' |
||
| 317 | . 'which must be defined' |
||
| 318 | ); |
||
| 319 | } |
||
| 320 | |||
| 321 | return $setter; |
||
| 322 | } |
||
| 323 | |||
| 324 | public function setRendererToStrategy(array $strategyForRenderer): void |
||
| 325 | { |
||
| 326 | $this->rendererToStrategy = $strategyForRenderer; |
||
| 327 | } |
||
| 328 | |||
| 329 | public function addRendererToStrategy(string $rendererClass, string $strategyClass): void |
||
| 330 | { |
||
| 331 | $this->rendererToStrategy[$rendererClass] = $strategyClass; |
||
| 332 | } |
||
| 333 | |||
| 334 | public function getStrategyNameForRenderer(string $rendererName, string $default = null): ?string |
||
| 335 | { |
||
| 336 | return array_key_exists($rendererName, $this->rendererToStrategy) ? $this->rendererToStrategy[$rendererName] : $default; |
||
| 337 | } |
||
| 338 | |||
| 339 | public function setAcceptableErrors(array $acceptableErrors): void |
||
| 340 | { |
||
| 341 | $this->acceptableErrors = $acceptableErrors; |
||
| 342 | } |
||
| 343 | |||
| 344 | public function getAcceptableErrors(): array |
||
| 345 | { |
||
| 346 | return $this->acceptableErrors; |
||
| 347 | } |
||
| 348 | |||
| 349 | public function getUmask(): ?int |
||
| 350 | { |
||
| 351 | return $this->umask; |
||
| 352 | } |
||
| 353 | |||
| 354 | public function setUmask(?int $umask): void |
||
| 359 | } |
||
| 360 | } |
||
| 361 | |||
| 362 | public function setBuildOnRequest(bool $flag): void |
||
| 363 | { |
||
| 364 | $this->buildOnRequest = (bool) $flag; |
||
| 365 | } |
||
| 366 | |||
| 367 | public function getBuildOnRequest(): bool |
||
| 368 | { |
||
| 369 | return $this->buildOnRequest; |
||
| 370 | } |
||
| 371 | |||
| 372 | public function setWriteIfChanged(bool $flag): void |
||
| 375 | } |
||
| 376 | |||
| 377 | public function getWriteIfChanged(): bool |
||
| 378 | { |
||
| 379 | return $this->writeIfChanged; |
||
| 380 | } |
||
| 381 | |||
| 382 | } |
||
| 383 |