Passed
Pull Request — master (#1255)
by
unknown
56:50 queued 21:45
created

AbstractResponseType::setValidity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace League\OAuth2\Server\ResponseTypes\Introspection;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Psr\Http\Message\ServerRequestInterface;
7
8
abstract class AbstractResponseType implements ResponseTypeInterface
9
{
10
    /**
11
     * @var bool
12
     */
13
    protected $valid = false;
14
15
    /**
16
     * @var ServerRequestInterface
17
     */
18
    protected $request;
19
20
    /**
21
     * Set the validity of the response.
22
     *
23
     * @param bool $bool
24
     */
25
    public function setValidity(bool $bool): void
26
    {
27
        $this->valid = $bool;
28
    }
29
30
    /**
31
     * Set the request.
32
     *
33
     * @param ServerRequestInterface $request
34
     */
35
    public function setRequest(ServerRequestInterface $request): void
36
    {
37
        $this->request = $request;
38
    }
39
40
    /**
41
     * Return the valid introspection parameters.
42
     *
43
     * @return array
44
     */
45
    protected function validIntrospectionResponse(): array
46
    {
47
        $responseParams = [
48
            'active' => true,
49
        ];
50
51
        return \array_merge($this->getExtraParams(), $responseParams);
52
    }
53
54
    /**
55
     * Return the invalid introspection parameters.
56
     *
57
     * @return array
58
     */
59
    protected function invalidIntrospectionResponse(): array
60
    {
61
        return [
62
            'active' => false,
63
        ];
64
    }
65
66
    /**
67
     * Extract the introspection response.
68
     *
69
     * @return array
70
     */
71
    public function getIntrospectionResponseParams(): array
72
    {
73
        return $this->isValid() ?
74
            $this->validIntrospectionResponse() :
75
            $this->invalidIntrospectionResponse();
76
    }
77
78
    /**
79
     * Check if the response is valid.
80
     *
81
     * @return bool
82
     */
83
    protected function isValid(): bool
84
    {
85
        return $this->valid === true;
86
    }
87
88
    /**
89
     * Generate a HTTP response.
90
     *
91
     * @param ResponseInterface $response
92
     *
93
     * @return ResponseInterface
94
     */
95
    public function generateHttpResponse(ResponseInterface $response): ResponseInterface
96
    {
97
        $responseParams = $this->getIntrospectionResponseParams();
98
99
        $response = $response
100
                ->withStatus(200)
101
                ->withHeader('pragma', 'no-cache')
102
                ->withHeader('cache-control', 'no-store')
103
                ->withHeader('content-type', 'application/json; charset=UTF-8');
104
105
        $response->getBody()->write(\json_encode($responseParams));
106
107
        return $response;
108
    }
109
110
    /**
111
     * Add custom fields to your Introspection response here, then add your introspection
112
     * response to IntrospectionServer constructor to pull in your version of
113
     * this class rather than the default.
114
     *
115
     * @return array
116
     */
117
    protected function getExtraParams(): array
118
    {
119
        return [];
120
    }
121
}
122