KaliopQueueingPluginsSQSExtension::loadQueues()   F
last analyzed

Complexity

Conditions 12
Paths 516

Size

Total Lines 57

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 90.8752

Importance

Changes 0
Metric Value
dl 0
loc 57
c 0
b 0
f 0
ccs 6
cts 33
cp 0.1818
rs 3.7159
cc 12
nc 516
nop 0
crap 90.8752

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
            if ($consumer['queue_options']['message_group_id'] != null) {
67
                $pDefinition->addMethodCall('setMessageGroupId', array($consumer['queue_options']['message_group_id']));
68
            }
69
            if ($consumer['queue_options']['message_deduplication_id_calculator'] != null) {
70
                $pDefinition->addMethodCall('setMessageDeduplicationIdCalculator', array($consumer['queue_options']['message_deduplication_id_calculator']));
71
            }
72
            $name = sprintf('kaliop_queueing.sqs.%s_producer', $key);
73
            $this->container->setDefinition($name, $pDefinition);
74
75
            $cDefinition = new Definition('%kaliop_queueing.sqs.consumer.class%', array($this->config['connections'][$consumer['connection']]));
76
            $cDefinition
77
                ->addMethodCall('setQueueUrl', array($consumer['queue_options']['name']))
78
                ->addMethodCall('setCallback', array(new Reference($consumer['callback'])));
79
            ;
80
            if (count($consumer['queue_options']['routing_keys'])) {
81
                $cDefinition->addMethodCall('setRoutingKey', array(reset($consumer['queue_options']['routing_keys'])));
82
            }
83
            if ($consumer['queue_options']['max_messages_per_request'] > 1) {
84
                $cDefinition->addMethodCall('setRequestBatchSize', array($consumer['queue_options']['max_messages_per_request']));
85
            }
86
            if ($consumer['queue_options']['request_timeout'] > 0) {
87
                $cDefinition->addMethodCall('setRequestTimeout', array($consumer['queue_options']['request_timeout']));
88
            }
89
            if ($consumer['queue_options']['gc_probability'] != 1) {
90
                $cDefinition->addMethodCall('setGCProbability', array($consumer['queue_options']['gc_probability']));
91
            }
92
            if ($consumer['queue_options']['polling_interval'] != 200000) {
93
                $cDefinition->addMethodCall('setPollingInterval', array($consumer['queue_options']['polling_interval']));
94
            }
95
            $name = sprintf('kaliop_queueing.sqs.%s_consumer', $key);
96
            $this->container->setDefinition($name, $cDefinition);
97
98
            //if (!$consumer['auto_setup_fabric']) {
99
            //    $definition->addMethodCall('disableAutoSetupFabric');
100
            //}
101
102
            if ($qmDefinition) {
103
                $qmDefinition->addMethodCall('registerQueue', array($key));
104
            }
105
        }
106 1
    }
107
}
108