Completed
Pull Request — master (#9)
by Kevin
09:02 queued 06:58
created

ZenstruckCacheExtension::configureHttpAdapter()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 29
Code Lines 15

Duplication

Lines 29
Ratio 100 %

Code Coverage

Tests 16
CRAP Score 7

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 29
loc 29
ccs 16
cts 16
cp 1
rs 6.7273
cc 7
eloc 15
nc 10
nop 2
crap 7
1
<?php
2
3
namespace Zenstruck\CacheBundle\DependencyInjection;
4
5
use Http\Discovery\HttpClientDiscovery;
6
use Http\Discovery\MessageFactoryDiscovery;
7
use Http\Discovery\NotFoundException;
8
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
9
use Symfony\Component\Config\FileLocator;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
use Symfony\Component\DependencyInjection\Definition;
12
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
13
use Symfony\Component\DependencyInjection\Loader;
14
15
/**
16
 * @author Kevin Bond <[email protected]>
17
 */
18
class ZenstruckCacheExtension extends ConfigurableExtension
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23 22
    protected function loadInternal(array $mergedConfig, ContainerBuilder $container)
24
    {
25 22
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
26 22
        $loader->load('services.xml');
27
28 22
        $this->configureHttpClient($mergedConfig['http_client'], $container);
29 16
        $this->configureMessageFactory($mergedConfig['message_factory'], $container);
30
31 10
        if ($mergedConfig['sitemap_provider']['enabled']) {
32 2
            $container->setParameter('zenstruck_cache.sitemap_provider.sitemaps', $mergedConfig['sitemap_provider']['sitemaps']);
33 2
            $loader->load('sitemap_provider.xml');
34 2
        }
35 10
    }
36
37
    /**
38
     * @param string|null      $httpClient
39
     * @param ContainerBuilder $container
40
     */
41 22 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...
42
    {
43 22
        $httpClient = $httpClient ?: $this->autoDiscoverHttpClient();
44
45 22
        if (!class_exists($httpClient)) {
46
            // is a service
47 12
            $container->setAlias('zenstruck_cache.http_client', $httpClient);
48
49 12
            return;
50
        }
51
52 10
        $r = new \ReflectionClass($httpClient);
53
54 10
        if (!$r->implementsInterface('Http\Client\HttpClient')) {
55 2
            throw new InvalidConfigurationException('HttpClient class must implement "Http\Client\HttpClient".');
56
        }
57
58 8
        if ($r->isAbstract()) {
59 2
            throw new InvalidConfigurationException('HttpClient class must not be abstract.');
60
        }
61
62 6
        if (null !== $r->getConstructor() && 0 !== $r->getConstructor()->getNumberOfRequiredParameters()) {
63 2
            throw new InvalidConfigurationException('HttpClient class must not have required constructor arguments.');
64
        }
65
66 4
        $httpClient = new Definition($httpClient);
67 4
        $httpClient->setPublic(false);
68 4
        $container->setDefinition('zenstruck_cache.http_client', $httpClient);
69 4
    }
70
71
    /**
72
     * @param string|null      $messageFactory
73
     * @param ContainerBuilder $container
74
     */
75 16 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...
76
    {
77 16
        $messageFactory = $messageFactory ?: $this->autoDiscoverMessageFactory();
78
79 16
        if (!class_exists($messageFactory)) {
80
            // is a service
81 6
            $container->setAlias('zenstruck_cache.message_factory', $messageFactory);
82
83 6
            return;
84
        }
85
86 10
        $r = new \ReflectionClass($messageFactory);
87
88 10
        if (!$r->implementsInterface('Http\Message\MessageFactory')) {
89 2
            throw new InvalidConfigurationException('MessageFactory class must implement "Http\Message\MessageFactory".');
90
        }
91
92 8
        if ($r->isAbstract()) {
93 2
            throw new InvalidConfigurationException('MessageFactory class must not be abstract.');
94
        }
95
96 6
        if (null !== $r->getConstructor() && 0 !== $r->getConstructor()->getNumberOfRequiredParameters()) {
97 2
            throw new InvalidConfigurationException('MessageFactory class must not have required constructor arguments.');
98
        }
99
100 4
        $messageFactory = new Definition($messageFactory);
101 4
        $messageFactory->setPublic(false);
102 4
        $container->setDefinition('zenstruck_cache.message_factory', $messageFactory);
103 4
    }
104
105
    /**
106
     * @return string
107
     */
108 2
    private function autoDiscoverHttpClient()
109
    {
110
        try {
111 2
            return get_class(HttpClientDiscovery::find());
112
        } catch (NotFoundException $e) {
113
            throw new InvalidConfigurationException('A HttpClient was not found, please define one in your configuration.', 0, $e);
114
        }
115
    }
116
117
    /**
118
     * @return string
119
     */
120 2
    private function autoDiscoverMessageFactory()
121
    {
122
        try {
123 2
            return get_class(MessageFactoryDiscovery::find());
124
        } catch (NotFoundException $e) {
125
            throw new InvalidConfigurationException('A MessageFactory was not found, please define one in your configuration.', 0, $e);
126
        }
127
    }
128
}
129