Completed
Push — master ( 4b394d...0ca4f0 )
by Damien
09:55
created

Response   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 9
dl 0
loc 102
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A addValidators() 0 15 2
A validate() 0 13 2
A validateAssertions() 0 16 2
A addErrorsToResult() 0 6 2
1
<?php
2
3
4
namespace flipbox\saml\core\validators;
5
6
use flipbox\saml\core\records\AbstractProvider;
7
use SAML2\Configuration\Destination;
8
use SAML2\Response\Validation\ConstraintValidator\DestinationMatches;
9
use SAML2\Response\Validation\ConstraintValidator\IsSuccessful;
10
use SAML2\Response\Validation\Result as ResponseResult;
11
use SAML2\Assertion\Validation\Result as AssertionResult;
12
use SAML2\Response as SamlResponse;
13
14
class Response
15
{
16
    /**
17
     * @var AbstractProvider
18
     */
19
    private $identityProvider;
20
21
    /**
22
     * @var AbstractProvider
23
     */
24
    private $serviceProvider;
25
26
    /**
27
     * @var array
28
     */
29
    private $validators = [];
30
31
    /**
32
     * Response constructor.
33
     * @param AbstractProvider $identityProvider
34
     * @param AbstractProvider $serviceProvider
35
     */
36
    public function __construct(
37
        AbstractProvider $identityProvider,
38
        AbstractProvider $serviceProvider
39
    )
40
    {
41
42
        $this->identityProvider = $identityProvider;
43
        $this->serviceProvider = $serviceProvider;
44
45
        $this->addValidators();
46
    }
47
48
    private function addValidators()
49
    {
50
        $this->validators = [
51
            new IsSuccessful(),
52
            new DestinationMatches(
53
                new Destination(
54
                    $this->serviceProvider->firstSpAcsService()->getLocation()
55
                )
56
            ),
57
58
        ];
59
        if ($key = $this->identityProvider->signingXMLSecurityKey()) {
60
            $this->validators[] = new SignedElement($key);
61
        }
62
    }
63
64
    /**
65
     * @param $response
66
     * @return ResponseResult
67
     */
68
    public function validate($response): ResponseResult
69
    {
70
        $responseResult = new ResponseResult();
71
        foreach ($this->validators as $validator) {
72
            $validator->validate($response, $responseResult);
73
        }
74
75
        $this->validateAssertions($response, $responseResult);
76
77
78
        return $responseResult;
79
80
    }
81
82
    /**
83
     * @param SamlResponse $response
84
     * @param ResponseResult $responseResult
85
     */
86
    protected function validateAssertions(SamlResponse $response, ResponseResult $responseResult)
87
    {
88
        $assertionResult = null;
0 ignored issues
show
Unused Code introduced by
$assertionResult is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
89
        foreach ($response->getAssertions() as $assertion) {
90
            $validator = new Assertion(
91
                $response,
92
                $this->identityProvider,
93
                $this->serviceProvider
94
            );
95
96
            $assertionResult = $validator->validate($assertion);
97
98
            $this->addErrorsToResult($responseResult, $assertionResult);
99
        }
100
101
    }
102
103
    /**
104
     * @param ResponseResult $reponseResult
105
     * @param AssertionResult $assertionResult
106
     */
107
    private function addErrorsToResult(ResponseResult $reponseResult, AssertionResult $assertionResult)
108
    {
109
        foreach ($assertionResult->getErrors() as $error) {
110
            $reponseResult->addError($error);
111
        }
112
    }
113
114
115
}