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

CreateAccessTokenHandler   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 5
dl 0
loc 40
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A handle() 0 28 2
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\AccessToken;
17
use OAuth2Framework\Component\Core\AccessToken\AccessTokenRepository;
18
use OAuth2Framework\Component\Core\AccessToken\Event\AccessTokenCreatedEvent;
19
use SimpleBus\SymfonyBridge\Bus\EventBus;
20
21
class CreateAccessTokenHandler
22
{
23
    private $accessTokenRepository;
24
    private $eventBus;
25
26
    public function __construct(AccessTokenRepository $accessTokenRepository, EventBus $eventBus)
27
    {
28
        $this->accessTokenRepository = $accessTokenRepository;
29
        $this->eventBus = $eventBus;
30
    }
31
32
    public function handle(CreateAccessToken $command): void
33
    {
34
        $accessToken = $this->accessTokenRepository->find($command->getAccessTokenId());
35
        if ($accessToken) {
36
            throw new \InvalidArgumentException(\sprintf('The access token with ID "%s" already exist.', $command->getAccessTokenId()->getValue()));
37
        }
38
39
        $accessToken = new AccessToken(
40
            $command->getAccessTokenId(),
41
            $command->getResourceOwnerId(),
42
            $command->getClientId(),
43
            $command->getParameter(),
44
            $command->getMetadata(),
45
            $command->getExpiresAt(),
46
            $command->getResourceServerId()
47
        );
48
        $this->accessTokenRepository->save($accessToken);
49
        $event = new AccessTokenCreatedEvent(
50
            $command->getAccessTokenId(),
51
            $command->getResourceOwnerId(),
52
            $command->getClientId(),
53
            $command->getParameter(),
54
            $command->getMetadata(),
55
            $command->getExpiresAt(),
56
            $command->getResourceServerId()
57
        );
58
        $this->eventBus->handle($event);
59
    }
60
}
61