Driver   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 36
c 1
b 0
f 0
dl 0
loc 135
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A open() 0 10 2
A interop() 0 3 1
A state() 0 3 1
A __construct() 0 5 1
A context() 0 7 3
A reconnect() 0 5 1
A close() 0 13 2
A __destruct() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BinaryCube\CarrotMQ\Driver;
6
7
use Throwable;
8
use Psr\Log\LoggerInterface;
9
use Interop\Amqp\AmqpContext;
10
use BinaryCube\CarrotMQ\Component;
11
use Interop\Amqp\AmqpConnectionFactory;
12
use BinaryCube\CarrotMQ\Support\LoggerAwareTrait;
13
14
/**
15
 * Class Driver
16
 */
17
abstract class Driver extends Component
18
{
19
    use LoggerAwareTrait;
20
21
    const STATE_OPEN  = 'open';
22
    const STATE_CLOSE = 'close';
23
24
    /**
25
     * @var string
26
     */
27
    private $state = self::STATE_CLOSE;
28
29
    /**
30
     * @const array Default driver parameters
31
     */
32
    const DEFAULTS = [];
33
34
    /**
35
     * @var array
36
     */
37
    protected $config = [];
38
39
    /**
40
     * @var AmqpConnectionFactory
41
     */
42
    protected $interop;
43
44
    /**
45
     * @var AmqpContext
46
     */
47
    protected $context;
48
49
    /**
50
     * Constructor.
51
     *
52
     * @param array                $config
53
     * @param LoggerInterface|null $logger
54
     */
55
    public function __construct(array $config = [], ?LoggerInterface $logger = null)
56
    {
57
        parent::__construct(null, $logger);
58
59
        $this->config = $config;
60
    }
61
62
    /**
63
     * @return AmqpConnectionFactory
64
     */
65
    public function interop(): AmqpConnectionFactory
66
    {
67
        return $this->interop;
68
    }
69
70
    /**
71
     * @param bool $new
72
     *
73
     * @return AmqpContext
74
     */
75
    public function context(bool $new = false): AmqpContext
76
    {
77
        if ($new || empty($this->context)) {
78
            $this->context = $this->interop->createContext();
79
        }
80
81
        return $this->context;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->context could return the type Interop\Queue\Context which includes types incompatible with the type-hinted return Interop\Amqp\AmqpContext. Consider adding an additional type-check to rule them out.
Loading history...
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function state(): string
88
    {
89
        return $this->state;
90
    }
91
92
    /**
93
     * @return $this
94
     */
95
    public function open()
96
    {
97
        if ($this->state === self::STATE_OPEN) {
98
            return $this;
99
        }
100
101
        $this->interop = $this->build();
102
        $this->state   = self::STATE_OPEN;
103
104
        return $this;
105
    }
106
107
    /**
108
     * @return $this
109
     */
110
    public function close()
111
    {
112
        try {
113
            $this->context->close();
114
        } catch (Throwable $e) {
115
            /* Ignore on shutdown. */
116
        }
117
118
        $this->context = null;
119
        $this->interop = null;
120
        $this->state   = self::STATE_CLOSE;
121
122
        return $this;
123
    }
124
125
    /**
126
     * @return $this
127
     */
128
    public function reconnect()
129
    {
130
        $this->close()->open();
131
132
        return $this;
133
    }
134
135
    /**
136
     * @return AmqpConnectionFactory
137
     */
138
    abstract protected function build(): AmqpConnectionFactory;
139
140
    /**
141
     * @return void
142
     */
143
    public function __destruct()
144
    {
145
        $this->close();
146
147
        unset(
148
            $this->state,
149
            $this->config,
150
            $this->context,
151
            $this->interop
152
        );
153
    }
154
155
}
156