Completed
Pull Request — master (#925)
by Steve
01:48
created

IntrospectionResponse   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 114
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setValidity() 0 4 1
A setRequest() 0 4 1
A validIntrospectionResponse() 0 8 1
A invalidIntrospectionResponse() 0 6 1
A getIntrospectionResponseParams() 0 6 2
A isValid() 0 4 1
A generateHttpResponse() 0 14 1
A getExtraParams() 0 4 1
1
<?php
2
3
namespace League\OAuth2\Server\ResponseTypes;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Psr\Http\Message\ServerRequestInterface;
7
8
class IntrospectionResponse extends AbstractResponseType
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 3
    public function setValidity(bool $bool)
26
    {
27 3
        $this->valid = $bool;
28 3
    }
29
30
    /**
31
     * Set the request.
32
     *
33
     * @param ServerRequestInterface $request
34
     */
35 1
    public function setRequest(ServerRequestInterface $request)
36
    {
37 1
        $this->request = $request;
38 1
    }
39
40
    /**
41
     * Return the valid introspection parameters.
42
     *
43
     * @return array
44
     */
45 1
    protected function validIntrospectionResponse()
46
    {
47
        $responseParams = [
48 1
            'active' => true,
49
        ];
50
51 1
        return array_merge($this->getExtraParams(), $responseParams);
52
    }
53
54
    /**
55
     * Return the invalid introspection parameters.
56
     *
57
     * @return array
58
     */
59 2
    protected function invalidIntrospectionResponse()
60
    {
61
        return [
62 2
            'active' => false,
63
        ];
64
    }
65
66
    /**
67
     * Extract the introspection response.
68
     *
69
     * @return array
70
     */
71 5
    public function getIntrospectionResponseParams()
72
    {
73 5
        return $this->isValid() ?
74 3
            $this->validIntrospectionResponse() :
75 5
            $this->invalidIntrospectionResponse();
76
    }
77
78
    /**
79
     * Check if the response is valid.
80
     *
81
     * @return bool
82
     */
83 5
    protected function isValid()
84
    {
85 5
        return $this->valid === true;
86
    }
87
88
    /**
89
     * Generate a HTTP response.
90
     *
91
     * @param ResponseInterface $response
92
     *
93
     * @return ResponseInterface
94
     */
95 3
    public function generateHttpResponse(ResponseInterface $response)
96
    {
97 3
        $responseParams = $this->getIntrospectionResponseParams();
98
99
        $response = $response
100 3
                ->withStatus(200)
101 3
                ->withHeader('pragma', 'no-cache')
102 3
                ->withHeader('cache-control', 'no-store')
103 3
                ->withHeader('content-type', 'application/json; charset=UTF-8');
104
105 3
        $response->getBody()->write(json_encode($responseParams));
106
107 3
        return $response;
108
    }
109
110
    /**
111
     * Add custom fields to your Introspection response here, then set your introspection
112
     * reponse in AuthorizationServer::setIntrospectionResponseType() to pull in your version of
113
     * this class rather than the default.
114
     *
115
     * @return array
116
     */
117 2
    protected function getExtraParams()
118
    {
119 2
        return [];
120
    }
121
}
122