Completed
Branch master (c0d8ef)
by Yaroslav
06:36
created

ForeignExtensionAccessor   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 13
dl 0
loc 42
rs 10
c 0
b 0
f 0
ccs 13
cts 13
cp 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 2
A getProcessedConfig() 0 17 3
1
<?php
2
3
/*
4
 *
5
 * (c) Yaroslav Honcharuk <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Yarhon\RouteGuardBundle\DependencyInjection\Container;
12
13
use Symfony\Component\DependencyInjection\ContainerBuilder;
14
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
15
use Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface;
16
use Symfony\Component\Config\Definition\Processor;
17
use Symfony\Component\Config\Definition\Exception\Exception as ConfigDefinitionException;
18
use Yarhon\RouteGuardBundle\Exception\LogicException;
19
20
/**
21
 * @author Yaroslav Honcharuk <[email protected]>
22
 */
23
class ForeignExtensionAccessor
24
{
25
    /**
26
     * @var Processor
27
     */
28
    private $processor;
29
30
    /**
31
     * @param Processor|null $processor
32
     */
33 4
    public function __construct(Processor $processor = null)
34
    {
35 4
        $this->processor = $processor ?: new Processor();
36 4
    }
37
38
    /**
39
     * Returns processed config of a foreign extension outside of it's context.
40
     *
41
     * @param ContainerBuilder   $container
42
     * @param ExtensionInterface $extension
43
     *
44
     * @return array Processed configuration
45
     *
46
     * @throws LogicException When extension configuration class in not an instance of ConfigurationExtensionInterface
47
     */
48 4
    public function getProcessedConfig(ContainerBuilder $container, ExtensionInterface $extension)
49
    {
50 4
        if (!($extension instanceof ConfigurationExtensionInterface)) {
51 1
            throw new LogicException(sprintf('"%s" extension class is not an instance of %s.',
52 1
                $extension->getAlias(), ConfigurationExtensionInterface::class));
53
        }
54
55 3
        $configuration = $extension->getConfiguration([], $container);
56 3
        $configs = $container->getExtensionConfig($extension->getAlias());
57
58
        try {
59 3
            $processed = $this->processor->processConfiguration($configuration, $configs);
0 ignored issues
show
Bug introduced by
It seems like $configuration can also be of type null; however, parameter $configuration of Symfony\Component\Config...:processConfiguration() does only seem to accept Symfony\Component\Config...\ConfigurationInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
            $processed = $this->processor->processConfiguration(/** @scrutinizer ignore-type */ $configuration, $configs);
Loading history...
60 1
        } catch (ConfigDefinitionException $e) {
61 1
            throw new LogicException(sprintf('Cannot read configuration of the "%s" extension because of configuration exception.', $extension->getAlias()), 0, $e);
62
        }
63
64 2
        return $processed;
65
    }
66
}
67