Test Failed
Push — master ( 159af9...b2d1de )
by Banciu N. Cristian Mihai
03:26
created

AutoWireAwareTrait::autoWire()   B

Complexity

Conditions 8
Paths 17

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 15
c 2
b 0
f 0
dl 0
loc 31
rs 8.4444
cc 8
nc 17
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BinaryCube\CarrotMQ\Support;
6
7
use BinaryCube\CarrotMQ\Container;
8
9
/**
10
 * Trait AutoWireAwareTrait
11
 */
12
trait AutoWireAwareTrait
13
{
14
15
    /**
16
     * @var boolean
17
     */
18
    protected $wasAutoWired = false;
19
20
    /**
21
     * @param Container $container
22
     *
23
     * @return void
24
     */
25
    protected function autoWire(Container $container): void
26
    {
27
        if ($this->wasAutoWired()) {
28
            return;
29
        }
30
31
        foreach ($container->topics()->all() as $topic) {
32
            if (false === $topic->canAutoCreate()) {
33
                continue;
34
            }
35
36
            try {
37
                $topic->install();
38
            } catch (\Throwable $exception) {
39
                //
40
            }
41
        }
42
43
        foreach ($container->queues()->all() as $queue) {
44
            if (false === $queue->canAutoCreate()) {
45
                continue;
46
            }
47
48
            try {
49
                $queue->install();
50
            } catch (\Throwable $exception) {
51
                //
52
            }
53
        }
54
55
        $this->wasAutoWired = true;
56
    }
57
58
    /**
59
     * @return bool
60
     */
61
    public function wasAutoWired(): bool
62
    {
63
        return $this->wasAutoWired;
64
    }
65
66
}
67