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

AuthCodeRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 6
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\AuthorizationCodeGrant\AuthorizationCode;
18
use OAuth2Framework\Component\AuthorizationCodeGrant\AuthorizationCodeId;
19
use OAuth2Framework\Component\AuthorizationCodeGrant\AuthorizationCodeRepository;
20
use OAuth2Framework\Component\Core\Client\ClientId;
21
use OAuth2Framework\Component\Core\DataBag\DataBag;
22
use OAuth2Framework\Component\Core\Event\Event;
23
use OAuth2Framework\Component\Core\Event\EventStore;
24
use OAuth2Framework\Component\Core\ResourceServer\ResourceServerId;
25
use OAuth2Framework\Component\Core\UserAccount\UserAccountId;
26
use SimpleBus\Message\Bus\MessageBus;
27
use Symfony\Component\Cache\Adapter\AdapterInterface;
28
29
final class AuthCodeRepository implements AuthorizationCodeRepository
30
{
31
    /**
32
     * @var int
33
     */
34
    private $minLength;
35
36
    /**
37
     * @var int
38
     */
39
    private $maxLength;
40
41
    /**
42
     * @var int
43
     */
44
    private $lifetime;
45
46
    /**
47
     * @var MessageBus
48
     */
49
    private $eventBus;
50
51
    /**
52
     * @var EventStore
53
     */
54
    private $eventStore;
55
56
    /**
57
     * @var AdapterInterface
58
     */
59
    private $cache;
60
61
    /**
62
     * AuthCodeRepository constructor.
63
     *
64
     * @param int              $minLength
65
     * @param int              $maxLength
66
     * @param int              $lifetime
67
     * @param EventStore       $eventStore
68
     * @param MessageBus       $eventBus
69
     * @param AdapterInterface $cache
70
     */
71
    public function __construct(int $minLength, int $maxLength, int $lifetime, EventStore $eventStore, MessageBus $eventBus, AdapterInterface $cache)
72
    {
73
        $this->minLength = $minLength;
74
        $this->maxLength = $maxLength;
75
        $this->lifetime = $lifetime;
76
        $this->eventStore = $eventStore;
77
        $this->eventBus = $eventBus;
78
        $this->cache = $cache;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function create(ClientId $clientId, UserAccountId $userAccountId, array $queryParameters, string $redirectUri, DataBag $parameters, DataBag $metadatas, ? ResourceServerId $resourceServerId): AuthorizationCode
85
    {
86
        $expiresAt = new \DateTimeImmutable(sprintf('now +%u seconds', $this->lifetime));
87
        $authCodeId = AuthorizationCodeId::create(RandomIdGenerator::generate($this->minLength, $this->maxLength));
88
        $authCode = AuthorizationCode::createEmpty();
89
        $authCode = $authCode->create($authCodeId, $clientId, $userAccountId, $queryParameters, $redirectUri, $expiresAt, $parameters, $metadatas, $resourceServerId);
90
91
        return $authCode;
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function find(AuthorizationCodeId $authCodeId): ? AuthorizationCode
98
    {
99
        $authCode = $this->getFromCache($authCodeId);
100
        if (null === $authCode) {
101
            $events = $this->eventStore->findAllForDomainId($authCodeId);
102
            if (!empty($events)) {
103
                $authCode = $this->getFromEvents($events);
104
                $this->cacheObject($authCode);
105
            }
106
        }
107
108
        return $authCode;
109
    }
110
111
    /**
112
     * @param AuthorizationCode $authCode
113
     */
114
    public function save(AuthorizationCode $authCode)
115
    {
116
        $events = $authCode->recordedMessages();
117
        foreach ($events as $event) {
118
            $this->eventStore->save($event);
119
            $this->eventBus->handle($event);
120
        }
121
        $authCode->eraseMessages();
122
        $this->cacheObject($authCode);
123
    }
124
125
    /**
126
     * @param Event[] $events
127
     *
128
     * @return AuthorizationCode
129
     */
130
    private function getFromEvents(array $events): AuthorizationCode
131
    {
132
        $authCode = AuthorizationCode::createEmpty();
133
        foreach ($events as $event) {
134
            $authCode = $authCode->apply($event);
135
        }
136
137
        return $authCode;
138
    }
139
140
    /**
141
     * @param AuthorizationCodeId $authCodeId
142
     *
143
     * @return AuthorizationCode|null
144
     */
145
    private function getFromCache(AuthorizationCodeId $authCodeId): ? AuthorizationCode
146
    {
147
        $itemKey = sprintf('oauth2-auth_code-%s', $authCodeId->getValue());
148
        $item = $this->cache->getItem($itemKey);
149
        if ($item->isHit()) {
150
            return $item->get();
151
        }
152
153
        return null;
154
    }
155
156
    /**
157
     * @param AuthorizationCode $authCode
158
     */
159
    private function cacheObject(AuthorizationCode $authCode)
160
    {
161
        $itemKey = sprintf('oauth2-auth_code-%s', $authCode->getTokenId()->getValue());
162
        $item = $this->cache->getItem($itemKey);
163
        $item->set($authCode);
164
        $item->tag(['oauth2_server', 'auth_code', $itemKey]);
165
        $this->cache->save($item);
166
    }
167
}
168