Completed
Push — master ( c80d7c...01f162 )
by Colin
05:28
created

OmnipayExtension   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 91.43%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 7
dl 0
loc 51
ccs 32
cts 35
cp 0.9143
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B load() 0 45 6
1
<?php
2
3
/*
4
 * This file is part of the colinodell\omnipay-bundle package.
5
 *
6
 * (c) 2018 Colin O'Dell <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ColinODell\OmnipayBundle\DependencyInjection;
13
14
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\Config\FileLocator;
17
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
18
use Symfony\Component\DependencyInjection\Loader;
19
20
/**
21
 * This is the class that loads and manages your bundle configuration
22
 *
23
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
24
 */
25
class OmnipayExtension extends Extension
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30 16
    public function load(array $configs, ContainerBuilder $container)
31
    {
32 16
        $configuration = new Configuration();
33 16
        $config = $this->processConfiguration($configuration, $configs);
34
35 16
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
36 16
        $loader->load('services.yml');
37
38 16
        $methods = $config['methods'];
39 16
        $methodNames = array_keys($methods);
40
41
        // Add configuration to the Omnipay service
42 16
        $omnipay = $container->getDefinition('omnipay');
43 16
        $omnipay->addArgument($methods);
44
45 16
        if ($disabledGateways = $config['disabled_gateways']) {
46 6
            $omnipay->addMethodCall('setDisabledGateways', [$disabledGateways]);
47 6
        }
48
49 16
        if ($defaultGateway = $config['default_gateway']) {
50 8
            $allowedValues = array_diff($methodNames, $disabledGateways);
51
52 8
            if (!in_array($defaultGateway, $methodNames)) {
53 2
                throw new InvalidConfigurationException(sprintf(
54 2
                    'You cannot specify non-existing gateway (%s) as default. Allowed values: %s',
55 2
                    $defaultGateway,
56 2
                    implode(', ', $allowedValues)
57 2
                ));
58
            }
59
60 6
            if (in_array($defaultGateway, $disabledGateways)) {
61 2
                throw new InvalidConfigurationException(sprintf(
62 2
                    'You cannot specify disabled gateway (%s) as default. Allowed values: %s',
63 2
                    $defaultGateway,
64 2
                    implode(', ', $allowedValues)
65 2
                ));
66
            }
67
68 4
            $omnipay->addMethodCall('setDefaultGatewayName', [$defaultGateway]);
69 4
        }
70
71 12
        if ($initializeOnRegistration = $config['initialize_gateway_on_registration']) {
72 2
            $omnipay->addMethodCall('initializeOnRegistration', [$initializeOnRegistration]);
73 2
        }
74 12
    }
75
}
76