Passed
Push — master ( 14b87a...2ae7fe )
by Benjamin
09:12 queued 07:22
created

AmqpTransport::send()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 11
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 20
ccs 0
cts 16
cp 0
crap 6
rs 9.9
1
<?php
2
3
/*
4
 * This file is part of the php-gelf package.
5
 *
6
 * (c) Benjamin Zikarsky <http://benjamin-zikarsky.de>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Gelf\Transport;
13
14
use AMQPExchange;
15
use AMQPQueue;
16
use Gelf\Encoder\JsonEncoder as DefaultEncoder;
17
use Gelf\MessageInterface as Message;
18
19
/**
20
 * Class AmqpTransport
21
 *
22
 * @package Gelf\Transport
23
 * @see http://php.net/manual/pl/book.amqp.php
24
 */
25
class AmqpTransport extends AbstractTransport
26
{
27
    /**
28
     * @var AMQPExchange $exchange
29
     */
30
    protected $exchange;
31
32
    /**
33
     * @var AMQPQueue $exchange
34
     */
35
    protected $queue;
36
37
    /**
38
     * @param AMQPExchange $exchange
39
     * @param AMQPQueue $queue
40
     */
41
    public function __construct(AMQPExchange $exchange, AMQPQueue $queue)
42
    {
43
        $this->queue = $queue;
44
        $this->exchange = $exchange;
45
        $this->messageEncoder = new DefaultEncoder();
46
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51
    public function send(Message $message)
52
    {
53
        $rawMessage = $this->getMessageEncoder()->encode($message);
54
55
        $attributes = array(
56
            'Content-type' => 'application/json'
57
        );
58
59
        // if queue is durable then mark message as 'persistent'
60
        if (($this->queue->getFlags() & AMQP_DURABLE) > 0) {
61
            $attributes['delivery_mode'] = 2;
62
        }
63
64
        $this->exchange->publish(
65
            $rawMessage,
66
            $this->queue->getName(),
67
            AMQP_NOPARAM,
68
            $attributes
69
        );
70
        return 1;
71
    }
72
}
73