Passed
Pull Request — master (#56)
by Rustam
13:25
created

DefaultController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
nc 1
nop 0
dl 0
loc 5
ccs 0
cts 3
cp 0
c 1
b 0
f 0
cc 1
crap 2
rs 10
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
        $generator = $request->getGenerator();
40
        $answers = $request->getAnswers();
41
        $validationResult = $generator->validate();
42
        if ($validationResult->isValid()) {
43
            $generator->saveStickyAttributes();
0 ignored issues
show
Bug introduced by
The method saveStickyAttributes() does not exist on Yiisoft\Yii\Gii\GeneratorInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Yiisoft\Yii\Gii\GeneratorInterface. ( Ignorable by Annotation )

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

43
            $generator->/** @scrutinizer ignore-call */ 
44
                        saveStickyAttributes();
Loading history...
44
            $files = $generator->generate();
45
            $params = [];
46
            $results = [];
47
            $params['hasError'] = !$generator->save($files, (array)$answers, $results);
0 ignored issues
show
Bug introduced by
The method save() does not exist on Yiisoft\Yii\Gii\GeneratorInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Yiisoft\Yii\Gii\GeneratorInterface. ( Ignorable by Annotation )

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

47
            $params['hasError'] = !$generator->/** @scrutinizer ignore-call */ save($files, (array)$answers, $results);
Loading history...
48
            $params['results'] = $results;
49
            return $this->responseFactory->createResponse($params);
50
        }
51
52
        return $this->responseFactory->createResponse(
53
            ['errors' => $validationResult->getErrorMessagesIndexedByAttribute()],
54
            Status::UNPROCESSABLE_ENTITY
55
        );
56
    }
57
58
    public function preview(GeneratorRequest $request, #[Query('file')] ?string $file = null): ResponseInterface
59
    {
60
        $generator = $request->getGenerator();
61
        $validationResult = $generator->validate();
62
        if ($validationResult->isValid()) {
63
            $files = $generator->generate();
64
            if ($file !== null) {
65
                foreach ($files as $generatedFile) {
66
                    if ($generatedFile->getId() === $file) {
67
                        $content = $generatedFile->preview();
68
                        if ($content !== false) {
69
                            return $this->responseFactory->createResponse(['content' => $content]);
70
                        }
71
                        return $this->responseFactory->createResponse(
72
                            ['content' => 'Preview is not available for this file type.']
73
                        );
74
                    }
75
                }
76
                return $this->responseFactory->createResponse(
77
                    ['message' => "Code file not found: $file"],
78
                    Status::UNPROCESSABLE_ENTITY
79
                );
80
            }
81
            return $this->responseFactory->createResponse(['files' => $files, 'operations' => CodeFile::OPERATIONS]);
82
        }
83
84
        return $this->responseFactory->createResponse(
85
            ['errors' => $validationResult->getErrorMessagesIndexedByAttribute()],
86
            Status::UNPROCESSABLE_ENTITY
87
        );
88
    }
89
90
    public function diff(GeneratorRequest $request, #[Query('file')] string $file): ResponseInterface
91
    {
92
        $generator = $request->getGenerator();
93
        $validationResult = $generator->validate();
94
        if ($validationResult->isValid()) {
95
            foreach ($generator->generate() as $generatedFile) {
96
                if ($generatedFile->getId() === $file) {
97
                    return $this->responseFactory->createResponse(['diff' => $generatedFile->diff()]);
98
                }
99
            }
100
            return $this->responseFactory->createResponse(
101
                ['message' => "Code file not found: $file"],
102
                Status::UNPROCESSABLE_ENTITY
103
            );
104
        }
105
106
        return $this->responseFactory->createResponse(
107
            ['errors' => $validationResult->getErrorMessagesIndexedByAttribute()],
108
            Status::UNPROCESSABLE_ENTITY
109
        );
110
    }
111
}
112