GeneratorTrait::solveControllers()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 9
c 3
b 0
f 0
dl 0
loc 13
rs 9.9666
cc 3
nc 2
nop 0
1
<?php
2
3
namespace SoliDry\Controllers;
4
5
use SoliDry\Blocks\Controllers;
6
use SoliDry\Blocks\Entities;
7
use SoliDry\Blocks\FileManager;
8
use SoliDry\Blocks\FormRequest;
9
use SoliDry\Blocks\Migrations;
10
use SoliDry\Blocks\Routes;
11
use SoliDry\Blocks\Tests;
12
use SoliDry\Exceptions\DirectoryException;
13
use SoliDry\Helpers\Console;
14
use SoliDry\Types\ConsoleInterface;
15
use SoliDry\Types\DefaultInterface;
16
use SoliDry\Types\DirsInterface;
17
use SoliDry\Types\PhpInterface;
18
19
/**
20
 * Trait GeneratorTrait
21
 *
22
 * @package SoliDry\Controllers
23
 */
24
trait GeneratorTrait
25
{
26
    use HistoryTrait;
27
28
    // all generated entities/resources
29
    private $forms;
30
    private $routes;
31
32
    // gen dir found in history
33
    private $genDir;
34
35
    /**
36
     * Standard generation
37
     */
38
    private function generateResources(): void
39
    {
40
        $this->outputEntity();
41
42
        // create controller
43
        $this->solveControllers();
44
45
        // create FormRequest
46
        $this->solveFormRequest();
47
48
        // create entities/models
49
        $this->solveEntities();
50
51
        // create routes
52
        $this->routes = new Routes($this);
53
        $this->routes->create();
54
55
        // create tests
56
        if (empty($this->options[ConsoleInterface::OPTION_TESTS]) === false) {
57
            try {
58
                FileManager::createPath($this->formatFuncTestsPath());
0 ignored issues
show
Bug introduced by
It seems like formatFuncTestsPath() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

58
                FileManager::createPath($this->/** @scrutinizer ignore-call */ formatFuncTestsPath());
Loading history...
59
            } catch (DirectoryException $e) {
60
                $this->error($e->getTraceAsString());
0 ignored issues
show
Bug introduced by
It seems like error() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

60
                $this->/** @scrutinizer ignore-call */ 
61
                       error($e->getTraceAsString());
Loading history...
61
            }
62
63
            $tests = new Tests($this);
64
            $tests->createEntity($this->formatFuncTestsPath(), DefaultInterface::FUNCTIONAL_POSTFIX);
65
        }
66
67
        $this->createMigrations();
68
    }
69
70
    /**
71
     *  Generation with merge option
72
     */
73
    private function mergeResources(): void
74
    {
75
        $this->outputEntity();
76
        $this->solveControllers();
77
78
        $this->solveFormRequest();
79
80
        $this->solveEntities();
81
82
        // create routes
83
        $this->routes = new Routes($this);
84
        $this->routes->create();
85
86
        $this->createMigrations();
87
    }
88
89
    /**
90
     *  Creates Controllers and leaves those generated in case of merge
91
     */
92
    private function solveControllers(): void
93
    {
94
        $controllers = new Controllers($this);
95
        $controllerPath = $this->formatControllersPath();
0 ignored issues
show
Bug introduced by
It seems like formatControllersPath() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

95
        /** @scrutinizer ignore-call */ 
96
        $controllerPath = $this->formatControllersPath();
Loading history...
96
97
        if (empty($this->options[ConsoleInterface::OPTION_REGENERATE]) === false
98
            && file_exists($controllers->getEntityFile($controllerPath,
99
                DefaultInterface::CONTROLLER_POSTFIX)) === true) {
100
101
            $controllers->recreateEntity($controllerPath, DefaultInterface::CONTROLLER_POSTFIX);
102
        } else {
103
            $controllers->createDefault();
104
            $controllers->createEntity($controllerPath, DefaultInterface::CONTROLLER_POSTFIX);
105
        }
106
    }
107
108
    private function solveFormRequest(): void
109
    {
110
        $this->forms = new FormRequest($this);
111
        $formRequestPath = $this->formatRequestsPath();
0 ignored issues
show
Bug introduced by
It seems like formatRequestsPath() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

111
        /** @scrutinizer ignore-call */ 
112
        $formRequestPath = $this->formatRequestsPath();
Loading history...
112
113
        if (empty($this->options[ConsoleInterface::OPTION_REGENERATE]) === false
114
            && file_exists($this->forms->getEntityFile($formRequestPath,
115
                DefaultInterface::FORM_REQUEST_POSTFIX)) === true) {
116
            $this->forms->recreateEntity($formRequestPath, DefaultInterface::FORM_REQUEST_POSTFIX);
117
        } else {
118
            $this->forms->createEntity($formRequestPath, DefaultInterface::FORM_REQUEST_POSTFIX);
119
        }
120
    }
121
122
    /**
123
     *  Decide whether to generate new Entities or mutate existing
124
     */
125
    private function solveEntities(): void
126
    {
127
        // create entities/models
128
        $mappers = new Entities($this);
129
        $mappers->createPivot();
130
        $entitiesPath = $this->formatEntitiesPath();
0 ignored issues
show
Bug introduced by
It seems like formatEntitiesPath() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

130
        /** @scrutinizer ignore-call */ 
131
        $entitiesPath = $this->formatEntitiesPath();
Loading history...
131
132
        if (empty($this->options[ConsoleInterface::OPTION_MERGE]) === false
133
            && file_exists($this->forms->getEntityFile($entitiesPath)) === true) {
134
            $mappers->recreateEntity($entitiesPath);
135
        } else {
136
            $mappers->createEntity($entitiesPath);
137
        }
138
    }
139
140
    private function outputEntity(): void
141
    {
142
        Console::out(
143
            '===============' . PhpInterface::SPACE . $this->objectName
144
            . PhpInterface::SPACE . DirsInterface::ENTITIES_DIR
145
        );
146
    }
147
148
    /**
149
     *  Creates migrations for every entity if there is merge option - adds additional
150
     */
151
    private function createMigrations(): void
152
    {
153
        if (empty($this->options[ConsoleInterface::OPTION_MIGRATIONS]) === false) {
154
            $migrations = new Migrations($this);
155
            $migrations->create();
156
            $migrations->createPivot();
157
        }
158
    }
159
}