Completed
Pull Request — master (#3)
by Márk
02:31
created

Chain   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 71
wmc 8
lcom 1
cbo 1
ccs 22
cts 22
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addAuthentication() 0 4 1
A getAuthenticationChain() 0 4 1
A setAuthenticationChain() 0 8 2
A clearAuthenticationChain() 0 4 1
A authenticate() 0 8 2
1
<?php
2
3
namespace Http\Message\Authentication;
4
5
use Http\Message\Authentication;
6
use Psr\Http\Message\RequestInterface;
7
8
/**
9
 * Authenticate a PSR-7 Request with a multiple authentication methods.
10
 *
11
 * @author Márk Sági-Kazár <[email protected]>
12
 */
13
final class Chain implements Authentication
14
{
15
    /**
16
     * @var Authentication[]
17
     */
18
    private $authenticationChain = [];
19
20
    /**
21
     * @param Authentication[] $authenticationChain
22
     */
23 7
    public function __construct(array $authenticationChain = [])
24
    {
25 7
        $this->setAuthenticationChain($authenticationChain);
26 7
    }
27
28
    /**
29
     * Adds an Authentication method to the chain.
30
     *
31
     * The order of authentication methods SHOULD NOT matter.
32
     *
33
     * @param Authentication $authentication
34
     */
35 5
    public function addAuthentication(Authentication $authentication)
36
    {
37 5
        $this->authenticationChain[] = $authentication;
38 5
    }
39
40
    /**
41
     * Returns the current authentication chain.
42
     *
43
     * @return Authentication[]
44
     */
45 4
    public function getAuthenticationChain()
46
    {
47 4
        return $this->authenticationChain;
48
    }
49
50
    /**
51
     * Replaces the current authentication chain.
52
     *
53
     * @param array $authenticationChain
54
     */
55 7
    public function setAuthenticationChain(array $authenticationChain)
56
    {
57 7
        $this->clearAuthenticationChain();
58
59 7
        foreach ($authenticationChain as $authentication) {
60 5
            $this->addAuthentication($authentication);
61 7
        }
62 7
    }
63
64
    /**
65
     * Clears the authentication chain.
66
     */
67 7
    public function clearAuthenticationChain()
68
    {
69 7
        $this->authenticationChain = [];
70 7
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 1
    public function authenticate(RequestInterface $request)
76
    {
77 1
        foreach ($this->authenticationChain as $authentication) {
78 1
            $request = $authentication->authenticate($request);
79 1
        }
80
81 1
        return $request;
82
    }
83
}
84