OmnipayExtension   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 7
dl 0
loc 52
ccs 0
cts 36
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B load() 0 46 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
    public function load(array $configs, ContainerBuilder $container)
31
    {
32
        $configuration = new Configuration();
33
        $config = $this->processConfiguration($configuration, $configs);
34
35
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
36
        $loader->load('services.yml');
37
38
        $methods = $config['methods'];
39
        $methodNames = array_keys($methods);
40
41
        // Add configuration to the Omnipay service
42
        $omnipay = $container->getDefinition('omnipay');
43
        $omnipay->setPublic(true);
44
        $omnipay->addArgument($methods);
45
46
        if ($disabledGateways = $config['disabled_gateways']) {
47
            $omnipay->addMethodCall('setDisabledGateways', [$disabledGateways]);
48
        }
49
50
        if ($defaultGateway = $config['default_gateway']) {
51
            $allowedValues = array_diff($methodNames, $disabledGateways);
52
53
            if (!in_array($defaultGateway, $methodNames)) {
54
                throw new InvalidConfigurationException(sprintf(
55
                    'You cannot specify non-existing gateway (%s) as default. Allowed values: %s',
56
                    $defaultGateway,
57
                    implode(', ', $allowedValues)
58
                ));
59
            }
60
61
            if (in_array($defaultGateway, $disabledGateways)) {
62
                throw new InvalidConfigurationException(sprintf(
63
                    'You cannot specify disabled gateway (%s) as default. Allowed values: %s',
64
                    $defaultGateway,
65
                    implode(', ', $allowedValues)
66
                ));
67
            }
68
69
            $omnipay->addMethodCall('setDefaultGatewayName', [$defaultGateway]);
70
        }
71
72
        if ($initializeOnRegistration = $config['initialize_gateway_on_registration']) {
73
            $omnipay->addMethodCall('initializeOnRegistration', [$initializeOnRegistration]);
74
        }
75
    }
76
}
77