AmqpTransport   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 32
rs 10
ccs 0
cts 12
cp 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A send() 0 20 2
A __construct() 0 5 1
1
<?php
2
declare(strict_types=1);
3
4
/*
5
 * This file is part of the php-gelf package.
6
 *
7
 * (c) Benjamin Zikarsky <http://benjamin-zikarsky.de>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Gelf\Transport;
14
15
use AMQPExchange;
16
use AMQPQueue;
17
use Gelf\Encoder\JsonEncoder as DefaultEncoder;
18
use Gelf\MessageInterface as Message;
19
20
/**
21
 * Class AmqpTransport
22
 *
23
 * @package Gelf\Transport
24
 * @see http://php.net/manual/pl/book.amqp.php
25
 */
26
class AmqpTransport extends AbstractTransport
27
{
28
    public function __construct(
29
        private AMQPExchange $exchange,
30
        private AMQPQueue $queue
31
    ) {
32
        parent::__construct();
33
    }
34
35
    /**
36
     * @inheritdoc
37
     */
38
    public function send(Message $message): int
39
    {
40
        $rawMessage = $this->getMessageEncoder()->encode($message);
41
42
        $attributes = [
43
            'Content-type' => 'application/json'
44
        ];
45
46
        // if queue is durable then mark message as 'persistent'
47
        if (($this->queue->getFlags() & AMQP_DURABLE) > 0) {
48
            $attributes['delivery_mode'] = 2;
49
        }
50
51
        $this->exchange->publish(
52
            $rawMessage,
53
            $this->queue->getName(),
54
            AMQP_NOPARAM,
55
            $attributes
56
        );
57
        return 1;
58
    }
59
}
60