Completed
Pull Request — master (#1)
by Thibaud
02:33 queued 14s
created

AmqpConfiguration::getTimeout()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Alchemy\Queue\Amqp;
4
5
class AmqpConfiguration
6
{
7
    /**
8
     * @param array $parameters
9
     * @return AmqpConfiguration
10
     */
11 8
    public static function parse(array $parameters)
12
    {
13 8
        $configuration = new self();
14
15 8
        $configuration->host = self::extractValueOrDefault($parameters, 'host', $configuration->host);
16 8
        $configuration->vhost = self::extractValueOrDefault($parameters, 'vhost', $configuration->vhost);
17 8
        $configuration->port = self::extractValueOrDefault($parameters, 'port', $configuration->port);
18 8
        $configuration->user = self::extractValueOrDefault($parameters, 'user', $configuration->user);
19 8
        $configuration->password = self::extractValueOrDefault($parameters, 'password', $configuration->password);
20 8
        $configuration->exchange = self::extractValueOrDefault($parameters, 'exchange', $configuration->exchange);
21 8
        $configuration->timeout = self::extractValueOrDefault($parameters, 'timeout', $configuration->timeout);
22
23 8
        $configuration->deadLetterExchange = self::extractValueOrDefault(
24 6
            $parameters,
25 8
            'dead-letter-exchange',
26 8
            $configuration->deadLetterExchange
27 6
        );
28
29 8
        $configuration->queue = self::extractValueOrDefault($parameters, 'queue', $configuration->queue);
30
31 8
        return $configuration;
32
    }
33
34
    /**
35
     * @param array $parameters
36
     * @param string $key
37
     * @param mixed|null $default
38
     * @return mixed Value set in array or default value
39
     */
40 8
    private static function extractValueOrDefault(array $parameters, $key, $default = null)
41
    {
42 8
        return isset($parameters[$key]) ? $parameters[$key] : $default;
43
    }
44
45
    /**
46
     * @var string
47
     */
48
    private $host = 'localhost';
49
50
    /**
51
     * @var string
52
     */
53
    private $vhost = '/';
54
55
    /**
56
     * @var int
57
     */
58
    private $port = 5672;
59
60
    /**
61
     * @var string
62
     */
63
    private $user = 'guest';
64
65
    /**
66
     * @var string
67
     */
68
    private $password = 'guest';
69
70
    /**
71
     * @var string
72
     */
73
    private $deadLetterExchange = '';
74
75
    /**
76
     * @var string
77
     */
78
    private $exchange = 'alchemy-exchange';
79
80
    /**
81
     * @var string
82
     */
83
    private $queue = 'alchemy-queue';
84
85
    /**
86
     * @var int
87
     */
88
    private $timeout = 0;
89
90
    /**
91
     * @return string
92
     */
93 8
    public function getHost()
94
    {
95 8
        return $this->host;
96
    }
97
98
    /**
99
     * @return string
100
     */
101 8
    public function getVhost()
102
    {
103 8
        return $this->vhost;
104
    }
105
106
    /**
107
     * @return int
108
     */
109 8
    public function getPort()
110
    {
111 8
        return $this->port;
112
    }
113
114
    /**
115
     * @return string
116
     */
117 8
    public function getUser()
118
    {
119 8
        return $this->user;
120
    }
121
122
    /**
123
     * @return string
124
     */
125 8
    public function getPassword()
126
    {
127 8
        return $this->password;
128
    }
129
130
    /**
131
     * @return string
132
     */
133 12
    public function getDeadLetterExchange()
134
    {
135 12
        return $this->deadLetterExchange;
136
    }
137
138
    /**
139
     * @return string
140
     */
141 12
    public function getExchange()
142
    {
143 12
        return $this->exchange;
144
    }
145
146
    /**
147
     * @return string
148
     */
149 4
    public function getQueue()
150
    {
151 4
        return $this->queue;
152
    }
153
154
    /**
155
     * @return int
156
     */
157 4
    public function getTimeout()
158
    {
159 4
        return $this->timeout;
160
    }
161
162 8
    public function toConnectionArray()
163
    {
164
        return [
165 8
            'host' => $this->host,
166 8
            'port' => $this->port,
167 8
            'vhost' => $this->vhost,
168 8
            'login' => $this->user,
169 8
            'password' => $this->password
170 6
        ];
171
    }
172
}
173
174