|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of FlexPHP. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Freddie Gar <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace FlexPHP\Generator\Domain\UseCases; |
|
11
|
|
|
|
|
12
|
|
|
use FlexPHP\Generator\Domain\Builders\Gateway\MySQLGatewayBuilder; |
|
13
|
|
|
use FlexPHP\Generator\Domain\Builders\Inflector; |
|
14
|
|
|
use FlexPHP\Generator\Domain\Messages\Requests\CreateConcreteGatewayFileRequest; |
|
15
|
|
|
use FlexPHP\Generator\Domain\Messages\Responses\CreateConcreteGatewayFileResponse; |
|
16
|
|
|
use FlexPHP\Generator\Domain\Writers\PhpWriter; |
|
17
|
|
|
use InvalidArgumentException; |
|
18
|
|
|
|
|
19
|
|
|
final class CreateConcreteGatewayFileUseCase |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var array<string, string> |
|
23
|
|
|
*/ |
|
24
|
|
|
private array $concretes = [ |
|
25
|
|
|
'MySQL' => 'MySQLGatewayBuilder', |
|
26
|
|
|
]; |
|
27
|
|
|
|
|
28
|
|
|
public function execute(CreateConcreteGatewayFileRequest $request): CreateConcreteGatewayFileResponse |
|
29
|
|
|
{ |
|
30
|
|
|
$inflector = new Inflector(); |
|
31
|
|
|
$entity = $inflector->entity($request->schema->name()); |
|
32
|
|
|
$concrete = $request->concrete; |
|
33
|
|
|
$concretesAvailable = \array_keys($this->concretes); |
|
34
|
|
|
|
|
35
|
|
|
if (!\in_array($concrete, $concretesAvailable)) { |
|
36
|
|
|
throw new InvalidArgumentException($concrete . ' is not valid, use: ' . \implode(',', $concretesAvailable)); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$gateway = new MySQLGatewayBuilder($request->schema, $request->actions); |
|
40
|
|
|
$filename = $concrete . $entity . 'Gateway'; |
|
41
|
|
|
$path = \sprintf('%1$s/../../tmp/skeleton/domain/%2$s/Gateway', __DIR__, $entity); |
|
42
|
|
|
$writer = new PhpWriter($gateway->build(), $filename, $path); |
|
43
|
|
|
|
|
44
|
|
|
return new CreateConcreteGatewayFileResponse($writer->save()); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|