ActionBuilder::getPathTemplate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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\Builders\Controller;
11
12
use FlexPHP\Generator\Domain\Builders\AbstractBuilder;
13
use FlexPHP\Schema\Constants\Action;
14 17
use FlexPHP\Schema\SchemaInterface;
15
use Symfony\Component\HttpFoundation\Request;
16 17
17 17
final class ActionBuilder extends AbstractBuilder
18 17
{
19
    private string $action;
20 17
21
    public function __construct(
22 17
        SchemaInterface $schema,
23 17
        string $action,
24
        string $requestMessage = '',
25
        string $useCase = '',
26 17
        string $responseMessage = ''
27 17
    ) {
28
        $inflector = $this->getInflector();
29
        $action = empty($action)
30 17
            ? 'index'
31 17
            : $inflector->action($action);
32
33 17
        $this->action = $action;
34
35 17
        $data = [];
36
        $data['action'] = $action;
37
        $data['entity'] = $inflector->entity($schema->name());
38 17
        $data['entity_dash'] = $inflector->route($schema->name());
39
        $data['item'] = $inflector->item($schema->name());
40 17
        $data['pkName'] = $inflector->camelProperty($schema->pkName());
41
        $data['pkTypeHint'] = $schema->pkTypeHint();
42
        $data['request_message'] = $requestMessage;
43 17
        $data['use_case'] = $useCase;
44
        $data['response_message'] = $responseMessage;
45
        $data['action_camel'] = $inflector->camelAction($action);
46 17
        $data['action_pascal'] = $inflector->pascalAction($action);
47 15
        $data['route'] = $this->getGuessRoute($inflector->dashAction($action));
48 9
        $data['route_name'] = $inflector->routeName($schema->name(), $action);
49 9
        $data['methods'] = $action === 'index' && $schema->hasAction(Action::FILTER)
50 14
            ? \implode('","', [Request::METHOD_GET, Request::METHOD_POST])
51 6
            : $this->getGuessMethod($action);
52 6
53 13
        parent::__construct($data);
54 5
    }
55 5
56 12
    protected function getPathTemplate(): string
57
    {
58 12
        return \sprintf('%1$s/Symfony/src/Controller', parent::getPathTemplate());
59 12
    }
60
61
    protected function getFileTemplate(): string
62 17
    {
63
        if (\in_array($this->action, ['index', 'create', 'read', 'update', 'delete'])) {
64
            return $this->action . '.php.twig';
65 17
        }
66
67 17
        return 'default.php.twig';
68
    }
69
70 17
    private function getGuessMethod(string $action): string
71 7
    {
72 7
        $methodByAction = [
73 15
            'index' => Request::METHOD_GET,
74 6
            'read' => Request::METHOD_GET,
75 6
            'update' => Request::METHOD_PUT,
76 14
            'delete' => Request::METHOD_DELETE,
77 13
            'create' => Request::METHOD_POST,
78 7
        ];
79 7
80
        if (isset($methodByAction[$action])) {
81
            return $methodByAction[$action];
82 17
        }
83
84
        return Request::METHOD_POST;
85
    }
86
87
    private function getGuessRoute(string $action): string
88
    {
89
        $routeByMethod = [
90
            'index' => '/',
91
            'read' => '/{id}',
92
            'update' => \sprintf('/%1$s/{id}', $action),
93
            'delete' => \sprintf('/%1$s/{id}', $action),
94
        ];
95
96
        if (isset($routeByMethod[$action])) {
97
            return $routeByMethod[$action];
98
        }
99
100
        return '/' . $action;
101
    }
102
}
103