|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* Application Handler Class |
|
5
|
|
|
* @package Ticaje_BookingApi |
|
6
|
|
|
* @author Hector Luis Barrientos <[email protected]> |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Ticaje\BookingApi\Application\UseCase\Handler; |
|
10
|
|
|
|
|
11
|
|
|
use Exception; |
|
12
|
|
|
use Ticaje\BookingApi\Application\Signatures\Provider\ServiceProviderSignature; |
|
13
|
|
|
use Ticaje\Contract\Application\Service\ServiceLocatorInterface; |
|
14
|
|
|
use Ticaje\Hexagonal\Application\Signatures\Responder\ResponseInterface; |
|
15
|
|
|
use Ticaje\Hexagonal\Application\Signatures\UseCase\HandlerInterface; |
|
16
|
|
|
use Ticaje\Hexagonal\Application\Signatures\UseCase\UseCaseCommandInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class AggregateBase |
|
20
|
|
|
* @package Ticaje\BookingApi\Application\UseCase\Handler |
|
21
|
|
|
*/ |
|
22
|
|
|
abstract class AggregateBase implements HandlerInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var ResponseInterface */ |
|
25
|
|
|
protected $responder; |
|
26
|
|
|
|
|
27
|
|
|
/** @var ServiceLocatorInterface */ |
|
28
|
|
|
private $serviceLocator; |
|
29
|
|
|
|
|
30
|
|
|
/** @var string */ |
|
31
|
|
|
private $serviceClassMapPath; |
|
32
|
|
|
|
|
33
|
|
|
/** @var string */ |
|
34
|
|
|
private $aggregateName; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* AggregateBase constructor. |
|
38
|
|
|
* |
|
39
|
|
|
* @param ResponseInterface $responder |
|
40
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
|
41
|
|
|
* @param string $serviceClassMapPath |
|
42
|
|
|
* @param string $aggregateName |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct( |
|
45
|
|
|
ResponseInterface $responder, |
|
46
|
|
|
ServiceLocatorInterface $serviceLocator, |
|
47
|
|
|
string $serviceClassMapPath, |
|
48
|
|
|
string $aggregateName |
|
49
|
|
|
) { |
|
50
|
|
|
$this->responder = $responder; |
|
51
|
|
|
$this->serviceLocator = $serviceLocator; |
|
52
|
|
|
$this->serviceClassMapPath = $serviceClassMapPath; |
|
53
|
|
|
$this->aggregateName = $aggregateName; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param UseCaseCommandInterface $command |
|
58
|
|
|
* |
|
59
|
|
|
* @return mixed |
|
60
|
|
|
* @throws Exception |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function launchLogic(UseCaseCommandInterface $command) |
|
63
|
|
|
{ |
|
64
|
|
|
/** @var ServiceProviderSignature $serviceProvider */ |
|
65
|
|
|
$serviceProvider = $this->serviceLocator->get($this->getAggregateClassName($command)); |
|
66
|
|
|
|
|
67
|
|
|
return $serviceProvider->execute($command); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param UseCaseCommandInterface $command |
|
72
|
|
|
* |
|
73
|
|
|
* @return string |
|
74
|
|
|
* @throws Exception |
|
75
|
|
|
*/ |
|
76
|
|
|
private function getAggregateClassName(UseCaseCommandInterface $command): string |
|
77
|
|
|
{ |
|
78
|
|
|
if (!($aggregate = $command->getAggregate())) { |
|
|
|
|
|
|
79
|
|
|
throw new Exception('Aggregate does not exist'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return "{$this->serviceClassMapPath}\\{$aggregate}\\{$this->aggregateName}"; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|