QueueProvider   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 15
eloc 33
dl 0
loc 100
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A withChannelName() 0 15 3
A withExchangeSettings() 0 6 1
A __construct() 0 8 2
A getChannel() 0 13 3
A getExchangeSettings() 0 3 1
A withMessageProperties() 0 6 1
A getQueueSettings() 0 3 1
A __destruct() 0 3 1
A getMessageProperties() 0 3 1
A withQueueSettings() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Queue\AMQP;
6
7
use PhpAmqpLib\Channel\AMQPChannel;
8
use PhpAmqpLib\Connection\AbstractConnection;
9
use Yiisoft\Queue\AMQP\Exception\ExchangeDeclaredException;
10
use Yiisoft\Queue\AMQP\Settings\Exchange;
11
use Yiisoft\Queue\AMQP\Settings\ExchangeSettingsInterface;
12
use Yiisoft\Queue\AMQP\Settings\QueueSettingsInterface;
13
14
final class QueueProvider implements QueueProviderInterface
15
{
16
    public const EXCHANGE_NAME_DEFAULT = 'yii-queue';
17
18
    private ?AMQPChannel $channel = null;
19
20
    public function __construct(
21
        private AbstractConnection $connection,
22
        private QueueSettingsInterface $queueSettings,
23
        private ?ExchangeSettingsInterface $exchangeSettings = null,
24
        private array $messageProperties = [],
25
    ) {
26
        if ($this->exchangeSettings === null) {
27
            $this->exchangeSettings = new Exchange(self::EXCHANGE_NAME_DEFAULT);
28
        }
29
    }
30
31
    public function __destruct()
32
    {
33
        $this->channel?->close();
34
    }
35
36
    public function getChannel(): AMQPChannel
37
    {
38
        if ($this->channel === null) {
39
            $this->channel = $this->connection->channel();
40
            $this->channel->queue_declare(...$this->queueSettings->getPositionalSettings());
0 ignored issues
show
Bug introduced by
The method queue_declare() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
            $this->channel->/** @scrutinizer ignore-call */ 
41
                            queue_declare(...$this->queueSettings->getPositionalSettings());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
41
42
            if ($this->exchangeSettings !== null) {
43
                $this->channel->exchange_declare(...$this->exchangeSettings->getPositionalSettings());
44
                $this->channel->queue_bind($this->queueSettings->getName(), $this->exchangeSettings->getName());
45
            }
46
        }
47
48
        return $this->channel;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->channel could return the type null which is incompatible with the type-hinted return PhpAmqpLib\Channel\AMQPChannel. Consider adding an additional type-check to rule them out.
Loading history...
49
    }
50
51
    public function getQueueSettings(): QueueSettingsInterface
52
    {
53
        return $this->queueSettings;
54
    }
55
56
    public function getExchangeSettings(): ?ExchangeSettingsInterface
57
    {
58
        return $this->exchangeSettings;
59
    }
60
61
    public function getMessageProperties(): array
62
    {
63
        return $this->messageProperties;
64
    }
65
66
    public function withChannelName(string $channel): self
67
    {
68
        if ($channel === $this->queueSettings->getName()) {
69
            return $this;
70
        }
71
72
        if ($this->exchangeSettings !== null) {
73
            throw new ExchangeDeclaredException();
74
        }
75
76
        $instance = clone $this;
77
        $instance->channel = null;
78
        $instance->queueSettings = $instance->queueSettings->withName($channel);
79
80
        return $instance;
81
    }
82
83
    /**
84
     * @return self
85
     */
86
    public function withQueueSettings(QueueSettingsInterface $queueSettings): QueueProviderInterface
87
    {
88
        $new = clone $this;
89
        $new->queueSettings = $queueSettings;
90
91
        return $new;
92
    }
93
94
    /**
95
     * @return self
96
     */
97
    public function withExchangeSettings(?ExchangeSettingsInterface $exchangeSettings): QueueProviderInterface
98
    {
99
        $new = clone $this;
100
        $new->exchangeSettings = $exchangeSettings;
101
102
        return $new;
103
    }
104
105
    /**
106
     * @return self
107
     */
108
    public function withMessageProperties(array $properties): QueueProviderInterface
109
    {
110
        $new = clone $this;
111
        $new->messageProperties = $properties;
112
113
        return $new;
114
    }
115
}
116