Completed
Push — master ( 3d366e...7a582f )
by Gaetano
06:22
created

KaliopQueueingPluginsSQSExtension::loadQueues()   C

Complexity

Conditions 10
Paths 132

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 59.8865

Importance

Changes 0
Metric Value
dl 0
loc 51
ccs 6
cts 29
cp 0.2069
rs 6.989
c 0
b 0
f 0
cc 10
nc 132
nop 0
crap 59.8865

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Kaliop\Queueing\Plugins\SQSBundle\DependencyInjection;
4
5
use Symfony\Component\DependencyInjection\ContainerBuilder;
6
use Symfony\Component\Config\FileLocator;
7
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
8
use Symfony\Component\DependencyInjection\Loader;
9
use Symfony\Component\DependencyInjection\Definition;
10
use Symfony\Component\DependencyInjection\Reference;
11
12
/**
13
 * This is the class that loads and manages your bundle configuration
14
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
15
 *
16
 * Logic heavily inspired by the way that Oldsound/RabbitMqBundle does things
17
 */
18
class KaliopQueueingPluginsSQSExtension extends Extension
19
{
20
    protected $config = array();
21
    protected $container;
22
    protected $queueManagerService = 'kaliop_queueing.sqs.queue_manager';
23
24
    /**
25
     * {@inheritDoc}
26
     */
27 1
    public function load(array $configs, ContainerBuilder $container)
28
    {
29 1
        $this->container = $container;
30
31 1
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
32 1
        $loader->load('services.yml');
33
34 1
        $configuration = new Configuration();
35 1
        $this->config = $this->processConfiguration($configuration, $configs);
36
37 1
        $this->loadConnections();
38 1
        $this->loadQueues();
39 1
    }
40
41 1
    protected function loadConnections()
42
    {
43
        // this is not so much a loading as a 'store definition for later access', really
44 1
        $definition = $this->container->findDefinition('kaliop_queueing.driver.sqs');
45 1
        foreach ($this->config['connections'] as $key => $def) {
46 1
            $definition->addMethodCall('registerConnection', array($key, $def));
47
        }
48 1
    }
49
50 1
    protected function loadQueues()
51
    {
52 1
        $qmDefinition = null;
53 1
        if ($this->container->hasDefinition($this->queueManagerService)) {
54 1
            $qmDefinition = $this->container->findDefinition($this->queueManagerService);
55
        }
56
57 1
        foreach ($this->config['queues'] as $key => $consumer) {
58
            if (!isset($this->config['connections'][$consumer['connection']])) {
59
                throw new \RuntimeException("SQS queue '$key' can not use connection '{$consumer['connection']}' because it is not defined in the connections section");
60
            }
61
62
            $pDefinition = new Definition('%kaliop_queueing.sqs.producer.class%', array($this->config['connections'][$consumer['connection']]));
63
            $pDefinition
64
                ->addMethodCall('setQueueUrl', array($consumer['queue_options']['name']))
65
            ;
66
            $name = sprintf('kaliop_queueing.sqs.%s_producer', $key);
67
            $this->container->setDefinition($name, $pDefinition);
68
69
            $cDefinition = new Definition('%kaliop_queueing.sqs.consumer.class%', array($this->config['connections'][$consumer['connection']]));
70
            $cDefinition
71
                ->addMethodCall('setQueueUrl', array($consumer['queue_options']['name']))
72
                ->addMethodCall('setCallback', array(new Reference($consumer['callback'])));
73
            ;
74
            if (count($consumer['queue_options']['routing_keys'])) {
75
                $cDefinition->addMethodCall('setRoutingKey', array(reset($consumer['queue_options']['routing_keys'])));
76
            }
77
            if ($consumer['queue_options']['max_messages_per_request'] > 1) {
78
                $cDefinition->addMethodCall('setRequestBatchSize', array($consumer['queue_options']['max_messages_per_request']));
79
            }
80
            if ($consumer['queue_options']['request_timeout'] > 0) {
81
                $cDefinition->addMethodCall('setRequestTimeout', array($consumer['queue_options']['request_timeout']));
82
            }
83
            if ($consumer['queue_options']['gc_probability'] != 1) {
84
                $cDefinition->addMethodCall('setGCProbability', array($consumer['queue_options']['gc_probability']));
85
            }
86
            if ($consumer['queue_options']['polling_interval'] != 200000) {
87
                $cDefinition->addMethodCall('setPollingInterval', array($consumer['queue_options']['polling_interval']));
88
            }
89
            $name = sprintf('kaliop_queueing.sqs.%s_consumer', $key);
90
            $this->container->setDefinition($name, $cDefinition);
91
92
            //if (!$consumer['auto_setup_fabric']) {
93
            //    $definition->addMethodCall('disableAutoSetupFabric');
94
            //}
95
96
            if ($qmDefinition) {
97
                $qmDefinition->addMethodCall('registerQueue', array($key));
98
            }
99
        }
100 1
    }
101
}
102