Passed
Pull Request — master (#1473)
by
unknown
34:32
created

IntrospectionResponse::setTokenType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
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
    private ?string $tokenType = null;
14
15
    /**
16
     * @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...
17
     */
18
    private ?array $token = null;
19
20
    public function setActive(bool $active): void
21
    {
22
        $this->active = $active;
23
    }
24
25
    public function setTokenType(string $tokenType): void
26
    {
27
        $this->tokenType = $tokenType;
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function setToken(array $token): void
34
    {
35
        $this->token = $token;
36
    }
37
38
    public function generateHttpResponse(ResponseInterface $response): ResponseInterface
39
    {
40
        $params = [
41
            'active' => $this->active,
42
        ];
43
44
        if ($this->active === true && $this->tokenType !== null && $this->token !== null) {
45
            if ($this->tokenType === 'access_token') {
46
                $params = array_merge($params, array_filter([
47
                    'scope' => $token['scope'] ?? implode(' ', $token['scopes'] ?? []),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $token seems to never exist and therefore isset should always be false.
Loading history...
48
                    'client_id' => $token['client_id'] ?? $token['aud'][0] ?? null,
49
                    'username' => $token['username'] ?? null,
50
                    'token_type' => 'Bearer',
51
                    'exp' => $token['exp'] ?? null,
52
                    'iat' => $token['iat'] ?? null,
53
                    'nbf' => $token['nbf'] ?? null,
54
                    'sub' => $token['sub'] ?? null,
55
                    'aud' => $token['aud'] ?? null,
56
                    'iss' => $token['iss'] ?? null,
57
                    'jti' => $token['jti'] ?? null,
58
                ]));
59
            } elseif ($this->tokenType === 'refresh_token') {
60
                $params = array_merge($params, array_filter([
61
                    'scope' => implode(' ', $token['scopes'] ?? []),
62
                    'client_id' => $token['client_id'] ?? null,
63
                    'exp' => $token['expire_time'] ?? null,
64
                    'sub' => $token['user_id'] ?? null,
65
                    'jti' => $token['refresh_token_id'] ?? null,
66
                ]));
67
            }
68
69
            $params = array_merge($params, $this->getExtraParams($this->tokenType, $this->token));
70
        }
71
72
        $params = json_encode($params, flags: JSON_THROW_ON_ERROR);
73
74
        $response = $response
75
            ->withStatus(200)
76
            ->withHeader('pragma', 'no-cache')
77
            ->withHeader('cache-control', 'no-store')
78
            ->withHeader('content-type', 'application/json; charset=UTF-8');
79
80
        $response->getBody()->write($params);
81
82
        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...
83
    }
84
85
    /**
86
     * @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...
87
     * @param array<non-empty-string, mixed> $token
88
     * @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...
89
     */
90
    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

90
    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

90
    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...
91
    {
92
        return [];
93
    }
94
}
95