Passed
Push — master ( 2ee0d1...1e8995 )
by Tomasz
03:21
created

UseCase::handle()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 4
nop 1
dl 0
loc 18
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Aggrego\Domain\Api\Domain\Command\CreateBoard;
6
7
use Aggrego\Domain\Api\Application\Model\ProgressiveBoard\Exception\BoardExistException;
8
use Aggrego\Domain\Api\Application\Model\ProgressiveBoard\Repository;
9
use Aggrego\Domain\Api\Application\Profile\BoardConstruction\Exception\BuilderNotFoundException;
10
use Aggrego\Domain\Api\Application\Profile\BoardConstruction\Exception\UnableToBuildBoardException;
11
use Aggrego\Domain\Api\Application\Profile\BoardConstruction\Factory;
12
use Aggrego\Domain\Api\Domain\Command\CreateBoard\Exception\InvalidCommandDataException;
13
use Aggrego\Domain\Model\ProgressiveBoard\Board;
14
15
class UseCase
16
{
17
    /** @var Repository */
18
    private $boardRepository;
19
20
    /** @var Factory */
21
    private $profileBoardFactory;
22
23
    public function __construct(
24
        Repository $boardRepository,
25
        Factory $profileBoardFactory
26
    )
27
    {
28
        $this->boardRepository = $boardRepository;
29
        $this->profileBoardFactory = $profileBoardFactory;
30
    }
31
32
    /**
33
     * @param Command $command
34
     * @throws InvalidCommandDataException
35
     */
36
    public function handle(Command $command): void
37
    {
38
        $key = $command->getKey();
39
        $profile = $command->getProfile();
40
41
        try {
42
            $this->boardRepository->addBoard(
43
                Board::factory(
44
                    $key,
45
                    $this->profileBoardFactory->factory($profile)
46
                )
47
            );
48
        } catch (BuilderNotFoundException $e) {
49
            throw new InvalidCommandDataException($e->getMessage(), $e->getCode(), $e);
50
        } catch (UnableToBuildBoardException $e) {
51
            throw new InvalidCommandDataException($e->getMessage(), $e->getCode(), $e);
52
        } catch (BoardExistException $e) {
53
            throw new InvalidCommandDataException($e->getMessage(), $e->getCode(), $e);
54
        }
55
    }
56
}
57