ConnectionFactory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 4
Bugs 0 Features 3
Metric Value
wmc 6
c 4
b 0
f 3
lcom 1
cbo 4
dl 0
loc 83
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createDefaultConnection() 0 12 2
A createConnection() 0 13 2
A getConnectionOptions() 0 15 1
1
<?php
2
3
/*
4
 * This file is part of the NatsBundle package.
5
 *
6
 * (c) Issel Guberna <[email protected]>
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 Octante\NatsBundle\Services;
13
14
use Nats\Connection;
15
use Nats\ConnectionOptions;
16
use Octante\NatsBundle\Connection\ConnectionWrapper;
17
use Octante\NatsBundle\Exceptions\ConnectionNameNotFound;
18
19
class ConnectionFactory
20
{
21
    /**
22
     * @var array
23
     */
24
    private $configuration;
25
26
    /**
27
     * @var
28
     */
29
    private $logger;
30
31
    /**
32
     * @param array $configuration
33
     */
34
    public function __construct($configuration, $logger)
35
    {
36
        $this->configuration = $configuration;
37
        $this->logger = $logger;
38
    }
39
40
    /**
41
     * @return Connection
42
     *
43
     * @throws ConnectionNameNotFound
44
     */
45
    public function createDefaultConnection()
46
    {
47
        if (!isset($this->configuration['default_connection'])) {
48
            throw new ConnectionNameNotFound();
49
        }
50
51
        return new Connection(
52
            $this->getConnectionOptions(
53
                $this->configuration['default_connection']
54
            )
55
        );
56
    }
57
58
    /**
59
     * @param string $connectionName
60
     *
61
     * @throws \Octante\NatsBundle\Exceptions\ConnectionNameNotFound
62
     *
63
     * @return Connection
64
     */
65
    public function createConnection($connectionName)
66
    {
67
        if (!isset($this->configuration[$connectionName])) {
68
            throw new ConnectionNameNotFound();
69
        }
70
71
        $config = $this->configuration[$connectionName];
72
73
        return new ConnectionWrapper(
74
            new Connection($this->getConnectionOptions($config)),
75
            $this->logger
76
        );
77
    }
78
79
    /**
80
     * Return a ConnectionOptions instance getting parameters from config.
81
     *
82
     * @param $config
83
     *
84
     * @return ConnectionOptions
85
     */
86
    private function getConnectionOptions($config)
87
    {
88
        $connectionOptions = new ConnectionOptions();
89
        $connectionOptions->setHost($config['host'])
90
            ->setPort($config['port'])
91
            ->setUser($config['user'])
92
            ->setPass($config['password'])
93
            ->setVerbose($config['verbose'])
94
            ->setReconnect($config['reconnect'])
95
            ->setVersion($config['version'])
96
            ->setPedantic($config['pedantic'])
97
            ->setLang($config['lang']);
98
99
        return $connectionOptions;
100
    }
101
}
102