Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
44 / 44
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
ConsoleCommandFactory
100.00% covered (success)
100.00%
44 / 44
100.00% covered (success)
100.00%
2 / 2
11
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createForBoundedContext
100.00% covered (success)
100.00%
43 / 43
100.00% covered (success)
100.00%
1 / 1
10
1<?php
2namespace Apie\Console;
3
4use Apie\Common\ActionDefinitionProvider;
5use Apie\Common\ActionDefinitions\CreateResourceActionDefinition;
6use Apie\Common\ActionDefinitions\ModifyResourceActionDefinition;
7use Apie\Common\ActionDefinitions\RemoveResourceActionDefinition;
8use Apie\Common\ActionDefinitions\ReplaceResourceActionDefinition;
9use Apie\Common\ActionDefinitions\RunGlobalMethodDefinition;
10use Apie\Common\ActionDefinitions\RunResourceMethodDefinition;
11use Apie\Common\Actions\CreateObjectAction;
12use Apie\Common\Actions\ModifyObjectAction;
13use Apie\Common\Actions\RemoveObjectAction;
14use Apie\Common\Actions\RunAction;
15use Apie\Common\Actions\RunItemMethodAction;
16use Apie\Common\ApieFacade;
17use Apie\Console\Commands\ApieCreateResourceCommand;
18use Apie\Console\Commands\ApieModifyResourceCommand;
19use Apie\Console\Commands\ApieRemoveResourceCommand;
20use Apie\Console\Commands\ApieRunGlobalMethodCommand;
21use Apie\Console\Commands\ApieRunResourceMethodCommand;
22use Apie\Console\Lists\ConsoleCommandList;
23use Apie\Core\BoundedContext\BoundedContext;
24use Apie\Core\Context\ApieContext;
25use Apie\Core\ContextConstants;
26use Apie\Core\Enums\ConsoleCommand;
27
28class 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}