Total Complexity | 51 |
Total Lines | 356 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like BaseCommand often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BaseCommand, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class BaseCommand extends Command |
||
20 | { |
||
21 | use GeneratorTrait; |
||
22 | |||
23 | // dirs |
||
24 | public $rootDir = ''; |
||
25 | public $appDir = ''; |
||
26 | public $modulesDir = ''; |
||
27 | public $httpDir = ''; |
||
28 | public $controllersDir = ''; |
||
29 | public $formRequestDir = ''; |
||
30 | public $entitiesDir = ''; |
||
31 | public $migrationsDir = ''; |
||
32 | |||
33 | public $version; |
||
34 | public $objectName = ''; |
||
35 | public $defaultController = 'Default'; |
||
36 | public $uriNamedParams; |
||
37 | public $force; |
||
38 | public $customTypes = [ |
||
39 | CustomsInterface::CUSTOM_TYPES_ID, |
||
40 | CustomsInterface::CUSTOM_TYPES_TYPE, |
||
41 | CustomsInterface::CUSTOM_TYPES_RELATIONSHIPS, |
||
42 | CustomsInterface::CUSTOM_TYPE_REDIS, |
||
43 | ]; |
||
44 | |||
45 | public $types = []; |
||
46 | public $currentTypes = []; |
||
47 | public $historyTypes = []; |
||
48 | public $mergedTypes = []; |
||
49 | public $diffTypes = []; |
||
50 | public $objectProps = []; |
||
51 | public $generatedFiles = []; |
||
52 | public $relationships = []; |
||
53 | private $files = []; |
||
54 | |||
55 | public $excludedSubtypes = [ |
||
56 | CustomsInterface::CUSTOM_TYPES_ATTRIBUTES, |
||
57 | CustomsInterface::CUSTOM_TYPES_RELATIONSHIPS, |
||
58 | CustomsInterface::CUSTOM_TYPES_QUERY_PARAMS, |
||
59 | CustomsInterface::CUSTOM_TYPES_FILTER, |
||
60 | CustomsInterface::CUSTOM_TYPES_TREES, |
||
61 | ]; |
||
62 | |||
63 | public $options = []; |
||
64 | public $isMerge = false; |
||
65 | /** increment created routes to create file first and then append content */ |
||
66 | public $routesCreated = 0; |
||
67 | |||
68 | public $data = []; |
||
69 | |||
70 | public $isRollback = false; |
||
71 | |||
72 | /** |
||
73 | * Generates api components for OAS |
||
74 | * |
||
75 | * @param mixed $files path to openapi file or an array of files in case of rollback |
||
76 | * @throws \Symfony\Component\Yaml\Exception\ParseException |
||
77 | * @throws SchemaException |
||
78 | */ |
||
79 | public function actionIndex($files) |
||
80 | { |
||
81 | if ($this->isRollback) { |
||
82 | $this->files = $files; |
||
83 | $this->data = Yaml::parse(file_get_contents($this->formatGenPathByDir() . $files[0])); |
||
84 | } else { |
||
85 | $this->files[] = $files; |
||
86 | $this->data = Yaml::parse(file_get_contents($files)); |
||
87 | } |
||
88 | |||
89 | $this->validate(); |
||
90 | $this->generateOpenApi(); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Validates OAS + Custom fields |
||
95 | * @throws SchemaException |
||
96 | */ |
||
97 | private function validate() |
||
113 | } |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Main input point of generator |
||
118 | * |
||
119 | * @throws \Symfony\Component\Yaml\Exception\ParseException |
||
120 | */ |
||
121 | private function generateOpenApi() |
||
122 | { |
||
123 | $this->appDir = DirsInterface::APPLICATION_DIR; |
||
124 | $this->controllersDir = DirsInterface::CONTROLLERS_DIR; |
||
125 | $this->entitiesDir = DirsInterface::ENTITIES_DIR; |
||
126 | $this->modulesDir = DirsInterface::MODULES_DIR; |
||
127 | $this->httpDir = DirsInterface::HTTP_DIR; |
||
128 | $this->formRequestDir = DirsInterface::FORM_REQUEST_DIR; |
||
129 | $this->migrationsDir = DirsInterface::MIGRATIONS_DIR; |
||
130 | |||
131 | foreach ($this->data[ApiInterface::API_SERVERS] as $server) { |
||
132 | $vars = $server[ApiInterface::API_VARS]; |
||
133 | $this->version = $vars[ApiInterface::API_BASE_PATH][ApiInterface::API_DEFAULT]; |
||
134 | |||
135 | if (env('APP_ENV') === 'dev') { // for test env based on .env |
||
136 | $this->options = [ |
||
137 | ConsoleInterface::OPTION_REGENERATE => 1, |
||
138 | ConsoleInterface::OPTION_MIGRATIONS => 1, |
||
139 | ConsoleInterface::OPTION_TESTS => 1, |
||
140 | ]; |
||
141 | } else { |
||
142 | $this->options = $this->options(); |
||
143 | } |
||
144 | |||
145 | $this->setIncludedTypes(); |
||
146 | $this->runGenerator(); |
||
147 | |||
148 | try { |
||
149 | if ($this->isRollback === false) { |
||
150 | $this->setGenHistory(); |
||
151 | } |
||
152 | } catch (DirectoryException $ex) { |
||
153 | $this->error($ex->getTraceAsString()); |
||
154 | } |
||
155 | } |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Main generator method - the sequence of methods execution is crucial |
||
160 | * |
||
161 | * @throws \Symfony\Component\Yaml\Exception\ParseException |
||
162 | */ |
||
163 | private function runGenerator() |
||
164 | { |
||
165 | if (empty($this->options[ConsoleInterface::OPTION_MERGE]) === false) { // create new or regenerate |
||
166 | $this->setMergedTypes(); |
||
167 | $this->isMerge = true; |
||
168 | } |
||
169 | |||
170 | $this->generateModule(); |
||
171 | $this->generateConfig(); |
||
172 | |||
173 | $this->generate(); |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Generates new code or regenerate older with new content |
||
178 | */ |
||
179 | private function generate() |
||
180 | { |
||
181 | foreach ($this->types as $objName => $objData) { |
||
182 | if (in_array($objName, $this->customTypes) === false) { // if this is not a custom type generate resources |
||
183 | $excluded = false; |
||
184 | foreach ($this->excludedSubtypes as $type) { |
||
185 | if (strpos($objName, $type) !== false) { |
||
186 | $excluded = true; |
||
187 | } |
||
188 | } |
||
189 | // if the type is among excluded - continue |
||
190 | if ($excluded === true) { |
||
191 | continue; |
||
192 | } |
||
193 | $this->processObjectData($objName, $objData); |
||
194 | } |
||
195 | } |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * @param string $objName |
||
200 | * @param array $objData |
||
201 | */ |
||
202 | private function processObjectData(string $objName, array $objData) |
||
203 | { |
||
204 | foreach ($objData as $k => $v) { |
||
205 | if ($k === ApiInterface::RAML_PROPS) { // process props |
||
206 | $this->setObjectName($objName); |
||
207 | $this->setObjectProps($v); |
||
208 | if (true === $this->isMerge) { |
||
209 | $this->mergeResources(); |
||
210 | } else { |
||
211 | $this->generateResources(); |
||
212 | } |
||
213 | } |
||
214 | } |
||
215 | } |
||
216 | |||
217 | private function generateModule() |
||
218 | { |
||
219 | $module = new Module($this); |
||
220 | $module->create(); |
||
221 | } |
||
222 | |||
223 | private function generateConfig() |
||
224 | { |
||
225 | $module = new Config($this); |
||
226 | $module->create(); |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * @throws \SoliDry\Exceptions\DirectoryException |
||
231 | */ |
||
232 | public function createDirs() |
||
233 | { |
||
234 | // create modules dir |
||
235 | FileManager::createPath(FileManager::getModulePath($this)); |
||
236 | // create config dir |
||
237 | FileManager::createPath($this->formatConfigPath()); |
||
238 | // create Controllers dir |
||
239 | FileManager::createPath($this->formatControllersPath()); |
||
240 | // create forms dir |
||
241 | FileManager::createPath($this->formatRequestsPath()); |
||
242 | // create mapper dir |
||
243 | FileManager::createPath($this->formatEntitiesPath()); |
||
244 | // create migrations dir |
||
245 | FileManager::createPath($this->formatMigrationsPath()); |
||
246 | } |
||
247 | |||
248 | public function formatControllersPath() : string |
||
249 | { |
||
250 | /** @var Command $this */ |
||
251 | return FileManager::getModulePath($this, true) . $this->controllersDir; |
||
252 | } |
||
253 | |||
254 | public function formatRequestsPath() : string |
||
255 | { |
||
256 | /** @var Command $this */ |
||
257 | return FileManager::getModulePath($this, true) . $this->formRequestDir; |
||
258 | } |
||
259 | |||
260 | public function formatEntitiesPath() : string |
||
261 | { |
||
262 | /** @var Command $this */ |
||
263 | return FileManager::getModulePath($this) . $this->entitiesDir; |
||
264 | } |
||
265 | |||
266 | public function formatMigrationsPath() : string |
||
267 | { |
||
268 | /** @var Command $this */ |
||
269 | return FileManager::getModulePath($this) . DirsInterface::DATABASE_DIR . PhpInterface::SLASH |
||
270 | . $this->migrationsDir . PhpInterface::SLASH; |
||
271 | } |
||
272 | |||
273 | public function formatConfigPath() |
||
274 | { |
||
275 | return FileManager::getModulePath($this) . DirsInterface::MODULE_CONFIG_DIR . PhpInterface::SLASH; |
||
276 | } |
||
277 | |||
278 | public function formatGenPath() |
||
279 | { |
||
280 | return DirsInterface::GEN_DIR . PhpInterface::SLASH . date('Y-m-d') . PhpInterface::SLASH; |
||
281 | } |
||
282 | |||
283 | public function formatGenPathByDir(): string |
||
284 | { |
||
285 | return DirsInterface::GEN_DIR . PhpInterface::SLASH . $this->genDir . PhpInterface::SLASH; |
||
286 | } |
||
287 | |||
288 | public function formatFuncTestsPath() |
||
289 | { |
||
290 | return DirsInterface::TESTS_DIR . PhpInterface::SLASH . DirsInterface::TESTS_FUNC_DIR; |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * @param string $name |
||
295 | */ |
||
296 | private function setObjectName(string $name) |
||
297 | { |
||
298 | $this->objectName = $name; |
||
299 | } |
||
300 | |||
301 | private function setObjectProps($props) |
||
302 | { |
||
303 | $this->objectProps = $props; |
||
304 | } |
||
305 | |||
306 | private function setIncludedTypes() |
||
321 | } |
||
322 | } |
||
323 | } |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * @throws \SoliDry\Exceptions\DirectoryException |
||
328 | */ |
||
329 | private function setGenHistory() |
||
330 | { |
||
331 | if (empty($this->options[ConsoleInterface::OPTION_NO_HISTORY])) { |
||
332 | // create .gen dir to store raml history |
||
333 | FileManager::createPath($this->formatGenPath()); |
||
334 | foreach ($this->files as $file) { |
||
339 | } |
||
340 | } |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * Get files to process within rollback |
||
345 | * |
||
346 | * @return array |
||
347 | * @throws \Symfony\Component\Yaml\Exception\ParseException |
||
348 | * @throws DirectoryException |
||
349 | */ |
||
350 | protected function getRollbackInputFile(): array |
||
375 | } |
||
376 | } |