Completed
Push — master ( a552ca...886306 )
by Benjamin
02:11
created

AmqpTransport   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 88.24%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 48
ccs 15
cts 17
cp 0.8824
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A send() 0 21 2
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 4
    public function __construct(AMQPExchange $exchange, AMQPQueue $queue)
42
    {
43 4
        $this->queue = $queue;
44 4
        $this->exchange = $exchange;
45 4
        $this->messageEncoder = new DefaultEncoder();
46 4
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51 1
    public function send(Message $message)
52
    {
53 1
        $rawMessage = $this->getMessageEncoder()->encode($message);
54
55
        $attributes = array(
56
            'Content-type' => 'application/json'
57 1
        );
58
59
        // if queue is durable then mark message as 'persistent'
60 1
        if (($this->queue->getFlags() & AMQP_DURABLE) > 0) {
61
            $attributes['delivery_mode'] = 2;
62
        }
63
64 1
        $this->exchange->publish(
65 1
            $rawMessage,
66 1
            $this->queue->getName(),
67 1
            AMQP_NOPARAM,
68
            $attributes
69 1
        );
70 1
        return 1;
71
    }
72
}
73