Completed
Pull Request — master (#925)
by
unknown
01:39
created

IntrospectionResponse   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 102
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 102
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
     * @param bool $bool
22
     */
23 3
    public function setValidity(bool $bool)
24
    {
25 3
        $this->valid = $bool;
26 3
    }
27
28
    /**
29
     * @param ServerRequestInterface $request
30
     */
31 1
    public function setRequest(ServerRequestInterface $request)
32
    {
33 1
        $this->request = $request;
34 1
    }
35
36
    /**
37
     * @return array
38
     */
39 1
    protected function validIntrospectionResponse()
40
    {
41
        $responseParams = [
42 1
            'active' => true,
43
        ];
44
45 1
        return array_merge($this->getExtraParams(), $responseParams);
46
    }
47
48
    /**
49
     * @return array
50
     */
51 2
    protected function invalidIntrospectionResponse()
52
    {
53
        return [
54 2
            'active' => false,
55
        ];
56
    }
57
58
    /**
59
     * Extract the introspection response
60
     *
61
     * @return array
62
     */
63 5
    public function getIntrospectionResponseParams()
64
    {
65 5
        return $this->isValid() ?
66 3
            $this->validIntrospectionResponse() :
67 5
            $this->invalidIntrospectionResponse();
68
    }
69
70
    /**
71
     * @return bool
72
     */
73 5
    protected function isValid()
74
    {
75 5
        return $this->valid === true;
76
    }
77
78
    /**
79
     * @param ResponseInterface $response
80
     *
81
     * @return ResponseInterface
82
     */
83 3
    public function generateHttpResponse(ResponseInterface $response)
84
    {
85 3
        $responseParams = $this->getIntrospectionResponseParams();
86
87
        $response = $response
88 3
                ->withStatus(200)
89 3
                ->withHeader('pragma', 'no-cache')
90 3
                ->withHeader('cache-control', 'no-store')
91 3
                ->withHeader('content-type', 'application/json; charset=UTF-8');
92
93 3
        $response->getBody()->write(json_encode($responseParams));
94
95 3
        return $response;
96
    }
97
98
    /**
99
     * Add custom fields to your Introspection response here, then set your introspection
100
     * reponse in AuthorizationServer::setIntrospectionResponseType() to pull in your version of
101
     * this class rather than the default.
102
     *
103
     * @return array
104
     */
105 2
    protected function getExtraParams()
106
    {
107 2
        return [];
108
    }
109
}
110