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

UseCase::handle()   B

Complexity

Conditions 10
Paths 14

Size

Total Lines 35
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 28
nc 14
nop 1
dl 0
loc 35
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Aggrego\Domain\Api\Domain\Command\TransformBoard;
6
7
use Aggrego\Domain\Api\Application\Model\ProgressiveBoard\Exception\BoardNotFoundException;
8
use Aggrego\Domain\Api\Application\Model\ProgressiveBoard\Repository as ProgressiveBoardRepository;
9
use Aggrego\Domain\Api\Application\Model\Unit\Exception\UnitExistException;
10
use Aggrego\Domain\Api\Application\Model\Unit\Repository as UnitRepository;
11
use Aggrego\Domain\Api\Application\Profile\BoardTransformation\Exception\TransformationNotFoundException as BoardTransformationNotFoundException;
12
use Aggrego\Domain\Api\Application\Profile\BoardTransformation\Exception\UnprocessableBoardException;
13
use Aggrego\Domain\Api\Application\Profile\BoardTransformation\Factory as BoardTransformationFactory;
14
use Aggrego\Domain\Api\Application\Profile\UnitTransformation\Exception\TransformationNotFoundException as UnitTransformationNotFoundException;
15
use Aggrego\Domain\Api\Application\Profile\UnitTransformation\Exception\UnprocessableUnitException;
16
use Aggrego\Domain\Api\Application\Profile\UnitTransformation\Factory as UnitTransformationFactory;
17
use Aggrego\Domain\Api\Domain\Command\TransformBoard\Exception\InvalidCommandDataException;
18
use Aggrego\Domain\Model\ProgressiveBoard\Exception\UnfinishedStepPassedForTransformationException as BoardUnfinishedStepPassedForTransformationException;
19
use Aggrego\Domain\Model\Unit\Exception\UnfinishedStepPassedForTransformationException as UnitUnfinishedStepPassedForTransformationException;
20
use Aggrego\Domain\Model\Unit\Unit;
21
22
class UseCase
23
{
24
    /** @var ProgressiveBoardRepository */
25
    private $progressBoardRepository;
26
27
    /** @var UnitRepository */
28
    private $unitRepository;
29
30
    /** @var BoardTransformationFactory */
31
    private $boardTransformationFactory;
32
33
    /** @var UnitTransformationFactory */
34
    private $unitTransformationFactory;
35
36
    public function __construct(
37
        ProgressiveBoardRepository $progressBoardRepository,
38
        UnitRepository $unitRepository,
39
        BoardTransformationFactory $boardTransformationFactory,
40
        UnitTransformationFactory $unitTransformationFactory
41
    )
42
    {
43
        $this->progressBoardRepository = $progressBoardRepository;
44
        $this->unitRepository = $unitRepository;
45
        $this->boardTransformationFactory = $boardTransformationFactory;
46
        $this->unitTransformationFactory = $unitTransformationFactory;
47
    }
48
49
    /**
50
     * @param Command $command
51
     * @throws InvalidCommandDataException
52
     */
53
    public function handle(Command $command): void
54
    {
55
        try {
56
            $board = $this->progressBoardRepository->getBoardByUuid($command->getBoardUuid());
57
        } catch (BoardNotFoundException $e) {
58
            throw new InvalidCommandDataException($e->getMessage(), $e->getCode(), $e);
59
        }
60
        $profile = $board->getProfile();
61
62
        try {
63
            $transformation = $this->boardTransformationFactory->factory($profile);
64
        } catch (BoardTransformationNotFoundException $e) {
65
            throw new InvalidCommandDataException($e->getMessage(), $e->getCode(), $e);
66
        }
67
68
        if ($transformation->isLastState($board->getStep()->getState())) {
69
            try {
70
                $unitTransformation = $this->unitTransformationFactory->factory($profile);
71
                $this->unitRepository->addUnit(Unit::createFromBoard($board, $unitTransformation));
72
            } catch (UnitTransformationNotFoundException $e) {
73
                throw new InvalidCommandDataException($e->getMessage(), $e->getCode(), $e);
74
            } catch (UnitUnfinishedStepPassedForTransformationException $e) {
75
                throw new InvalidCommandDataException($e->getMessage(), $e->getCode(), $e);
76
            } catch (UnprocessableUnitException $e) {
77
                throw new InvalidCommandDataException($e->getMessage(), $e->getCode(), $e);
78
            } catch (UnitExistException $e) {
79
                throw new InvalidCommandDataException($e->getMessage(), $e->getCode(), $e);
80
            }
81
        } else {
82
            try {
83
                $board->transformStep($transformation);
84
            } catch (BoardUnfinishedStepPassedForTransformationException $e) {
85
                throw new InvalidCommandDataException($e->getMessage(), $e->getCode(), $e);
86
            } catch (UnprocessableBoardException $e) {
87
                throw new InvalidCommandDataException($e->getMessage(), $e->getCode(), $e);
88
            }
89
        }
90
    }
91
}
92