Failed Conditions
Push — master ( c6baf0...a3629e )
by Florent
16:19
created

MarkAccessTokenAsRevokedHandler::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 2
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\Component\Core\AccessToken\Command;
15
16
use OAuth2Framework\Component\Core\AccessToken\AccessTokenRepository;
17
use OAuth2Framework\Component\Core\AccessToken\Event\AccessTokenRevokedEvent;
18
use SimpleBus\SymfonyBridge\Bus\EventBus;
19
20
class MarkAccessTokenAsRevokedHandler
21
{
22
    private $accessTokenRepository;
23
    private $eventBus;
24
25
    public function __construct(AccessTokenRepository $accessTokenRepository, EventBus $eventBus)
26
    {
27
        $this->accessTokenRepository = $accessTokenRepository;
28
        $this->eventBus = $eventBus;
29
    }
30
31
    public function handle(CreateAccessToken $command): void
32
    {
33
        $accessToken = $this->accessTokenRepository->find($command->getAccessTokenId());
34
        if (!$accessToken) {
35
            throw new \InvalidArgumentException(\sprintf('The access token with ID "%s" does not exist.', $command->getAccessTokenId()->getValue()));
36
        }
37
38
        $accessToken->markAsRevoked();
39
        $this->accessTokenRepository->save($accessToken);
40
        $event = new AccessTokenRevokedEvent($command->getAccessTokenId());
41
        $this->eventBus->handle($event);
42
    }
43
}
44