Passed
Pull Request — master (#56)
by Rustam
02:36
created

DefaultController::generate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 14
c 0
b 0
f 0
dl 0
loc 19
ccs 0
cts 14
cp 0
rs 9.7998
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Gii\Controller;
6
7
use Psr\Http\Message\ResponseInterface;
8
use Yiisoft\DataResponse\DataResponseFactoryInterface;
9
use Yiisoft\Http\Status;
10
use Yiisoft\RequestModel\Attribute\Query;
11
use Yiisoft\Yii\Gii\CodeFile;
12
use Yiisoft\Yii\Gii\Generator\AbstractGenerator;
13
use Yiisoft\Yii\Gii\Request\GeneratorRequest;
14
15
final class DefaultController
16
{
17
    public function __construct(private DataResponseFactoryInterface $responseFactory)
18
    {
19
    }
20
21
    public function get(GeneratorRequest $request): ResponseInterface
22
    {
23
        /** @var AbstractGenerator $generator */
24
        $generator = $request->getGenerator();
25
        $params = [
26
            'name' => $generator->getName(),
27
            'description' => $generator->getDescription(),
28
            'template' => $generator->getTemplate(),
29
            'templatePath' => $generator->getTemplatePath(),
30
            'templates' => $generator->getTemplates(),
31
            'directory' => $generator->getDirectory(),
32
        ];
33
34
        return $this->responseFactory->createResponse($params);
35
    }
36
37
    public function generate(GeneratorRequest $request): ResponseInterface
38
    {
39
        /** @var AbstractGenerator $generator */
40
        $generator = $request->getGenerator();
41
        $answers = $request->getAnswers();
42
        $validationResult = $generator->validate();
43
        if ($validationResult->isValid()) {
44
            $generator->saveStickyAttributes();
45
            $files = $generator->generate();
46
            $params = [];
47
            $results = [];
48
            $params['hasError'] = !$generator->save($files, (array)$answers, $results);
49
            $params['results'] = $results;
50
            return $this->responseFactory->createResponse($params);
51
        }
52
53
        return $this->responseFactory->createResponse(
54
            ['errors' => $validationResult->getErrorMessagesIndexedByAttribute()],
55
            Status::UNPROCESSABLE_ENTITY
56
        );
57
    }
58
59
    public function preview(GeneratorRequest $request, #[Query('file')] ?string $file = null): ResponseInterface
60
    {
61
        /** @var AbstractGenerator $generator */
62
        $generator = $request->getGenerator();
63
        $validationResult = $generator->validate();
64
        if ($validationResult->isValid()) {
65
            $files = $generator->generate();
66
            if ($file !== null) {
67
                foreach ($files as $generatedFile) {
68
                    if ($generatedFile->getId() === $file) {
69
                        $content = $generatedFile->preview();
70
                        if ($content !== false) {
71
                            return $this->responseFactory->createResponse(['content' => $content]);
72
                        }
73
                        return $this->responseFactory->createResponse(
74
                            ['content' => 'Preview is not available for this file type.']
75
                        );
76
                    }
77
                }
78
                return $this->responseFactory->createResponse(
79
                    ['message' => "Code file not found: $file"],
80
                    Status::UNPROCESSABLE_ENTITY
81
                );
82
            }
83
            return $this->responseFactory->createResponse(['files' => $files, 'operations' => CodeFile::OPERATIONS]);
84
        }
85
86
        return $this->responseFactory->createResponse(
87
            ['errors' => $validationResult->getErrorMessagesIndexedByAttribute()],
88
            Status::UNPROCESSABLE_ENTITY
89
        );
90
    }
91
92
    public function diff(GeneratorRequest $request, #[Query('file')] string $file): ResponseInterface
93
    {
94
        /** @var AbstractGenerator $generator */
95
        $generator = $request->getGenerator();
96
        $validationResult = $generator->validate();
97
        if ($validationResult->isValid()) {
98
            foreach ($generator->generate() as $generatedFile) {
99
                if ($generatedFile->getId() === $file) {
100
                    return $this->responseFactory->createResponse(['diff' => $generatedFile->diff()]);
101
                }
102
            }
103
            return $this->responseFactory->createResponse(
104
                ['message' => "Code file not found: $file"],
105
                Status::UNPROCESSABLE_ENTITY
106
            );
107
        }
108
109
        return $this->responseFactory->createResponse(
110
            ['errors' => $validationResult->getErrorMessagesIndexedByAttribute()],
111
            Status::UNPROCESSABLE_ENTITY
112
        );
113
    }
114
}
115