1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dekalee\RedisSwarrotBundle\Broker; |
4
|
|
|
|
5
|
|
|
use Dekalee\RedisSwarrot\MessageProvider\RedisMessageProvider; |
6
|
|
|
use Dekalee\RedisSwarrot\MessagePublisher\RedisMessagePublisher; |
7
|
|
|
use Swarrot\Broker\MessageProvider\MessageProviderInterface; |
8
|
|
|
use Swarrot\Broker\MessagePublisher\MessagePublisherInterface; |
9
|
|
|
use Swarrot\SwarrotBundle\Broker\FactoryInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class RedisFactory |
13
|
|
|
*/ |
14
|
|
|
class RedisFactory implements FactoryInterface |
15
|
|
|
{ |
16
|
|
|
protected $connections = array(); |
17
|
|
|
protected $messageProviders = array(); |
18
|
|
|
protected $messagePublishers = array(); |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* {@inheritDoc} |
22
|
|
|
*/ |
23
|
|
|
public function addConnection($name, array $connection) |
24
|
|
|
{ |
25
|
|
|
$this->connections[$name] = $connection; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* getMessageProvider. |
30
|
|
|
* |
31
|
|
|
* @param string $name The name of the queue where the MessageProviderInterface will found messages |
32
|
|
|
* @param string $connection The name of the connection to use |
33
|
|
|
* |
34
|
|
|
* @return MessageProviderInterface |
35
|
|
|
*/ |
36
|
|
View Code Duplication |
public function getMessageProvider($name, $connection) |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
if (!isset($this->messageProviders[$connection][$name])) { |
39
|
|
|
if (!isset($this->messageProviders[$connection])) { |
40
|
|
|
$this->messageProviders[$connection] = array(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$channel = $this->getChannel($connection); |
44
|
|
|
|
45
|
|
|
$this->messageProviders[$connection][$name] = new RedisMessageProvider($channel, $name); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $this->messageProviders[$connection][$name]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* getMessagePublisher. |
53
|
|
|
* |
54
|
|
|
* @param string $name The name of the exchange where the MessagePublisher will publish |
55
|
|
|
* @param string $connection The name of the connection to use |
56
|
|
|
* |
57
|
|
|
* @return MessagePublisherInterface |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
public function getMessagePublisher($name, $connection) |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
if (!isset($this->messageProviders[$connection][$name])) { |
62
|
|
|
if (!isset($this->messageProviders[$connection])) { |
63
|
|
|
$this->messageProviders[$connection] = array(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$channel = $this->getChannel($connection); |
67
|
|
|
|
68
|
|
|
$this->messageProviders[$connection][$name] = new RedisMessagePublisher($channel, $name); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this->messageProviders[$connection][$name]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* getChannel. |
76
|
|
|
* |
77
|
|
|
* @param string $connection |
78
|
|
|
* |
79
|
|
|
* @return \Redis |
80
|
|
|
*/ |
81
|
|
|
protected function getChannel($connection) |
82
|
|
|
{ |
83
|
|
|
$client = new \Redis(); |
84
|
|
|
$client->connect($this->connections[$connection]['host'], $this->connections[$connection]['port']); |
85
|
|
|
|
86
|
|
|
if ('/' != $this->connections[$connection]['vhost']) { |
87
|
|
|
$client->select($this->connections[$connection]['vhost']); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $client; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
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.