RedisFactory   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 79
Duplicated Lines 35.44 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 28
loc 79
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addConnection() 0 4 1
A getMessageProvider() 14 14 3
A getMessagePublisher() 14 14 3
A getChannel() 0 11 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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)
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...
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)
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...
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