Completed
Push — master ( e9401f...2161bf )
by Daniel
03:27
created

Publisher::getFromName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Cmobi\RabbitmqBundle\Transport\Subscriber;
4
5
use Cmobi\RabbitmqBundle\Connection\CmobiAMQPChannel;
6
use Cmobi\RabbitmqBundle\Connection\CmobiAMQPConnectionInterface;
7
use Cmobi\RabbitmqBundle\Connection\ConnectionManager;
8
use Cmobi\RabbitmqBundle\Queue\CmobiAMQPMessage;
9
use Cmobi\RabbitmqBundle\Queue\QueueProducerInterface;
10
11
class Publisher implements QueueProducerInterface
12
{
13
    private $connectionManager;
14
    private $channel;
15
    private $fromName;
16
    private $queueName;
17
    private $exchange;
18
    private $exchangeType;
19
20
    public function __construct(
21
        $exchange,
22
        $exchangeType = ExchangeType::FANOUT,
23
        ConnectionManager $manager,
24
        $fromName = '',
25
        $queueName = ''
26
    ) {
27
        $this->exchange = $exchange;
28
        $this->exchangeType = $exchangeType;
29
        $this->queueName = $queueName;
30
        $this->fromName = $fromName;
31
        $this->connectionManager = $manager;
32
    }
33
34
    /**
35
     * @param $data
36
     * @param $expire
37
     * @param $priority
38
     */
39
    public function publish($data, $expire = self::DEFAULT_TTL, $priority = self::PRIORITY_LOW)
40
    {
41
        $this->refreshChannel();
42
        $queueBag = new SubscriberQueueBag($this->getExchange(), $this->getExchangeType(), $this->getQueueName());
43
        $this->getChannel()->exchangeDeclare($queueBag->getExchangeDeclare());
0 ignored issues
show
Security Bug introduced by
It seems like $queueBag->getExchangeDeclare() targeting Cmobi\RabbitmqBundle\Tra...g::getExchangeDeclare() can also be of type false; however, Cmobi\RabbitmqBundle\Con...nnel::exchangeDeclare() does only seem to accept array, did you maybe forget to handle an error condition?
Loading history...
44
        $msg = new CmobiAMQPMessage((string) $data);
45
        $this->getChannel()->basic_publish($msg, $queueBag->getExchange());
46
47
        $this->getChannel()->close();
48
        $this->connectionManager->getConnection()->close();
49
    }
50
51
    /**
52
     * @return CmobiAMQPChannel
53
     */
54 View Code Duplication
    public function refreshChannel()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
    {
56
        /** @var CmobiAMQPConnectionInterface $connection */
57
        $connection = $this->connectionManager->getConnection();
58
59
        if (!$connection->isConnected()) {
60
            $connection->reconnect();
61
        }
62
        $this->channel = $connection->channel();
63
64
        return $this->channel;
65
    }
66
67
    /**
68
     * @return CmobiAMQPChannel
69
     */
70
    public function getChannel()
71
    {
72
        return $this->channel;
73
    }
74
75
    /**
76
     * @return string
77
     */
78
    public function getQueueName()
79
    {
80
        return $this->queueName;
81
    }
82
83
    /**
84
     * Return caller name.
85
     *
86
     * @return string
87
     */
88
    public function getFromName()
89
    {
90
        return $this->fromName;
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    public function getExchange()
97
    {
98
        return $this->exchange;
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    public function getExchangeType()
105
    {
106
        return $this->exchangeType;
107
    }
108
}
109