Completed
Push — master ( 0eab77...b2f729 )
by Paul
05:35
created

ApiAuthenticationChain   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 72
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A resolve() 0 8 2
A addAuthenticationMethod() 0 4 1
A getAuthenticationMethods() 0 4 1
A getAuthenticationMethodsTypes() 0 9 2
A getAuthenticationMethod() 0 4 1
1
<?php
2
3
namespace Victoire\Bundle\APIBusinessEntityBundle\Chain;
4
5
use Victoire\Bundle\APIBusinessEntityBundle\Authentication\Interfaces\APIAuthenticationMethodInterface;
6
use Victoire\Bundle\APIBusinessEntityBundle\Chain\Exception\UnknownTokenTypeException;
7
8
class ApiAuthenticationChain
9
{
10
    protected $authenticationMethods;
11
12
    public function __construct()
13
    {
14
        $this->authenticationMethods = [];
15
    }
16
17
    /**
18
     * Finds the authentication handler that match with given token type.
19
     *
20
     * @param $tokenType
21
     *
22
     * @throws UnknownTokenTypeException
23
     *
24
     * @return mixed
25
     */
26
    public function resolve($tokenType)
27
    {
28
        if (!array_key_exists($tokenType, $this->authenticationMethods)) {
29
            throw new UnknownTokenTypeException();
30
        }
31
32
        return $this->authenticationMethods[$tokenType];
33
    }
34
35
    /**
36
     * Adds an authentication method.
37
     *
38
     * @param APIAuthenticationMethodInterface $authenticationMethod
39
     */
40
    public function addAuthenticationMethod(APIAuthenticationMethodInterface $authenticationMethod)
41
    {
42
        $this->authenticationMethods[$authenticationMethod::getType()] = $authenticationMethod;
43
    }
44
45
    /**
46
     * get authenticaton methods.
47
     *
48
     * @return array
49
     */
50
    public function getAuthenticationMethods()
51
    {
52
        return $this->authenticationMethods;
53
    }
54
55
    /**
56
     * Get all available authentication methods.
57
     *
58
     * @return array
59
     */
60
    public function getAuthenticationMethodsTypes()
61
    {
62
        $types = [];
63
        foreach ($this->authenticationMethods as $type => $authenticationMethod) {
64
            $types[] = $type;
65
        }
66
67
        return $types;
68
    }
69
70
    /**
71
     * @param string $alias
72
     *
73
     * @return APIAuthenticationMethodInterface
74
     */
75
    public function getAuthenticationMethod($alias)
76
    {
77
        return $this->authenticationMethods[$alias];
78
    }
79
}
80