Passed
Push — master ( 574b00...b5f26b )
by Arthur
03:04
created

GeneratorTrait::getLastFiles()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 28
c 0
b 0
f 0
rs 9.0777
cc 6
nc 5
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 $mappers;
31
    private $routes;
32
    private $migrations;
33
    private $controllers;
34
    private $tests;
35
36
    // gen dir found in history
37
    private $genDir;
38
39
    /**
40
     * Standard generation
41
     */
42
    private function generateResources(): void
43
    {
44
        $this->outputEntity();
45
46
        // create controller
47
        $this->solveControllers();
48
49
        // create FormRequest
50
        $this->solveFormRequest();
51
52
        // create entities/models
53
        $this->solveEntities();
54
55
        // create routes
56
        $this->routes = new Routes($this);
57
        $this->routes->create();
58
59
        // create tests
60
        if (empty($this->options[ConsoleInterface::OPTION_TESTS]) === false) {
61
            try {
62
                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

62
                FileManager::createPath($this->/** @scrutinizer ignore-call */ formatFuncTestsPath());
Loading history...
63
            } catch (DirectoryException $e) {
64
                $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

64
                $this->/** @scrutinizer ignore-call */ 
65
                       error($e->getTraceAsString());
Loading history...
65
            }
66
67
            $this->tests = new Tests($this);
68
            $this->tests->createEntity($this->formatFuncTestsPath(), DefaultInterface::FUNCTIONAL_POSTFIX);
69
        }
70
71
        $this->createMigrations();
72
    }
73
74
    /**
75
     *  Generation with merge option
76
     */
77
    private function mergeResources(): void
78
    {
79
        $this->outputEntity();
80
        $this->solveControllers();
81
82
        $this->solveFormRequest();
83
84
        $this->solveEntities();
85
86
        // create routes
87
        $this->routes = new Routes($this);
88
        $this->routes->create();
89
90
        $this->createMigrations();
91
    }
92
93
    /**
94
     *  Creates Controllers and leaves those generated in case of merge
95
     */
96
    private function solveControllers(): void
97
    {
98
        $this->controllers = new Controllers($this);
99
        $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

99
        /** @scrutinizer ignore-call */ 
100
        $controllerPath = $this->formatControllersPath();
Loading history...
100
101
        if (empty($this->options[ConsoleInterface::OPTION_REGENERATE]) === false
102
            && file_exists($this->controllers->getEntityFile($controllerPath,
103
                DefaultInterface::CONTROLLER_POSTFIX)) === true) {
104
105
            $this->controllers->recreateEntity($controllerPath, DefaultInterface::CONTROLLER_POSTFIX);
106
        } else {
107
            $this->controllers->createDefault();
108
            $this->controllers->createEntity($controllerPath, DefaultInterface::CONTROLLER_POSTFIX);
109
        }
110
    }
111
112
    private function solveFormRequest(): void
113
    {
114
        $this->forms = new FormRequest($this);
115
        $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

115
        /** @scrutinizer ignore-call */ 
116
        $formRequestPath = $this->formatRequestsPath();
Loading history...
116
117
        if (empty($this->options[ConsoleInterface::OPTION_REGENERATE]) === false
118
            && file_exists($this->forms->getEntityFile($formRequestPath,
119
                DefaultInterface::FORM_REQUEST_POSTFIX)) === true) {
120
            $this->forms->recreateEntity($formRequestPath, DefaultInterface::FORM_REQUEST_POSTFIX);
121
        } else {
122
            $this->forms->createEntity($formRequestPath, DefaultInterface::FORM_REQUEST_POSTFIX);
123
        }
124
    }
125
126
    /**
127
     *  Decide whether to generate new Entities or mutate existing
128
     */
129
    private function solveEntities(): void
130
    {
131
        // create entities/models
132
        $this->mappers = new Entities($this);
133
        $this->mappers->createPivot();
134
        $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

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