Issues (8)

src/MessageHeadersMethods.php (5 issues)

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;
13
14
use PhpAmqpLib\Wire\AMQPTable;
15
16
/**
17
 * MessageHeadersMethods
18
 *
19
 * @package Slick\Amqp
20
 */
21
trait MessageHeadersMethods
22
{
23
24
    /**
25
     * Retrieves the headers of the AMQP message.
26
     *
27
     * @return array<string, mixed> The headers of the AMQP message.
28
     */
29
    public function headers(): array
30
    {
31
        return $this->headers;
32
    }
33
34
    /**
35
     * Adds a header to the message.
36
     *
37
     * @param string $name The name of the header.
38
     * @param mixed $value The value of the header.
39
     * @return self Returns an instance of the class for method chaining.
40
     */
41
    public function withHeader(string $name, mixed $value): self
42
    {
43
        $this->headers[$name] = $value;
0 ignored issues
show
Bug Best Practice introduced by
The property headers does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
44
        $this->message->set(self::HEADERS, new AMQPTable($this->headers));
0 ignored issues
show
The constant Slick\Amqp\MessageHeadersMethods::HEADERS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
45
        return $this;
46
    }
47
48
    /**
49
     * Removes a header from the message if it exists.
50
     *
51
     * @param string $string
52
     * @return self Returns an instance of the class for method chaining.
53
     */
54
    public function withoutHeader(string $string): self
55
    {
56
        if (!array_key_exists($string, $this->headers)) {
57
            return $this;
58
        }
59
60
        unset($this->headers[$string]);
61
        $this->message->set(self::HEADERS, new AMQPTable($this->headers));
0 ignored issues
show
The constant Slick\Amqp\MessageHeadersMethods::HEADERS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
62
        return $this;
63
    }
64
65
    /**
66
     * Set multiple headers for the message.
67
     *
68
     * @param array<string, mixed> $headers An array of headers to set for the message.
69
     * @return self Returns an instance of the class for method chaining.
70
     */
71
    public function withHeaders(array $headers): self
72
    {
73
        $this->headers = $headers;
0 ignored issues
show
Bug Best Practice introduced by
The property headers does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
74
        $this->message->set(self::HEADERS, new AMQPTable($this->headers));
0 ignored issues
show
The constant Slick\Amqp\MessageHeadersMethods::HEADERS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
75
        return $this;
76
    }
77
}
78