IsSuccessful   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 53
rs 10
c 0
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 6 2
A buildMessage() 0 18 5
A truncateStatus() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\Response\Validation\ConstraintValidator;
6
7
use SimpleSAML\SAML2\Constants as C;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\Constants was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use SimpleSAML\SAML2\Response\Validation\ConstraintValidator;
9
use SimpleSAML\SAML2\Response\Validation\Result;
10
use SimpleSAML\SAML2\XML\samlp\Response;
11
use SimpleSAML\SAML2\XML\samlp\Status;
12
13
use function implode;
14
use function sprintf;
15
use function strlen;
16
use function substr;
17
18
class IsSuccessful implements ConstraintValidator
19
{
20
    /**
21
     * @param \SimpleSAML\SAML2\XML\samlp\Response $response
22
     * @param \SimpleSAML\SAML2\Response\Validation\Result $result
23
     */
24
    public function validate(
25
        Response $response,
26
        Result $result,
27
    ): void {
28
        if (!$response->isSuccess()) {
29
            $result->addError($this->buildMessage($response->getStatus()));
30
        }
31
    }
32
33
34
    /**
35
     * @param \SimpleSAML\SAML2\XML\samlp\Status $responseStatus
36
     */
37
    private function buildMessage(Status $responseStatus): string
38
    {
39
        $subCodes = [];
40
        $statusCode = $responseStatus->getStatusCode();
41
42
        $codes = $statusCode->getSubCodes();
43
        if (!empty($codes)) {
44
            foreach ($codes as $code) {
45
                $subCodes[] = $this->truncateStatus($code->getValue()->getValue());
46
            }
47
        }
48
        $statusMessage = $responseStatus->getStatusMessage();
49
50
        return sprintf(
51
            '%s%s%s',
52
            $this->truncateStatus($statusCode->getValue()->getValue()),
53
            $subCodes ? '/' . implode('/', $subCodes) : '',
54
            $statusMessage ? ' ' . $statusMessage->getContent()->getValue() : '',
55
        );
56
    }
57
58
59
    /**
60
     * Truncate the status if it is prefixed by its urn.
61
     * @param string $status
62
     */
63
    private function truncateStatus(string $status): string
64
    {
65
        if (!str_starts_with($status, C::STATUS_PREFIX)) {
66
            return $status;
67
        }
68
69
        $prefixLength = strlen(C::STATUS_PREFIX);
70
        return substr($status, $prefixLength);
71
    }
72
}
73