getDeviceCodeEntityByDeviceCode()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 15
c 1
b 1
f 0
nc 2
nop 1
dl 0
loc 25
rs 9.7666
1
<?php
2
3
/**
4
 * @author    Andrew Millington <[email protected]>
5
 * @copyright Copyright (c) Alex Bilbie
6
 * @license   http://mit-license.org/
7
 *
8
 * @link      https://github.com/thephpleague/oauth2-server
9
 */
10
11
declare(strict_types=1);
12
13
namespace OAuth2ServerExamples\Repositories;
14
15
use DateTimeImmutable;
16
use League\OAuth2\Server\Entities\DeviceCodeEntityInterface;
17
use League\OAuth2\Server\Repositories\DeviceCodeRepositoryInterface;
18
use OAuth2ServerExamples\Entities\ClientEntity;
19
use OAuth2ServerExamples\Entities\DeviceCodeEntity;
20
use OAuth2ServerExamples\Entities\ScopeEntity;
21
22
class DeviceCodeRepository implements DeviceCodeRepositoryInterface
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function getNewDeviceCode(): DeviceCodeEntityInterface
28
    {
29
        return new DeviceCodeEntity();
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function persistDeviceCode(DeviceCodeEntityInterface $deviceCodeEntity): void
36
    {
37
        // Some logic to persist a new device code to a database
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function getDeviceCodeEntityByDeviceCode($deviceCode): ?DeviceCodeEntityInterface
44
    {
45
        $clientEntity = new ClientEntity();
46
        $clientEntity->setIdentifier('myawesomeapp');
47
48
        $deviceCodeEntity = new DeviceCodeEntity();
49
50
        $deviceCodeEntity->setIdentifier($deviceCode);
51
        $deviceCodeEntity->setExpiryDateTime(new DateTimeImmutable('now +1 hour'));
52
        $deviceCodeEntity->setClient($clientEntity);
53
        $deviceCodeEntity->setLastPolledAt(new DateTimeImmutable());
54
55
        $scopes = [];
56
        foreach ($scopes as $scope) {
57
            $scopeEntity = new ScopeEntity();
58
            $scopeEntity->setIdentifier($scope);
59
            $deviceCodeEntity->addScope($scopeEntity);
60
        }
61
62
        // The user identifier should be set when the user authenticates on the
63
        // OAuth server, along with whether they approved the request
64
        $deviceCodeEntity->setUserApproved(true);
65
        $deviceCodeEntity->setUserIdentifier('1');
66
67
        return $deviceCodeEntity;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function revokeDeviceCode($codeId): void
74
    {
75
        // Some logic to revoke device code
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function isDeviceCodeRevoked($codeId): bool
82
    {
83
        return false;
84
    }
85
}
86