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

OmnipayExtension::load()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 45
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 6.0226

Importance

Changes 0
Metric Value
dl 0
loc 45
ccs 32
cts 35
cp 0.9143
rs 8.439
c 0
b 0
f 0
cc 6
eloc 26
nc 12
nop 2
crap 6.0226
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