Authenticator::get()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
crap 3
1
<?php
2
3
namespace ButterAMQP\Security;
4
5
use ButterAMQP\Exception\UnsupportedSecurityMechanismException;
6
use ButterAMQP\Security\Mechanism\AMQPlainMechanism;
7
use ButterAMQP\Security\Mechanism\PlainMechanism;
8
9
class Authenticator implements AuthenticatorInterface
10
{
11
    /**
12
     * @var MechanismInterface[]
13
     */
14
    private $mechanisms = [];
15
16
    /**
17
     * @param MechanismInterface[] $mechanisms
18
     */
19
    public function __construct(array $mechanisms)
20
    {
21 20
        array_walk($mechanisms, function (MechanismInterface $mechanism) {
22 20
            $this->mechanisms[$mechanism->getName()] = $mechanism;
23 20
        });
24 20
    }
25
26
    /**
27
     * @return Authenticator
28
     */
29 18
    public static function build()
30
    {
31 18
        return new self([
32 18
            new AMQPlainMechanism(),
33 18
            new PlainMechanism(),
34 18
        ]);
35
    }
36
37
    /**
38
     * @param array $mechanisms
39
     *
40
     * @return MechanismInterface
41
     *
42
     * @throws \Exception
43
     */
44 20
    public function get(array $mechanisms)
45
    {
46 20
        foreach ($mechanisms as $mechanism) {
47 20
            if (isset($this->mechanisms[$mechanism])) {
48 19
                return $this->mechanisms[$mechanism];
49
            }
50 2
        }
51
52 1
        throw new UnsupportedSecurityMechanismException(sprintf(
53 1
            'Non of the mechanisms "%s" is supported',
54 1
            implode('", "', $mechanisms)
55 1
        ));
56
    }
57
}
58