Completed
Push — develop ( e7cba6...164e03 )
by Freddie
13:34
created

CreateTranslateFileUseCase::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 20
rs 9.9
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\Translate\TranslateBuilder;
13
use FlexPHP\Generator\Domain\Messages\Requests\CreateTranslateFileRequest;
14
use FlexPHP\Generator\Domain\Messages\Responses\CreateTranslateFileResponse;
15
use FlexPHP\Generator\Domain\Traits\InflectorTrait;
16
use FlexPHP\Generator\Domain\Writers\PhpWriter;
17
use FlexPHP\Schema\SchemaAttributeInterface;
18
19
final class CreateTranslateFileUseCase
20
{
21
    use InflectorTrait;
22
23
    public function execute(CreateTranslateFileRequest $request): CreateTranslateFileResponse
24
    {
25
        $entity = $this->getSingularize($request->entity);
26
        $properties = \array_reduce(
27
            $request->attributes,
28
            function (array $result, SchemaAttributeInterface $schemaAttribute) {
29
                $result[] = $schemaAttribute->properties();
30
31
                return $result;
32
            },
33
            []
34
        );
35
36
        $translate = new TranslateBuilder($entity, $properties);
37
        $path = \sprintf('%1$s/../../tmp/skeleton/translations', __DIR__);
38
        $filename = sprintf('%1$s.%2$s', $this->getDashCase($entity), $request->language);
0 ignored issues
show
Bug introduced by
$request->language of type array is incompatible with the type string expected by parameter $args of sprintf(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
        $filename = sprintf('%1$s.%2$s', $this->getDashCase($entity), /** @scrutinizer ignore-type */ $request->language);
Loading history...
39
40
        $writer = new PhpWriter($translate->build(), $filename, $path);
41
42
        return new CreateTranslateFileResponse($writer->save());
43
    }
44
}
45