ZenstruckCacheExtension   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 83
Duplicated Lines 65.06 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 14
c 6
b 0
f 0
lcom 0
cbo 6
dl 54
loc 83
ccs 40
cts 40
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A loadInternal() 0 13 2
B configureHttpClient() 27 27 6
B configureMessageFactory() 27 27 6

How to fix   Duplicated Code   

Duplicated Code

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:

1
<?php
2
3
namespace Zenstruck\CacheBundle\DependencyInjection;
4
5
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
6
use Symfony\Component\Config\FileLocator;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Definition;
9
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
10
use Symfony\Component\DependencyInjection\Loader;
11
12
/**
13
 * @author Kevin Bond <[email protected]>
14
 */
15
class ZenstruckCacheExtension extends ConfigurableExtension
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20 15
    protected function loadInternal(array $mergedConfig, ContainerBuilder $container)
21
    {
22 9
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
23 9
        $loader->load('services.xml');
24
25 12
        $this->configureHttpClient($mergedConfig['http_client'], $container);
26 9
        $this->configureMessageFactory($mergedConfig['message_factory'], $container);
27
28 6
        if ($mergedConfig['sitemap_provider']['enabled']) {
29 1
            $container->setParameter('zenstruck_cache.sitemap_provider.sitemaps', $mergedConfig['sitemap_provider']['sitemaps']);
30 1
            $loader->load('sitemap_provider.xml');
31 1
        }
32 9
    }
33
34
    /**
35
     * @param string           $httpClient
36
     * @param ContainerBuilder $container
37
     */
38 17 View Code Duplication
    private function configureHttpClient($httpClient, ContainerBuilder $container)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
    {
40 9
        if (!class_exists($httpClient)) {
41
            // is a service
42 5
            $container->setAlias('zenstruck_cache.http_client', $httpClient);
43
44 10
            return;
45
        }
46
47 4
        $r = new \ReflectionClass($httpClient);
48
49 4
        if (!$r->implementsInterface('Http\Client\HttpClient')) {
50 1
            throw new InvalidConfigurationException('HttpClient class must implement "Http\Client\HttpClient".');
51
        }
52
53 3
        if ($r->isAbstract()) {
54 1
            throw new InvalidConfigurationException('HttpClient class must not be abstract.');
55
        }
56
57 2
        if (null !== $r->getConstructor() && 0 !== $r->getConstructor()->getNumberOfRequiredParameters()) {
58 1
            throw new InvalidConfigurationException('HttpClient class must not have required constructor arguments.');
59
        }
60
61 1
        $httpClient = new Definition($httpClient);
62 1
        $httpClient->setPublic(false);
63 1
        $container->setDefinition('zenstruck_cache.http_client', $httpClient);
64 9
    }
65
66
    /**
67
     * @param string           $messageFactory
68
     * @param ContainerBuilder $container
69
     */
70 11 View Code Duplication
    private function configureMessageFactory($messageFactory, ContainerBuilder $container)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72 6
        if (!class_exists($messageFactory)) {
73
            // is a service
74 2
            $container->setAlias('zenstruck_cache.message_factory', $messageFactory);
75
76 4
            return;
77
        }
78
79 4
        $r = new \ReflectionClass($messageFactory);
80
81 4
        if (!$r->implementsInterface('Http\Message\MessageFactory')) {
82 1
            throw new InvalidConfigurationException('MessageFactory class must implement "Http\Message\MessageFactory".');
83
        }
84
85 3
        if ($r->isAbstract()) {
86 1
            throw new InvalidConfigurationException('MessageFactory class must not be abstract.');
87
        }
88
89 2
        if (null !== $r->getConstructor() && 0 !== $r->getConstructor()->getNumberOfRequiredParameters()) {
90 1
            throw new InvalidConfigurationException('MessageFactory class must not have required constructor arguments.');
91
        }
92
93 1
        $messageFactory = new Definition($messageFactory);
94 1
        $messageFactory->setPublic(false);
95 1
        $container->setDefinition('zenstruck_cache.message_factory', $messageFactory);
96 6
    }
97
}
98