Test Setup Failed
Push — main ( e03535...dde247 )
by Pieter
03:39
created

SecurityScheme   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
eloc 29
c 2
b 1
f 1
dl 0
loc 76
rs 10
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
B validateProperties() 0 20 8
A validateFields() 0 5 3
1
<?php
2
3
4
namespace Apie\OpenapiSchema\Spec;
5
6
use Apie\CommonValueObjects\AuthenticationSchema;
7
use Apie\CommonValueObjects\Identifier;
8
use Apie\CommonValueObjects\Url;
9
use Apie\OpenapiSchema\Concerns\CompositeValueObjectWithExtension;
10
use Apie\OpenapiSchema\Exceptions\ApiKeySecuritySchemeRequiredFieldsMissing;
11
use Apie\OpenapiSchema\Exceptions\HttpSecuritySchemeRequiredFieldsMissing;
12
use Apie\OpenapiSchema\Exceptions\Oauth2SecuritySchemeRequiredFieldsMissing;
13
use Apie\OpenapiSchema\Exceptions\OpenIdSecuritySchemeRequiredFieldsMissing;
14
use Apie\OpenapiSchema\ValueObjects\ParameterIn;
15
use Apie\OpenapiSchema\ValueObjects\SecuritySchemeType;
16
use Apie\TypeJuggling\Exceptions\MissingValueException;
17
use Apie\ValueObjects\ValueObjectInterface;
18
use Throwable;
19
20
/**
21
 * @see https://swagger.io/specification/#security-scheme-object
22
 */
23
class SecurityScheme implements ValueObjectInterface
24
{
25
    use CompositeValueObjectWithExtension;
26
27
    /**
28
     * @var SecuritySchemeType
29
     */
30
    private $type;
31
32
    /**
33
     * @var string|null
34
     */
35
    private $description;
36
37
    /**
38
     * @var Identifier|null
39
     */
40
    private $name;
41
42
    /**
43
     * @var ParameterIn|null
44
     */
45
    private $in;
46
47
    /**
48
     * @var AuthenticationSchema|null
49
     */
50
    private $scheme;
51
52
    /**
53
     * @var string|null
54
     */
55
    private $bearerFormat;
56
57
    /**
58
     * @var OAuthFlows|null
59
     */
60
    private $flows;
61
62
    /**
63
     * @var Url|null
64
     */
65
    private $openIdConnectUrl;
66
67
    private function __construct()
68
    {
69
    }
70
71
    protected function validateProperties(): void
72
    {
73
        if (!$this->type) {
74
            return;
75
        }
76
        switch ($this->type->toNative()) {
77
            case SecuritySchemeType::API_KEY:
78
                $this->validateFields(new ApiKeySecuritySchemeRequiredFieldsMissing(), 'name', 'in');
79
                break;
80
            case SecuritySchemeType::HTTP:
81
                $this->validateFields(new HttpSecuritySchemeRequiredFieldsMissing(), 'scheme');
82
                if ($this->scheme->toNative() === AuthenticationSchema::BEARER && null === $this->bearerFormat) {
0 ignored issues
show
Bug introduced by
The method toNative() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

82
                if ($this->scheme->/** @scrutinizer ignore-call */ toNative() === AuthenticationSchema::BEARER && null === $this->bearerFormat) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
83
                    throw new MissingValueException('bearerFormat');
84
                }
85
                break;
86
            case SecuritySchemeType::OAUTH2:
87
                $this->validateFields(new Oauth2SecuritySchemeRequiredFieldsMissing(), 'flows');
88
                break;
89
            case SecuritySchemeType::OPEN_ID_CONNECT:
90
                $this->validateFields(new OpenIdSecuritySchemeRequiredFieldsMissing(), 'openIdConnectUrl');
91
        }
92
    }
93
94
    private function validateFields(Throwable $throwable, string... $fields)
95
    {
96
        foreach ($fields as $field) {
97
            if ($this->$field === null) {
98
                throw $throwable;
99
            }
100
        }
101
    }
102
}
103