Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
44 / 44 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| ConsoleCommandFactory | |
100.00% |
44 / 44 |
|
100.00% |
2 / 2 |
11 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createForBoundedContext | |
100.00% |
43 / 43 |
|
100.00% |
1 / 1 |
10 | |||
| 1 | <?php |
| 2 | namespace Apie\Console; |
| 3 | |
| 4 | use Apie\Common\ActionDefinitionProvider; |
| 5 | use Apie\Common\ActionDefinitions\CreateResourceActionDefinition; |
| 6 | use Apie\Common\ActionDefinitions\ModifyResourceActionDefinition; |
| 7 | use Apie\Common\ActionDefinitions\RemoveResourceActionDefinition; |
| 8 | use Apie\Common\ActionDefinitions\ReplaceResourceActionDefinition; |
| 9 | use Apie\Common\ActionDefinitions\RunGlobalMethodDefinition; |
| 10 | use Apie\Common\ActionDefinitions\RunResourceMethodDefinition; |
| 11 | use Apie\Common\Actions\CreateObjectAction; |
| 12 | use Apie\Common\Actions\ModifyObjectAction; |
| 13 | use Apie\Common\Actions\RemoveObjectAction; |
| 14 | use Apie\Common\Actions\RunAction; |
| 15 | use Apie\Common\Actions\RunItemMethodAction; |
| 16 | use Apie\Common\ApieFacade; |
| 17 | use Apie\Console\Commands\ApieCreateResourceCommand; |
| 18 | use Apie\Console\Commands\ApieModifyResourceCommand; |
| 19 | use Apie\Console\Commands\ApieRemoveResourceCommand; |
| 20 | use Apie\Console\Commands\ApieRunGlobalMethodCommand; |
| 21 | use Apie\Console\Commands\ApieRunResourceMethodCommand; |
| 22 | use Apie\Console\Lists\ConsoleCommandList; |
| 23 | use Apie\Core\BoundedContext\BoundedContext; |
| 24 | use Apie\Core\Context\ApieContext; |
| 25 | use Apie\Core\ContextConstants; |
| 26 | use Apie\Core\Enums\ConsoleCommand; |
| 27 | |
| 28 | class ConsoleCommandFactory |
| 29 | { |
| 30 | public function __construct( |
| 31 | private readonly ApieFacade $apieFacade, |
| 32 | private readonly ActionDefinitionProvider $actionDefinitionProvider, |
| 33 | private readonly ApieInputHelper $apieInputHelper, |
| 34 | private readonly ConsoleCliStorage $consoleCliStorage |
| 35 | ) { |
| 36 | } |
| 37 | |
| 38 | public function createForBoundedContext(BoundedContext $boundedContext, ApieContext $apieContext): ConsoleCommandList |
| 39 | { |
| 40 | $commands = []; |
| 41 | $apieContext = $apieContext->withContext(ConsoleCommand::class, ConsoleCommand::CONSOLE_COMMAND) |
| 42 | ->withContext(ConsoleCommand::CONSOLE_COMMAND->value, true) |
| 43 | ->withContext(ContextConstants::BOUNDED_CONTEXT_ID, $boundedContext->getId()) |
| 44 | ->registerInstance($this->consoleCliStorage) |
| 45 | ->registerInstance($boundedContext); |
| 46 | foreach ($this->actionDefinitionProvider->provideActionDefinitions($boundedContext, $apieContext) as $actionDefinition) { |
| 47 | $action = null; |
| 48 | $resourceName = null; |
| 49 | $className = null; |
| 50 | $method = null; |
| 51 | // create |
| 52 | if ($actionDefinition instanceof CreateResourceActionDefinition || $actionDefinition instanceof ReplaceResourceActionDefinition) { |
| 53 | $action = new CreateObjectAction($this->apieFacade); |
| 54 | $resourceName = $actionDefinition->getResourceName(); |
| 55 | $className = ApieCreateResourceCommand::class; |
| 56 | } |
| 57 | if ($actionDefinition instanceof RemoveResourceActionDefinition) { |
| 58 | $action = new RemoveObjectAction($this->apieFacade); |
| 59 | $resourceName = $actionDefinition->getResourceName(); |
| 60 | $className = ApieRemoveResourceCommand::class; |
| 61 | } |
| 62 | if ($actionDefinition instanceof ModifyResourceActionDefinition) { |
| 63 | $action = new ModifyObjectAction($this->apieFacade); |
| 64 | $resourceName = $actionDefinition->getResourceName(); |
| 65 | $className = ApieModifyResourceCommand::class; |
| 66 | } |
| 67 | if ($actionDefinition instanceof RunResourceMethodDefinition) { |
| 68 | $action = new RunItemMethodAction($this->apieFacade); |
| 69 | $resourceName = $actionDefinition->getResourceName(); |
| 70 | $className = ApieRunResourceMethodCommand::class; |
| 71 | $method = $actionDefinition->getMethod(); |
| 72 | } |
| 73 | if ($actionDefinition instanceof RunGlobalMethodDefinition) { |
| 74 | $action = new RunAction($this->apieFacade); |
| 75 | $method = $actionDefinition->getMethod(); |
| 76 | $resourceName = $method->getDeclaringClass(); |
| 77 | $className = ApieRunGlobalMethodCommand::class; |
| 78 | } |
| 79 | if ($action !== null && $className !== null) { |
| 80 | $commands[] = new $className( |
| 81 | $action, |
| 82 | $apieContext, |
| 83 | $resourceName, |
| 84 | $this->apieInputHelper, |
| 85 | $this->consoleCliStorage, |
| 86 | $method |
| 87 | ); |
| 88 | } |
| 89 | } |
| 90 | return new ConsoleCommandList($commands); |
| 91 | } |
| 92 | } |