Passed
Push — master ( 989339...709032 )
by Sébastien
03:24
created

ReceiverLoader::configure()   F

Complexity

Conditions 13
Paths 1024

Size

Total Lines 49
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 17.9234

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 25
c 1
b 0
f 0
nc 1024
nop 2
dl 0
loc 49
ccs 18
cts 26
cp 0.6923
crap 17.9234
rs 2.45

How to fix   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 4
    public function __construct(ContainerInterface $container, array $configuration, ReceiverFactory $factory = null)
39
    {
40 4
        $this->container = $container;
41 4
        $this->configuration = $configuration;
42 4
        $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 3
    public function load(string $name): ReceiverBuilder
53
    {
54 3
        $builder = new ReceiverBuilder($this->container, null, $this->factory);
55
56 3
        if (!isset($this->configuration[$name])) {
57 1
            return $builder;
58
        }
59
60 2
        $this->configure($builder, $this->configuration[$name]);
61
62 2
        return $builder;
63
    }
64
65
    /**
66
     * @param ReceiverBuilder $builder
67
     *
68
     * @param array $config
69
     */
70 2
    private function configure(ReceiverBuilder $builder, array $config)
71
    {
72 2
        $builder->log();
73
74 2
        if ($config['middlewares'] ?? false) {
75
            foreach ($config['middlewares'] as $middleware => $parameters) {
76
                if (is_int($middleware)) {
77
                    $middleware = $parameters;
78
                    $parameters = [];
79
                }
80
81
                $builder->add($middleware, $parameters);
82
            }
83
        }
84
85 2
        if ($config['retry'] ?? false) {
86 1
            $builder->retry($config['retry']);
87
        }
88
89 2
        if ($config['save'] ?? false) {
90
            $builder->store();
91
        }
92
93 2
        if ($config['limit'] ?? false) {
94 1
            $builder->limit($config['limit']);
95
        }
96
97 2
        if (($config['no_failure'] ?? false)) {
98 1
            $builder->noFailure();
99
        }
100
101 2
        if ($config['stop_when_empty'] ?? false) {
102
            $builder->stopWhenEmpty();
103
        }
104
105 2
        if ($config['max'] ?? false) {
106 1
            $builder->max($config['max']);
107
        }
108
109 2
        if ($config['memory'] ?? false) {
110 1
            $builder->memory($this->convertToBytes($config['memory']));
111
        }
112
113 2
        if ($config['handler'] ?? false) {
114 1
            $builder->handler($config['handler']);
115
        }
116
117 2
        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 1
    public function convertToBytes(string $value): int
130
    {
131 1
        $value = strtolower(trim($value));
132 1
        $unit = substr($value, -1);
133 1
        $bytes = (int) $value;
134
135 1
        switch ($unit) {
136 1
            case 't': $bytes *= 1024;
137
            // no break
138 1
            case 'g': $bytes *= 1024;
139
            // no break
140 1
            case 'm': $bytes *= 1024;
141
            // no break
142 1
            case 'k': $bytes *= 1024;
143
        }
144
145 1
        return $bytes;
146
    }
147
}
148