ConsumerMethods::mergeExchangeOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
/**
4
 * This file is part of amqp
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
 
10
declare(strict_types=1);
11
12
namespace Slick\Amqp\Consumer;
13
14
use Slick\Amqp\Consumer;
15
use Slick\Amqp\Producer\BasicProducer;
16
17
/**
18
 * ConsumerMethods
19
 *
20
 * @package Slick\Amqp\Consumer
21
 */
22
trait ConsumerMethods
23
{
24
    /**
25
     * @var array<string, mixed>
26
     */
27
    protected array $options = [];
28
29
    /** @var array<string, mixed> */
30
    protected array $consumeOptions = [];
31
32
    /** @var array<string, mixed> */
33
    protected array $exchangeOptions = [];
34
35
    /**
36
     * @var array<string, mixed>
37
     */
38
    protected static array $defaultOptions = [
39
        Consumer::OPT_PASSIVE => false,
40
        Consumer::OPT_DURABLE => false,
41
        Consumer::OPT_EXCLUSIVE => false,
42
        Consumer::OPT_AUTO_DELETE => true,
43
        Consumer::OPT_NOWAIT => false,
44
        Consumer::OPT_ARGUMENTS => [],
45
        Consumer::OPT_TICKET => null
46
    ];
47
48
    /** @var array<string, mixed>  */
49
    protected static array $defaultConsumeOpt = [
50
        Consumer::CONSUME_OPT_CONSUMER_TAG => '',
51
        Consumer::CONSUME_OPT_NO_LOCAL => false,
52
        Consumer::CONSUME_OPT_NO_ACK => true,
53
        Consumer::CONSUME_OPT_EXCLUSIVE => false,
54
        Consumer::CONSUME_OPT_NOWAIT => false,
55
        'callback' => null,
56
        Consumer::CONSUME_OPT_TICKET => null,
57
        Consumer::CONSUME_OPT_ARGUMENTS=> [],
58
    ];
59
60
    /**
61
     * Merges provided options
62
     *
63
     * @param array<string, mixed>|null $options
64
     * @return self
65
     */
66
    protected function mergeOptions(?array $options = []): self
67
    {
68
        $this->options = array_merge(self::$defaultOptions, $this->options, $options ?? []);
69
        return $this;
70
    }
71
72
    /**
73
     * Merges consume options
74
     *
75
     * @param array<string, mixed> $options
76
     * @return self|$this
77
     */
78
    protected function mergeConsumeOptions(array $options): self
79
    {
80
        $this->consumeOptions = array_merge(self::$defaultConsumeOpt, $this->consumeOptions, $options);
81
        return $this;
82
    }
83
84
    /**
85
     * Merges exchange options
86
     *
87
     * @param array<string, mixed>|null $options
88
     * @return self
89
     */
90
    protected function mergeExchangeOptions(?array $options = []): self
91
    {
92
        $this->exchangeOptions = array_merge(
93
            BasicProducer::exchangeDefaultOptions(),
94
            $this->exchangeOptions,
95
            $options ?? []
96
        );
97
        return $this;
98
    }
99
100
    /**
101
     * @inheritDoc
102
     */
103
    public function isPassive(): bool
104
    {
105
        return (bool) $this->options[Consumer::OPT_PASSIVE];
106
    }
107
108
    /**
109
     * @inheritDoc
110
     */
111
    public function isDurable(): bool
112
    {
113
        return (bool) $this->options[Consumer::OPT_DURABLE];
114
    }
115
116
    /**
117
     * @inheritDoc
118
     */
119
    public function isExclusive(): bool
120
    {
121
        return (bool) $this->options[Consumer::OPT_EXCLUSIVE];
122
    }
123
124
    /**
125
     * @inheritDoc
126
     */
127
    public function isAutoDelete(): bool
128
    {
129
        return (bool) $this->options[Consumer::OPT_AUTO_DELETE];
130
    }
131
132
    /**
133
     * Exchange/Producer options
134
     *
135
     * @return array<string, mixed>
136
     */
137
    public function options(): array
138
    {
139
        return $this->options;
140
    }
141
142
    /**
143
     * Options used to declare exchange
144
     *
145
     * @return array<string, mixed>
146
     */
147
    public function exchangeOptions(): array
148
    {
149
        return $this->exchangeOptions;
150
    }
151
}
152