DestinationMatches::validate()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 2
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\Response\Validation\ConstraintValidator;
6
7
use Exception;
8
use SimpleSAML\SAML2\Configuration\Destination;
9
use SimpleSAML\SAML2\Response\Validation\ConstraintValidator;
10
use SimpleSAML\SAML2\Response\Validation\Result;
11
use SimpleSAML\SAML2\XML\samlp\Response;
12
13
use function sprintf;
14
use function strval;
15
16
final class DestinationMatches implements ConstraintValidator
17
{
18
    /**
19
     * DestinationMatches constructor.
20
     *
21
     * @param \SimpleSAML\SAML2\Configuration\Destination $expectedDestination
22
     */
23
    public function __construct(
24
        private Destination $expectedDestination,
25
    ) {
26
    }
27
28
29
    /**
30
     * @param \SimpleSAML\SAML2\XML\samlp\Response $response
31
     * @param \SimpleSAML\SAML2\Response\Validation\Result $result
32
     */
33
    public function validate(Response $response, Result $result): void
34
    {
35
        $destination = $response->getDestination();
36
        if ($destination === null) {
37
            throw new Exception('No destination set in the response.');
38
        }
39
        if (!$this->expectedDestination->equals(new Destination($destination))) {
40
            $result->addError(sprintf(
41
                'Destination in response "%s" does not match the expected destination "%s"',
42
                $destination,
43
                strval($this->expectedDestination),
44
            ));
45
        }
46
    }
47
}
48