Passed
Pull Request — master (#1446)
by
unknown
35:02
created

DeviceCodeRepository::persistDeviceCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
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
21
class DeviceCodeRepository implements DeviceCodeRepositoryInterface
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function getNewDeviceCode(): DeviceCodeEntityInterface
27
    {
28
        return new DeviceCodeEntity();
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function persistDeviceCode(DeviceCodeEntityInterface $deviceCodeEntity): void
35
    {
36
        // Some logic to persist a new device code to a database
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function persistUser(DeviceCodeEntityInterface $deviceCodeEntity): void
43
    {
44
        $user = $deviceCodeEntity->getUserIdentifier();
0 ignored issues
show
Unused Code introduced by
The assignment to $user is dead and can be removed.
Loading history...
45
        $approved = $deviceCodeEntity->getUserApproved();
0 ignored issues
show
Unused Code introduced by
The assignment to $approved is dead and can be removed.
Loading history...
46
47
        // Some logic to persist user ID and approval status of the given device code entity to a database
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function persistLastPolledAt(DeviceCodeEntityInterface $deviceCodeEntity): void
54
    {
55
        $lastPolledAt = $deviceCodeEntity->getLastPolledAt();
0 ignored issues
show
Unused Code introduced by
The assignment to $lastPolledAt is dead and can be removed.
Loading history...
56
        $approved = $deviceCodeEntity->getUserApproved();
0 ignored issues
show
Unused Code introduced by
The assignment to $approved is dead and can be removed.
Loading history...
57
58
        // Some logic to persist "last polled at" datetime of the given device code entity to a database
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function getDeviceCodeEntityByDeviceCode($deviceCode): ?DeviceCodeEntityInterface
65
    {
66
        $clientEntity = new ClientEntity();
67
        $clientEntity->setIdentifier('myawesomeapp');
68
69
        $deviceCodeEntity = new DeviceCodeEntity();
70
71
        $deviceCodeEntity->setIdentifier($deviceCode);
72
        $deviceCodeEntity->setExpiryDateTime(new DateTimeImmutable('now +1 hour'));
73
        $deviceCodeEntity->setClient($clientEntity);
74
75
        // The user identifier should be set when the user authenticates on the
76
        // OAuth server, along with whether they approved the request
77
        $deviceCodeEntity->setUserApproved(true);
78
        $deviceCodeEntity->setUserIdentifier('1');
79
80
        return $deviceCodeEntity;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function revokeDeviceCode($codeId): void
87
    {
88
        // Some logic to revoke device code
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function isDeviceCodeRevoked($codeId): bool
95
    {
96
        return false;
97
    }
98
}
99