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
|
|
|
|