Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like FOSHttpCacheExtension 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 FOSHttpCacheExtension, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class FOSHttpCacheExtension extends Extension |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * {@inheritdoc} |
||
| 33 | */ |
||
| 34 | 22 | public function getConfiguration(array $config, ContainerBuilder $container) |
|
| 38 | |||
| 39 | /** |
||
| 40 | * {@inheritdoc} |
||
| 41 | */ |
||
| 42 | 22 | public function load(array $configs, ContainerBuilder $container) |
|
| 43 | { |
||
| 44 | 22 | $configuration = $this->getConfiguration($configs, $container); |
|
| 45 | 22 | $config = $this->processConfiguration($configuration, $configs); |
|
|
|
|||
| 46 | |||
| 47 | 22 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
| 48 | 22 | $loader->load('matcher.xml'); |
|
| 49 | |||
| 50 | 22 | if ($config['debug']['enabled'] || (!empty($config['cache_control']))) { |
|
| 51 | 3 | $debugHeader = $config['debug']['enabled'] ? $config['debug']['header'] : false; |
|
| 52 | 3 | $container->setParameter($this->getAlias().'.debug_header', $debugHeader); |
|
| 53 | 3 | $loader->load('cache_control_listener.xml'); |
|
| 54 | } |
||
| 55 | |||
| 56 | 22 | if (!empty($config['cache_control'])) { |
|
| 57 | 3 | $this->loadCacheControl($container, $config['cache_control']); |
|
| 58 | } |
||
| 59 | |||
| 60 | 22 | if (isset($config['proxy_client'])) { |
|
| 61 | 16 | $this->loadProxyClient($container, $loader, $config['proxy_client']); |
|
| 62 | } |
||
| 63 | |||
| 64 | 21 | if (isset($config['test'])) { |
|
| 65 | 1 | $this->loadTest($container, $loader, $config['test']); |
|
| 66 | } |
||
| 67 | |||
| 68 | 21 | if ($config['cache_manager']['enabled']) { |
|
| 69 | 16 | if (array_key_exists('custom_proxy_client', $config['cache_manager'])) { |
|
| 70 | // overwrite the previously set alias, if a proxy client was also configured |
||
| 71 | 1 | $container->setAlias( |
|
| 72 | 1 | $this->getAlias().'.default_proxy_client', |
|
| 73 | 1 | $config['cache_manager']['custom_proxy_client'] |
|
| 74 | ); |
||
| 75 | } |
||
| 76 | 16 | if ('auto' === $config['cache_manager']['generate_url_type']) { |
|
| 77 | 16 | if (array_key_exists('custom_proxy_client', $config['cache_manager'])) { |
|
| 78 | 1 | $generateUrlType = UrlGeneratorInterface::ABSOLUTE_URL; |
|
| 79 | } else { |
||
| 80 | 15 | $defaultClient = $this->getDefaultProxyClient($config['proxy_client']); |
|
| 81 | 15 | $generateUrlType = array_key_exists('base_url', $config['proxy_client'][$defaultClient]) |
|
| 82 | ? UrlGeneratorInterface::ABSOLUTE_PATH |
||
| 83 | 16 | : UrlGeneratorInterface::ABSOLUTE_URL |
|
| 84 | ; |
||
| 85 | } |
||
| 86 | } else { |
||
| 87 | $generateUrlType = $config['cache_manager']['generate_url_type']; |
||
| 88 | } |
||
| 89 | 16 | $container->setParameter($this->getAlias().'.cache_manager.generate_url_type', $generateUrlType); |
|
| 90 | 16 | $loader->load('cache_manager.xml'); |
|
| 91 | } |
||
| 92 | |||
| 93 | 21 | if ($config['tags']['enabled']) { |
|
| 94 | 16 | $this->loadCacheTagging( |
|
| 95 | $container, |
||
| 96 | $loader, |
||
| 97 | 16 | $config['tags'], |
|
| 98 | 16 | array_key_exists('proxy_client', $config) |
|
| 99 | 15 | ? $this->getDefaultProxyClient($config['proxy_client']) |
|
| 100 | 16 | : 'custom' |
|
| 101 | ); |
||
| 102 | } else { |
||
| 103 | 5 | $container->setParameter($this->getAlias().'.compiler_pass.tag_annotations', false); |
|
| 104 | } |
||
| 105 | |||
| 106 | 20 | if ($config['invalidation']['enabled']) { |
|
| 107 | 15 | $loader->load('invalidation_listener.xml'); |
|
| 108 | |||
| 109 | 15 | if (!empty($config['invalidation']['expression_language'])) { |
|
| 110 | $container->setAlias( |
||
| 111 | $this->getAlias().'.invalidation.expression_language', |
||
| 112 | $config['invalidation']['expression_language'] |
||
| 113 | ); |
||
| 114 | } |
||
| 115 | |||
| 116 | 15 | if (!empty($config['invalidation']['rules'])) { |
|
| 117 | 2 | $this->loadInvalidatorRules($container, $config['invalidation']['rules']); |
|
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | 20 | if ($config['user_context']['enabled']) { |
|
| 122 | 4 | $this->loadUserContext($container, $loader, $config['user_context']); |
|
| 123 | } |
||
| 124 | |||
| 125 | 20 | if (!empty($config['flash_message']) && $config['flash_message']['enabled']) { |
|
| 126 | 1 | $container->setParameter($this->getAlias().'.event_listener.flash_message.options', $config['flash_message']); |
|
| 127 | |||
| 128 | 1 | $loader->load('flash_message.xml'); |
|
| 129 | } |
||
| 130 | 20 | } |
|
| 131 | |||
| 132 | /** |
||
| 133 | * @param ContainerBuilder $container |
||
| 134 | * @param array $config |
||
| 135 | * |
||
| 136 | * @throws InvalidConfigurationException |
||
| 137 | */ |
||
| 138 | 3 | private function loadCacheControl(ContainerBuilder $container, array $config) |
|
| 152 | |||
| 153 | 5 | private function parseRuleMatcher(ContainerBuilder $container, array $match) |
|
| 179 | |||
| 180 | 5 | private function createRuleMatcher(ContainerBuilder $container, Reference $requestMatcher, array $extraCriteria) |
|
| 196 | |||
| 197 | 4 | private function loadUserContext(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 229 | |||
| 230 | 5 | private function createRequestMatcher(ContainerBuilder $container, $path = null, $host = null, $methods = null, $ips = null, array $attributes = []) |
|
| 245 | |||
| 246 | 16 | private function loadProxyClient(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 247 | { |
||
| 248 | 16 | if (isset($config['varnish'])) { |
|
| 249 | 13 | $this->loadVarnish($container, $loader, $config['varnish']); |
|
| 250 | } |
||
| 251 | 15 | if (isset($config['nginx'])) { |
|
| 252 | 2 | $this->loadNginx($container, $loader, $config['nginx']); |
|
| 253 | } |
||
| 254 | 15 | if (isset($config['symfony'])) { |
|
| 255 | 1 | $this->loadSymfony($container, $loader, $config['symfony']); |
|
| 256 | } |
||
| 257 | |||
| 258 | 15 | $container->setAlias( |
|
| 259 | 15 | $this->getAlias().'.default_proxy_client', |
|
| 260 | 15 | $this->getAlias().'.proxy_client.'.$this->getDefaultProxyClient($config) |
|
| 261 | ); |
||
| 262 | 15 | } |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Define the http dispatcher service for the proxy client $name. |
||
| 266 | * |
||
| 267 | * @param ContainerBuilder $container |
||
| 268 | * @param array $config |
||
| 269 | * @param string $serviceName |
||
| 270 | */ |
||
| 271 | 16 | private function createHttpDispatcherDefinition(ContainerBuilder $container, array $config, $serviceName) |
|
| 272 | { |
||
| 273 | 16 | foreach ($config['servers'] as $url) { |
|
| 274 | 16 | $this->validateUrl($url, 'Not a valid Varnish server address: "%s"'); |
|
| 275 | } |
||
| 276 | 16 | if (!empty($config['base_url'])) { |
|
| 277 | 16 | $baseUrl = $this->prefixSchema($config['base_url']); |
|
| 278 | 16 | $this->validateUrl($baseUrl, 'Not a valid base path: "%s"'); |
|
| 279 | } else { |
||
| 280 | $baseUrl = null; |
||
| 281 | } |
||
| 282 | 15 | $httpClient = null; |
|
| 283 | 15 | if ($config['http_client']) { |
|
| 284 | 1 | $httpClient = new Reference($config['http_client']); |
|
| 285 | } |
||
| 286 | |||
| 287 | 15 | $definition = new Definition(HttpDispatcher::class, [ |
|
| 288 | 15 | $config['servers'], |
|
| 289 | 15 | $baseUrl, |
|
| 290 | 15 | $httpClient, |
|
| 291 | ]); |
||
| 292 | |||
| 293 | 15 | $container->setDefinition($serviceName, $definition); |
|
| 294 | 15 | } |
|
| 295 | |||
| 296 | 13 | private function loadVarnish(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 301 | |||
| 302 | 2 | private function loadNginx(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 303 | { |
||
| 304 | 2 | $this->createHttpDispatcherDefinition($container, $config['http'], $this->getAlias().'.proxy_client.nginx.http_dispatcher'); |
|
| 305 | 2 | $container->setParameter($this->getAlias().'.proxy_client.nginx.options', [ |
|
| 306 | 2 | 'purge_location' => $config['purge_location'], |
|
| 307 | ]); |
||
| 308 | 2 | $loader->load('nginx.xml'); |
|
| 309 | 2 | } |
|
| 310 | |||
| 311 | 1 | private function loadSymfony(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 312 | { |
||
| 313 | 1 | $this->createHttpDispatcherDefinition($container, $config['http'], $this->getAlias().'.proxy_client.symfony.http_dispatcher'); |
|
| 314 | 1 | $loader->load('symfony.xml'); |
|
| 315 | 1 | } |
|
| 316 | |||
| 317 | /** |
||
| 318 | * @param ContainerBuilder $container |
||
| 319 | * @param XmlFileLoader $loader |
||
| 320 | * @param array $config Configuration section for the tags node |
||
| 321 | * @param string $client Name of the client used with the cache manager, |
||
| 322 | * "custom" when a custom client is used |
||
| 323 | */ |
||
| 324 | 16 | private function loadCacheTagging(ContainerBuilder $container, XmlFileLoader $loader, array $config, $client) |
|
| 325 | { |
||
| 326 | 16 | if ('auto' === $config['enabled'] && 'varnish' !== $client) { |
|
| 327 | 3 | $container->setParameter($this->getAlias().'.compiler_pass.tag_annotations', false); |
|
| 328 | |||
| 329 | 3 | return; |
|
| 330 | } |
||
| 331 | 13 | if (!in_array($client, ['varnish', 'custom'])) { |
|
| 332 | 1 | throw new InvalidConfigurationException(sprintf('You can not enable cache tagging with the %s client', $client)); |
|
| 333 | } |
||
| 334 | |||
| 335 | 12 | $container->setParameter($this->getAlias().'.compiler_pass.tag_annotations', true); |
|
| 336 | 12 | $container->setParameter($this->getAlias().'.tag_handler.header', $config['header']); |
|
| 337 | 12 | $loader->load('cache_tagging.xml'); |
|
| 338 | |||
| 339 | 12 | if (!empty($config['expression_language'])) { |
|
| 340 | $container->setAlias( |
||
| 341 | $this->getAlias().'.tag_handler.expression_language', |
||
| 342 | $config['expression_language'] |
||
| 343 | ); |
||
| 344 | } |
||
| 345 | |||
| 346 | 12 | if (!empty($config['rules'])) { |
|
| 347 | 2 | $this->loadTagRules($container, $config['rules']); |
|
| 348 | } |
||
| 349 | 12 | } |
|
| 350 | |||
| 351 | 1 | private function loadTest(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 359 | |||
| 360 | 1 | private function loadProxyServer(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 375 | |||
| 376 | 1 | private function loadVarnishProxyServer(ContainerBuilder $container, XmlFileLoader $loader, $config) |
|
| 386 | |||
| 387 | private function loadNginxProxyServer(ContainerBuilder $container, XmlFileLoader $loader, $config) |
||
| 388 | { |
||
| 389 | $loader->load('nginx_proxy.xml'); |
||
| 390 | foreach ($config as $key => $value) { |
||
| 391 | $container->setParameter( |
||
| 392 | $this->getAlias().'.test.proxy_server.nginx.'.$key, |
||
| 393 | $value |
||
| 394 | ); |
||
| 395 | } |
||
| 396 | } |
||
| 397 | |||
| 398 | 2 | private function loadTagRules(ContainerBuilder $container, array $config) |
|
| 413 | |||
| 414 | 2 | private function loadInvalidatorRules(ContainerBuilder $container, array $config) |
|
| 423 | |||
| 424 | 16 | private function validateUrl($url, $msg) |
|
| 425 | { |
||
| 426 | 16 | $prefixed = $this->prefixSchema($url); |
|
| 427 | |||
| 428 | 16 | if (!$parts = parse_url($prefixed)) { |
|
| 429 | 1 | throw new InvalidConfigurationException(sprintf($msg, $url)); |
|
| 430 | } |
||
| 431 | 16 | } |
|
| 432 | |||
| 433 | 16 | private function prefixSchema($url) |
|
| 441 | |||
| 442 | 15 | private function getDefaultProxyClient(array $config) |
|
| 443 | { |
||
| 444 | 15 | if (isset($config['default'])) { |
|
| 445 | return $config['default']; |
||
| 446 | } |
||
| 447 | |||
| 448 | 15 | if (isset($config['varnish'])) { |
|
| 449 | 12 | return 'varnish'; |
|
| 462 | } |
||
| 463 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: