Passed
Push — master ( 902d5f...34a14d )
by Andrew
03:57 queued 02:02
created

DeviceCodeResponse::includeInterval()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
/**
4
 * OAuth 2.0 Bearer Token Response.
5
 *
6
 * @author      Alex Bilbie <[email protected]>
7
 * @copyright   Copyright (c) Alex Bilbie
8
 * @license     http://mit-license.org/
9
 *
10
 * @link        https://github.com/thephpleague/oauth2-server
11
 */
12
13
declare(strict_types=1);
14
15
namespace League\OAuth2\Server\ResponseTypes;
16
17
use League\OAuth2\Server\Entities\DeviceCodeEntityInterface;
18
use LogicException;
19
use Psr\Http\Message\ResponseInterface;
20
21
use function json_encode;
22
use function time;
23
24
class DeviceCodeResponse extends AbstractResponseType
25
{
26
    protected DeviceCodeEntityInterface $deviceCodeEntity;
27
    private bool $includeVerificationUriComplete = false;
28
    private bool $includeInterval = false;
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 6
    public function generateHttpResponse(ResponseInterface $response): ResponseInterface
34
    {
35 6
        $expireDateTime = $this->deviceCodeEntity->getExpiryDateTime()->getTimestamp();
36
37 6
        $responseParams = [
38 6
            'device_code'      => $this->deviceCodeEntity->getIdentifier(),
39 6
            'user_code'        => $this->deviceCodeEntity->getUserCode(),
40 6
            'verification_uri' => $this->deviceCodeEntity->getVerificationUri(),
41 6
            'expires_in'       => $expireDateTime - time(),
42 6
        ];
43
44 6
        if ($this->includeVerificationUriComplete === true) {
45 1
            $responseParams['verification_uri_complete'] = $this->deviceCodeEntity->getVerificationUriComplete();
46
        }
47
48 6
        if ($this->includeInterval === true) {
49 2
            $responseParams['interval'] = $this->deviceCodeEntity->getInterval();
50
        }
51
52 6
        $responseParams = json_encode($responseParams);
53
54 6
        if ($responseParams === false) {
55
            throw new LogicException('Error encountered JSON encoding response parameters');
56
        }
57
58 6
        $response = $response
59 6
            ->withStatus(200)
60 6
            ->withHeader('pragma', 'no-cache')
61 6
            ->withHeader('cache-control', 'no-store')
62 6
            ->withHeader('content-type', 'application/json; charset=UTF-8');
63
64 6
        $response->getBody()->write($responseParams);
65
66 6
        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...
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 6
    public function setDeviceCodeEntity(DeviceCodeEntityInterface $deviceCodeEntity): void
73
    {
74 6
        $this->deviceCodeEntity = $deviceCodeEntity;
75
    }
76
77 1
    public function includeVerificationUriComplete(): void
78
    {
79 1
        $this->includeVerificationUriComplete = true;
80
    }
81
82 2
    public function includeInterval(): void
83
    {
84 2
        $this->includeInterval = true;
85
    }
86
87
    /**
88
     * Add custom fields to your Bearer Token response here, then override
89
     * AuthorizationServer::getResponseType() to pull in your version of
90
     * this class rather than the default.
91
     *
92
     * @return array<array-key,mixed>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key,mixed> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key,mixed>.
Loading history...
93
     */
94
    protected function getExtraParams(DeviceCodeEntityInterface $deviceCode): array
0 ignored issues
show
Unused Code introduced by
The parameter $deviceCode 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

94
    protected function getExtraParams(/** @scrutinizer ignore-unused */ DeviceCodeEntityInterface $deviceCode): 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...
95
    {
96
        return [];
97
    }
98
}
99