ReceiverLoader::configure()   F
last analyzed

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
     * ReceiverLoader constructor.
32
     *
33
     * @param array[] $configuration
34
     */
35 8
    public function __construct(ContainerInterface $container, array $configuration, ReceiverFactory $factory = null)
36
    {
37 8
        $this->container = $container;
38 8
        $this->configuration = $configuration;
39 8
        $this->factory = $factory;
40
    }
41
42
    /**
43
     * Load the receiver build from the configuration.
44
     *
45
     * @param string $name The destination name
46
     */
47 6
    public function load(string $name): ReceiverBuilder
48
    {
49 6
        $builder = new ReceiverBuilder($this->container, null, $this->factory);
50
51 6
        $this->configure($builder, $this->configuration[$name] ?? []);
52
53 6
        return $builder;
54
    }
55
56 6
    private function configure(ReceiverBuilder $builder, array $config)
57
    {
58 6
        $builder->log();
59
60 6
        if ($config['middlewares'] ?? false) {
61
            foreach ($config['middlewares'] as $middleware => $parameters) {
62
                if (is_int($middleware)) {
63
                    $middleware = $parameters;
64
                    $parameters = [];
65
                }
66
67
                $builder->add($middleware, $parameters);
68
            }
69
        }
70
71 6
        if ($config['retry'] ?? false) {
72 3
            $builder->retry($config['retry']);
73
        }
74
75 6
        if ($config['save'] ?? false) {
76
            $builder->store();
77
        }
78
79 6
        if ($config['limit'] ?? false) {
80 3
            $builder->limit($config['limit']);
81
        }
82
83 6
        if ($config['no_failure'] ?? false) {
84 3
            $builder->noFailure();
85
        }
86
87 6
        if ($config['stop_when_empty'] ?? false) {
88
            $builder->stopWhenEmpty();
89
        }
90
91 6
        if ($config['max'] ?? false) {
92 3
            $builder->max($config['max']);
93
        }
94
95 6
        if ($config['memory'] ?? false) {
96 3
            $builder->memory($this->convertToBytes($config['memory']));
97
        }
98
99 6
        if (!($config['no_reset'] ?? false) && $this->container->has('services_resetter')) {
100 3
            $builder->add('reset', [$this->container->get('services_resetter')]);
101
        }
102
103 6
        if ($config['handler'] ?? false) {
104 1
            $builder->handler($config['handler']);
105
        }
106
107 6
        if ($config['auto_handle'] ?? false) {
108
            $builder->jobProcessor();
109
        }
110
    }
111
112
    /**
113
     * Convert the given string value in bytes.
114
     */
115 3
    public function convertToBytes(string $value): int
116
    {
117 3
        $value = strtolower(trim($value));
118 3
        $unit = substr($value, -1);
119 3
        $bytes = (int) $value;
120
121
        switch ($unit) {
122 3
            case 't': $bytes *= 1024;
123
                // no break
124 3
            case 'g': $bytes *= 1024;
125
                // no break
126 3
            case 'm': $bytes *= 1024;
127
                // no break
128 3
            case 'k': $bytes *= 1024;
129
        }
130
131 3
        return $bytes;
132
    }
133
}
134