• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

RonasIT / laravel-entity-generator / 15315608533

29 May 2025 03:31AM UTC coverage: 100.0%. Remained the same
15315608533

Pull #155

github

web-flow
Merge 93320b33e into 2dbd60a67
Pull Request #155: refactor: remove useless faker import

896 of 896 relevant lines covered (100.0%)

5.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/Generators/ControllerGenerator.php
1
<?php
2

3
namespace RonasIT\Support\Generators;
4

5
use Illuminate\Contracts\Filesystem\FileNotFoundException;
6
use RonasIT\Support\Exceptions\ClassAlreadyExistsException;
7
use RonasIT\Support\Exceptions\ClassNotExistsException;
8
use RonasIT\Support\Events\SuccessCreateMessage;
9

10
class ControllerGenerator extends EntityGenerator
11
{
12
    public function generate(): void
13
    {
14
        if ($this->classExists('controllers', "{$this->model}Controller")) {
9✔
15
            $this->throwFailureException(
1✔
16
                ClassAlreadyExistsException::class,
1✔
17
                "Cannot create {$this->model}Controller cause {$this->model}Controller already exists.",
1✔
18
                "Remove {$this->model}Controller.",
1✔
19
            );
1✔
20
        }
21

22
        if (!$this->classExists('services', "{$this->model}Service")) {
8✔
23
            $this->throwFailureException(
1✔
24
                ClassNotExistsException::class,
1✔
25
                "Cannot create {$this->model}Controller cause {$this->model}Service does not exists.",
1✔
26
                "Create a {$this->model}Service by himself.",
1✔
27
            );
1✔
28
        }
29

30
        if (!$this->isStubExists('controller')) {
7✔
31
            return;
1✔
32
        }
33

34
        $controllerContent = $this->getControllerContent($this->model);
6✔
35

36
        $this->saveClass('controllers', "{$this->model}Controller", $controllerContent);
6✔
37

38
        $this->createRoutes();
6✔
39

40
        event(new SuccessCreateMessage("Created a new Controller: {$this->model}Controller"));
5✔
41
    }
42

43
    protected function getControllerContent($model): string
44
    {
45
        return $this->getStub('controller', [
6✔
46
            'entity' => $model,
6✔
47
            'requestsFolder' => $model,
6✔
48
            'namespace' => $this->getOrCreateNamespace('controllers'),
6✔
49
            'requestsNamespace' => $this->getOrCreateNamespace('requests'),
6✔
50
            'resourcesNamespace' => $this->getOrCreateNamespace('resources'),
6✔
51
            'servicesNamespace' => $this->getOrCreateNamespace('services'),
6✔
52
        ]);
6✔
53
    }
54

55
    protected function createRoutes(): void
56
    {
57
        $routesPath = base_path($this->paths['routes']);
6✔
58

59
        if (!file_exists($routesPath)) {
6✔
60
            $this->throwFailureException(
1✔
61
                FileNotFoundException::class,
1✔
62
                "Not found file with routes.",
1✔
63
                "Create a routes file on path: '{$routesPath}'.",
1✔
64
            );
1✔
65
        }
66

67
        if ($this->isStubExists('routes') && $this->isStubExists('use_routes')) {
5✔
68
            $this->addUseController($routesPath);
3✔
69
            $this->addRoutes($routesPath);
3✔
70
        }
71
    }
72

73
    protected function addRoutes($routesPath): string
74
    {
75
        $routesContent = $this->getStub('routes', [
3✔
76
            'entity' => $this->model,
3✔
77
            'entities' => $this->getTableName($this->model, '-'),
3✔
78
        ]);
3✔
79

80
        $routes = explode("\n", $routesContent);
3✔
81
        $routes = array_slice($routes, 1, array_key_last($routes) - 1);
3✔
82

83
        foreach ($routes as $route) {
3✔
84
            if (!empty($route)) {
3✔
85
                $route = trim($route);
3✔
86

87
                $createMessage = "Created a new Route: $route";
3✔
88

89
                event(new SuccessCreateMessage($createMessage));
3✔
90
            }
91
        }
92

93
        return file_put_contents($routesPath, "\n\n{$routesContent}", FILE_APPEND);
3✔
94
    }
95

96
    protected function addUseController(string $routesPath): void
97
    {
98
        $routesFileContent = file_get_contents($routesPath);
3✔
99

100
        $stub = $this->getStub('use_routes', [
3✔
101
            'namespace' => $this->getOrCreateNamespace('controllers'),
3✔
102
            'entity' => $this->model
3✔
103
        ]);
3✔
104

105
        $routesFileContent = preg_replace('/\<\?php[^A-Za-z]*/', "<?php\n\n{$stub}", $routesFileContent);
3✔
106

107
        file_put_contents($routesPath, $routesFileContent);
3✔
108
    }
109
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc