Passed
Pull Request — master (#1473)
by
unknown
55:04 queued 20:02
created

IntrospectionResponse   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 40
c 1
b 0
f 0
dl 0
loc 91
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setToken() 0 3 1
B generateHttpResponse() 0 45 6
A getExtraParams() 0 3 1
A setActive() 0 3 1
A setTokenType() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace League\OAuth2\Server\ResponseTypes;
6
7
use Psr\Http\Message\ResponseInterface;
8
9
class IntrospectionResponse implements IntrospectionResponseTypeInterface
10
{
11
    private bool $active = false;
12
13
    /**
14
     * @var non-empty-string|null
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string|null at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string|null.
Loading history...
15
     */
16
    private ?string $tokenType = null;
17
18
    /**
19
     * @var array<non-empty-string, mixed>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<non-empty-string, mixed> at position 2 could not be parsed: Unknown type name 'non-empty-string' at position 2 in array<non-empty-string, mixed>.
Loading history...
20
     */
21
    private ?array $token = null;
22
23
    public function setActive(bool $active): void
24
    {
25
        $this->active = $active;
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function setTokenType(string $tokenType): void
32
    {
33
        $this->tokenType = $tokenType;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function setToken(array $token): void
40
    {
41
        $this->token = $token;
42
    }
43
44
    public function generateHttpResponse(ResponseInterface $response): ResponseInterface
45
    {
46
        $params = [
47
            'active' => $this->active,
48
        ];
49
50
        if ($this->active === true && $this->tokenType !== null && $this->token !== null) {
51
            if ($this->tokenType === 'access_token') {
52
                $params = array_merge($params, array_filter([
53
                    'scope' => $this->token['scope'] ?? implode(' ', $this->token['scopes'] ?? []),
54
                    'client_id' => $this->token['client_id'] ?? $this->token['aud'][0] ?? null,
55
                    'username' => $this->token['username'] ?? null,
56
                    'token_type' => 'Bearer',
57
                    'exp' => $this->token['exp'] ?? null,
58
                    'iat' => $this->token['iat'] ?? null,
59
                    'nbf' => $this->token['nbf'] ?? null,
60
                    'sub' => $this->token['sub'] ?? null,
61
                    'aud' => $this->token['aud'] ?? null,
62
                    'iss' => $this->token['iss'] ?? null,
63
                    'jti' => $this->token['jti'] ?? null,
64
                ]));
65
            } elseif ($this->tokenType === 'refresh_token') {
66
                $params = array_merge($params, array_filter([
67
                    'scope' => implode(' ', $this->token['scopes'] ?? []),
68
                    'client_id' => $this->token['client_id'] ?? null,
69
                    'exp' => $this->token['expire_time'] ?? null,
70
                    'sub' => $this->token['user_id'] ?? null,
71
                    'jti' => $this->token['refresh_token_id'] ?? null,
72
                ]));
73
            }
74
75
            $params = array_merge($params, $this->getExtraParams($this->tokenType, $this->token));
76
        }
77
78
        $params = json_encode($params, flags: JSON_THROW_ON_ERROR);
79
80
        $response = $response
81
            ->withStatus(200)
82
            ->withHeader('pragma', 'no-cache')
83
            ->withHeader('cache-control', 'no-store')
84
            ->withHeader('content-type', 'application/json; charset=UTF-8');
85
86
        $response->getBody()->write($params);
87
88
        return $response;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response returns the type Psr\Http\Message\MessageInterface which includes types incompatible with the type-hinted return Psr\Http\Message\ResponseInterface.
Loading history...
89
    }
90
91
    /**
92
     * @param non-empty-string               $tokenType
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
93
     * @param array<non-empty-string, mixed> $token
94
     *
95
     * @return array<non-empty-string, mixed>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<non-empty-string, mixed> at position 2 could not be parsed: Unknown type name 'non-empty-string' at position 2 in array<non-empty-string, mixed>.
Loading history...
96
     */
97
    protected function getExtraParams(string $tokenType, array $token): array
0 ignored issues
show
Unused Code introduced by
The parameter $token is not used and could be removed. ( Ignorable by Annotation )

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

97
    protected function getExtraParams(string $tokenType, /** @scrutinizer ignore-unused */ array $token): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $tokenType is not used and could be removed. ( Ignorable by Annotation )

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

97
    protected function getExtraParams(/** @scrutinizer ignore-unused */ string $tokenType, array $token): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
98
    {
99
        return [];
100
    }
101
}
102