AmqpTransport::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
rs 10
ccs 0
cts 0
cp 0
crap 2
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