Passed
Push — master ( c2b6eb...8c3bb9 )
by Sébastien
09:25
created

ReceiverLoader::configure()   F

Complexity

Conditions 15
Paths 2048

Size

Total Lines 53
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 20.247

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 15
eloc 27
c 1
b 0
f 0
nc 2048
nop 2
dl 0
loc 53
ccs 20
cts 28
cp 0.7143
crap 20.247
rs 1.7499

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 Bdf\QueueBundle\Consumption;
4
5
use Bdf\Queue\Consumer\Receiver\Builder\ReceiverBuilder;
6
use Bdf\Queue\Consumer\Receiver\Builder\ReceiverFactory;
7
use Bdf\Queue\Consumer\Receiver\Builder\ReceiverLoaderInterface;
8
use Psr\Container\ContainerInterface;
9
10
/**
11
 * Loader for receiver configuration
12
 */
13
class ReceiverLoader implements ReceiverLoaderInterface
14
{
15
    /**
16
     * @var ContainerInterface
17
     */
18
    private $container;
19
20
    /**
21
     * @var ReceiverFactory
22
     */
23
    private $factory;
24
25
    /**
26
     * @var array[]
27
     */
28
    private $configuration;
29
30
31
    /**
32
     * ReceiverLoader constructor.
33
     *
34
     * @param ContainerInterface $container
35
     * @param array[] $configuration
36
     * @param ReceiverFactory|null $factory
37
     */
38 7
    public function __construct(ContainerInterface $container, array $configuration, ReceiverFactory $factory = null)
39
    {
40 7
        $this->container = $container;
41 7
        $this->configuration = $configuration;
42 7
        $this->factory = $factory;
43
    }
44
45
    /**
46
     * Load the receiver build from the configuration
47
     *
48
     * @param string $name The destination name
49
     *
50
     * @return ReceiverBuilder
51
     */
52 6
    public function load(string $name): ReceiverBuilder
53
    {
54 6
        $builder = new ReceiverBuilder($this->container, null, $this->factory);
55
56 6
        $this->configure($builder, $this->configuration[$name] ?? []);
57
58 6
        return $builder;
59
    }
60
61
    /**
62
     * @param ReceiverBuilder $builder
63
     *
64
     * @param array $config
65
     */
66 6
    private function configure(ReceiverBuilder $builder, array $config)
67
    {
68 6
        $builder->log();
69
70 6
        if ($config['middlewares'] ?? false) {
71
            foreach ($config['middlewares'] as $middleware => $parameters) {
72
                if (is_int($middleware)) {
73
                    $middleware = $parameters;
74
                    $parameters = [];
75
                }
76
77
                $builder->add($middleware, $parameters);
78
            }
79
        }
80
81 6
        if ($config['retry'] ?? false) {
82 3
            $builder->retry($config['retry']);
83
        }
84
85 6
        if ($config['save'] ?? false) {
86
            $builder->store();
87
        }
88
89 6
        if ($config['limit'] ?? false) {
90 3
            $builder->limit($config['limit']);
91
        }
92
93 6
        if (($config['no_failure'] ?? false)) {
94 3
            $builder->noFailure();
95
        }
96
97 6
        if ($config['stop_when_empty'] ?? false) {
98
            $builder->stopWhenEmpty();
99
        }
100
101 6
        if ($config['max'] ?? false) {
102 3
            $builder->max($config['max']);
103
        }
104
105 6
        if ($config['memory'] ?? false) {
106 3
            $builder->memory($this->convertToBytes($config['memory']));
107
        }
108
109 6
        if (!($config['no_reset'] ?? false) && $this->container->has('services_resetter')) {
110 3
            $builder->add('reset', [$this->container->get('services_resetter')]);
111
        }
112
113 6
        if ($config['handler'] ?? false) {
114 1
            $builder->handler($config['handler']);
115
        }
116
117 6
        if ($config['auto_handle'] ?? false) {
118
            $builder->jobProcessor();
119
        }
120
    }
121
122
    /**
123
     * Convert the given string value in bytes.
124
     *
125
     * @param string $value
126
     *
127
     * @return int
128
     */
129 3
    public function convertToBytes(string $value): int
130
    {
131 3
        $value = strtolower(trim($value));
132 3
        $unit = substr($value, -1);
133 3
        $bytes = (int) $value;
134
135 3
        switch ($unit) {
136 3
            case 't': $bytes *= 1024;
137
            // no break
138 3
            case 'g': $bytes *= 1024;
139
            // no break
140 3
            case 'm': $bytes *= 1024;
141
            // no break
142 3
            case 'k': $bytes *= 1024;
143
        }
144
145 3
        return $bytes;
146
    }
147
}
148