Failed Conditions
Push — ng ( e90992...13fb6e )
by Florent
17:32
created

InitialAccessTokenRepository::getFromEvents()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace OAuth2Framework\Bundle\Tests\TestBundle\Entity;
15
16
use OAuth2Framework\Bundle\Service\RandomIdGenerator;
17
use OAuth2Framework\Component\ClientRegistrationEndpoint\InitialAccessToken;
18
use OAuth2Framework\Component\ClientRegistrationEndpoint\InitialAccessTokenId;
19
use OAuth2Framework\Component\Core\Event\Event;
20
use OAuth2Framework\Component\Core\Event\EventStore;
21
use OAuth2Framework\Component\Core\UserAccount\UserAccountId;
22
use SimpleBus\Message\Recorder\RecordsMessages;
23
use Symfony\Component\Cache\Adapter\AdapterInterface;
24
25
final class InitialAccessTokenRepository implements \OAuth2Framework\Component\ClientRegistrationEndpoint\InitialAccessTokenRepository
26
{
27
    /**
28
     * @var int
29
     */
30
    private $minLength;
31
32
    /**
33
     * @var int
34
     */
35
    private $maxLength;
36
37
    /**
38
     * @var EventStore
39
     */
40
    private $eventStore;
41
42
    /**
43
     * @var RecordsMessages
44
     */
45
    private $eventRecorder;
46
47
    /**
48
     * @var AdapterInterface
49
     */
50
    private $cache;
51
52
    /**
53
     * InitialAccessTokenRepository constructor.
54
     *
55
     * @param int              $minLength
56
     * @param int              $maxLength
57
     * @param EventStore       $eventStore
58
     * @param RecordsMessages  $eventRecorder
59
     * @param AdapterInterface $cache
60
     */
61
    public function __construct(int $minLength, int $maxLength, EventStore $eventStore, RecordsMessages $eventRecorder, AdapterInterface $cache)
62
    {
63
        $this->minLength = $minLength;
64
        $this->maxLength = $maxLength;
65
        $this->eventStore = $eventStore;
66
        $this->eventRecorder = $eventRecorder;
67
        $this->cache = $cache;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function create(? UserAccountId $userAccountId, ? \DateTimeImmutable $expiresAt): InitialAccessToken
74
    {
75
        $initialAccessTokeId = InitialAccessTokenId::create(RandomIdGenerator::generate($this->minLength, $this->maxLength));
76
        $initialAccessToken = InitialAccessToken::createEmpty();
77
        $initialAccessToken = $initialAccessToken->create($initialAccessTokeId, $userAccountId, $expiresAt);
78
79
        return $initialAccessToken;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function find(InitialAccessTokenId $initialAccessTokenId): ? InitialAccessToken
86
    {
87
        $initialAccessToken = $this->getFromCache($initialAccessTokenId);
88
        if (null === $initialAccessToken) {
89
            $events = $this->eventStore->findAllForDomainId($initialAccessTokenId);
90
            if (!empty($events)) {
91
                $initialAccessToken = $this->getFromEvents($events);
92
                $this->cacheObject($initialAccessToken);
93
            }
94
        }
95
96
        return $initialAccessToken;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function save(InitialAccessToken $initialAccessToken)
103
    {
104
        $events = $initialAccessToken->recordedMessages();
105
        foreach ($events as $event) {
106
            $this->eventStore->save($event);
107
            $this->eventRecorder->record($event);
108
        }
109
        $initialAccessToken->eraseMessages();
110
        $this->cacheObject($initialAccessToken);
111
    }
112
113
    /**
114
     * @param Event[] $events
115
     *
116
     * @return InitialAccessToken
117
     */
118
    private function getFromEvents(array $events): InitialAccessToken
119
    {
120
        $initialAccessToken = InitialAccessToken::createEmpty();
121
        foreach ($events as $event) {
122
            $initialAccessToken = $initialAccessToken->apply($event);
123
        }
124
125
        return $initialAccessToken;
126
    }
127
128
    /**
129
     * @param InitialAccessTokenId $initialAccessTokenId
130
     *
131
     * @return InitialAccessToken|null
132
     */
133
    private function getFromCache(InitialAccessTokenId $initialAccessTokenId): ? InitialAccessToken
134
    {
135
        $itemKey = sprintf('oauth2-initial_access_token-%s', $initialAccessTokenId->getValue());
136
        $item = $this->cache->getItem($itemKey);
137
        if ($item->isHit()) {
138
            return $item->get();
139
        }
140
141
        return null;
142
    }
143
144
    /**
145
     * @param InitialAccessToken $initialAccessToken
146
     */
147
    private function cacheObject(InitialAccessToken $initialAccessToken)
148
    {
149
        $itemKey = sprintf('oauth2-initial_access_token-%s', $initialAccessToken->getUserAccountId()->getValue());
150
        $item = $this->cache->getItem($itemKey);
151
        $item->set($initialAccessToken);
152
        $item->tag(['oauth2_server', 'initial_access_token', $itemKey]);
153
        $this->cache->save($item);
154
    }
155
}
156