| Total Complexity | 43 |
| Total Lines | 485 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Complex classes like BlockMaker 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 BlockMaker, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class BlockMaker |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @var Filesystem |
||
| 15 | */ |
||
| 16 | protected $files; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var \A17\Twill\Services\Blocks\BlockCollection |
||
| 20 | */ |
||
| 21 | protected $blockCollection; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var \Illuminate\Console\Command |
||
| 25 | */ |
||
| 26 | protected $command; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var \A17\Twill\Services\Blocks\Block |
||
| 30 | */ |
||
| 31 | protected $blockBase; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string` |
||
|
|
|||
| 35 | */ |
||
| 36 | protected $icon; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param Filesystem $files |
||
| 40 | * @param \A17\Twill\Services\Blocks\BlockCollection $blockCollection |
||
| 41 | */ |
||
| 42 | public function __construct( |
||
| 43 | Filesystem $files, |
||
| 44 | BlockCollection $blockCollection |
||
| 45 | ) { |
||
| 46 | $this->files = $files; |
||
| 47 | |||
| 48 | $this->blockCollection = $blockCollection; |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @return \A17\Twill\Services\Blocks\BlockCollection |
||
| 53 | */ |
||
| 54 | public function getBlockCollection() |
||
| 55 | { |
||
| 56 | return $this->blockCollection; |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Make a new block. |
||
| 61 | * |
||
| 62 | * @param $blockName |
||
| 63 | * @param $baseName |
||
| 64 | * @param $iconName |
||
| 65 | * @return mixed |
||
| 66 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
||
| 67 | * @throws \Exception |
||
| 68 | */ |
||
| 69 | public function make($blockName, $baseName, $iconName) |
||
| 70 | { |
||
| 71 | $this->info('Creating block...'); |
||
| 72 | |||
| 73 | if ( |
||
| 74 | !$this->checkBlockStub($baseName) || |
||
| 75 | !$this->checkIconFile($iconName) || |
||
| 76 | !$this->checkBlockBaseFormat( |
||
| 77 | $stubFileName = $this->blockBase->file->getPathName() |
||
| 78 | ) |
||
| 79 | ) { |
||
| 80 | return false; |
||
| 81 | } |
||
| 82 | |||
| 83 | if ( |
||
| 84 | !$this->checkBlockFile( |
||
| 85 | $blockFile = $this->makeBlockPath( |
||
| 86 | $blockIdentifier = $this->makeBlockIdentifier($blockName) |
||
| 87 | ) |
||
| 88 | ) |
||
| 89 | ) { |
||
| 90 | return false; |
||
| 91 | } |
||
| 92 | |||
| 93 | $this->blockBase = $this->makeBlock( |
||
| 94 | $blockName, |
||
| 95 | $iconName, |
||
| 96 | $stubFileName |
||
| 97 | ); |
||
| 98 | |||
| 99 | if ( |
||
| 100 | !$this->checkRepeaters( |
||
| 101 | $repeaters = $this->generateRepeaters( |
||
| 102 | $baseName, |
||
| 103 | $blockIdentifier, |
||
| 104 | $this->blockBase |
||
| 105 | ) |
||
| 106 | ) |
||
| 107 | ) { |
||
| 108 | return false; |
||
| 109 | } |
||
| 110 | |||
| 111 | return $this->saveAllFiles( |
||
| 112 | $blockName, |
||
| 113 | $blockFile, |
||
| 114 | $repeaters, |
||
| 115 | $blockIdentifier |
||
| 116 | ); |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param $baseName |
||
| 121 | * @return bool |
||
| 122 | * @throws \Exception |
||
| 123 | */ |
||
| 124 | protected function checkBlockStub($baseName) |
||
| 125 | { |
||
| 126 | if (blank($this->blockBase = $this->getBlockByName($baseName))) { |
||
| 127 | $this->error("Block '{$baseName}' doesn't exists."); |
||
| 128 | |||
| 129 | return false; |
||
| 130 | } |
||
| 131 | |||
| 132 | return true; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @param $iconName |
||
| 137 | * @return bool |
||
| 138 | */ |
||
| 139 | protected function checkIconFile($iconName) |
||
| 140 | { |
||
| 141 | if (blank($this->icon = $this->getIconFile($iconName))) { |
||
| 142 | $this->error("Icon '{$iconName}' doesn't exists."); |
||
| 143 | |||
| 144 | return false; |
||
| 145 | } |
||
| 146 | |||
| 147 | return true; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param $blockFile |
||
| 152 | * @return bool |
||
| 153 | */ |
||
| 154 | protected function checkBlockFile($blockFile) |
||
| 155 | { |
||
| 156 | $this->info("File: {$blockFile}"); |
||
| 157 | |||
| 158 | if ($this->files->exists($blockFile)) { |
||
| 159 | $this->error('A file with the same name already exists.'); |
||
| 160 | |||
| 161 | return false; |
||
| 162 | } |
||
| 163 | |||
| 164 | return true; |
||
| 165 | } |
||
| 166 | |||
| 167 | protected function checkBlockBaseFormat($stubFileName) |
||
| 168 | { |
||
| 169 | if (!$this->blockBase->isNewFormat) { |
||
| 170 | $this->error( |
||
| 171 | "The block file '{$stubFileName}' format is the old one." |
||
| 172 | ); |
||
| 173 | |||
| 174 | $this->error('Please upgrade it before using as template.'); |
||
| 175 | |||
| 176 | return false; |
||
| 177 | } |
||
| 178 | |||
| 179 | return true; |
||
| 180 | } |
||
| 181 | |||
| 182 | protected function checkRepeaters($repeaters) |
||
| 183 | { |
||
| 184 | foreach ($repeaters as $repeater) { |
||
| 185 | $this->info("Repeater file: {$repeater['newRepeaterPath']}"); |
||
| 186 | |||
| 187 | if ($this->files->exists($repeater['newRepeaterPath'])) { |
||
| 188 | $this->error( |
||
| 189 | 'A repeater file with the same name already exists.' |
||
| 190 | ); |
||
| 191 | |||
| 192 | return false; |
||
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 196 | return true; |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @param $blockName |
||
| 201 | * @param $iconName |
||
| 202 | * @param $stubFileName |
||
| 203 | * @param null|string $stub |
||
| 204 | * @return string|string[]|null |
||
| 205 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
||
| 206 | */ |
||
| 207 | public function makeBlock( |
||
| 208 | $blockName, |
||
| 209 | $iconName, |
||
| 210 | $stubFileName = null, |
||
| 211 | $stub = null |
||
| 212 | ) { |
||
| 213 | $stub = $stub ?? $this->files->get($stubFileName); |
||
| 214 | |||
| 215 | $title = $this->makeBlockTitle($blockName); |
||
| 216 | |||
| 217 | $stub = preg_replace( |
||
| 218 | "/@a17-title\('(.*)'\)/", |
||
| 219 | "@a17-title('{$title}')", |
||
| 220 | $stub |
||
| 221 | ); |
||
| 222 | |||
| 223 | $stub = preg_replace( |
||
| 224 | "/@a17-group\('twill'\)/", |
||
| 225 | "@a17-group('app')", |
||
| 226 | $stub |
||
| 227 | ); |
||
| 228 | |||
| 229 | $stub = preg_replace( |
||
| 230 | "/@a17-icon\('(.*)'\)/", |
||
| 231 | "@a17-icon('{$iconName}')", |
||
| 232 | $stub |
||
| 233 | ); |
||
| 234 | |||
| 235 | return $stub; |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param $blockName |
||
| 240 | * @return string |
||
| 241 | * @throws \Exception |
||
| 242 | */ |
||
| 243 | protected function makeBlockIdentifier($blockName) |
||
| 244 | { |
||
| 245 | return (new Block( |
||
| 246 | $this->blockBase->file, |
||
| 247 | $this->blockBase->type, |
||
| 248 | $this->blockBase->source |
||
| 249 | ))->makeName($blockName); |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param string $blockIdentifier |
||
| 254 | * @param string $type |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | protected function makeBlockPath(string $blockIdentifier, $type = 'block') |
||
| 258 | { |
||
| 259 | $destination = config( |
||
| 260 | "twill.block_editor.directories.destination.{$type}s" |
||
| 261 | ); |
||
| 262 | |||
| 263 | if (!$this->files->exists($destination)) { |
||
| 264 | if ( |
||
| 265 | !config('twill.block_editor.directories.destination.make_dir') |
||
| 266 | ) { |
||
| 267 | throw new Exception( |
||
| 268 | "Destination directory does not exists: {$destination}" |
||
| 269 | ); |
||
| 270 | } |
||
| 271 | |||
| 272 | $this->files->makeDirectory($destination, 0755, true); |
||
| 273 | } |
||
| 274 | |||
| 275 | return "{$destination}/{$blockIdentifier}.blade.php"; |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @param $string |
||
| 280 | * @return string |
||
| 281 | */ |
||
| 282 | public function makeBlockTitle($string) |
||
| 283 | { |
||
| 284 | $string = Str::kebab($string); |
||
| 285 | |||
| 286 | $string = str_replace(['-', '_'], ' ', $string); |
||
| 287 | |||
| 288 | return Str::title($string); |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param $block |
||
| 293 | * @param array $sources |
||
| 294 | * @return mixed |
||
| 295 | * @throws \Exception |
||
| 296 | */ |
||
| 297 | public function getBlockByName($block, $sources = []) |
||
| 298 | { |
||
| 299 | return $this->blockCollection->findByName($block, $sources); |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @param $icon |
||
| 304 | * @return mixed |
||
| 305 | */ |
||
| 306 | public function getIconFile($icon) |
||
| 307 | { |
||
| 308 | $icon .= '.svg'; |
||
| 309 | |||
| 310 | return collect( |
||
| 311 | $this->files->files(__DIR__ . '/../../../frontend/icons') |
||
| 312 | )->reduce(function ($keep, SplFileInfo $file) use ($icon) { |
||
| 313 | if ($keep) { |
||
| 314 | return $keep; |
||
| 315 | } |
||
| 316 | |||
| 317 | return $file->getFilename() === $icon ? $file->getPathName() : null; |
||
| 318 | }, null); |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @param $baseName |
||
| 323 | * @param $blockName |
||
| 324 | * @param $blockBase |
||
| 325 | * @return \Illuminate\Support\Collection |
||
| 326 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
||
| 327 | */ |
||
| 328 | public function generateRepeaters($baseName, $blockName, &$blockBase) |
||
| 329 | { |
||
| 330 | preg_match_all( |
||
| 331 | '/@formField(.*\'repeater\'.*\[.*=>.*\'(.*)\'].*)/', |
||
| 332 | $blockBase, |
||
| 333 | $matches |
||
| 334 | ); |
||
| 335 | |||
| 336 | $repeaters = collect(); |
||
| 337 | |||
| 338 | if (count($matches) === 0) { |
||
| 339 | return null; |
||
| 340 | } |
||
| 341 | |||
| 342 | foreach ($matches[2] as $index => $repeaterName) { |
||
| 343 | if (Str::startsWith($repeaterName, $baseName)) { |
||
| 344 | $newRepeater = $this->createRepeater( |
||
| 345 | $repeaterName, |
||
| 346 | $baseName, |
||
| 347 | $blockName, |
||
| 348 | $blockBase, |
||
| 349 | $matches[0][$index] |
||
| 350 | ); |
||
| 351 | |||
| 352 | // Get the update version of the block stub, to be used on next repeaters |
||
| 353 | $blockBase = $newRepeater['newBlockStub']; |
||
| 354 | |||
| 355 | $oldRepeaterTag = $matches[0][0]; |
||
| 356 | $newRepeaterTag = str_replace( |
||
| 357 | "'{$repeaterName}'", |
||
| 358 | "'{$newRepeater['newRepeaterName']}'", |
||
| 359 | $oldRepeaterTag |
||
| 360 | ); |
||
| 361 | |||
| 362 | $blockBase = str_replace( |
||
| 363 | $oldRepeaterTag, |
||
| 364 | $newRepeaterTag, |
||
| 365 | $blockBase |
||
| 366 | ); |
||
| 367 | |||
| 368 | $repeaters->push($newRepeater); |
||
| 369 | } |
||
| 370 | } |
||
| 371 | |||
| 372 | return $repeaters; |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @param $repeaterName |
||
| 377 | * @param $baseName |
||
| 378 | * @param $blockName |
||
| 379 | * @param $blockBase |
||
| 380 | * @param $blockString |
||
| 381 | * @return array |
||
| 382 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
||
| 383 | * @throws \Exception |
||
| 384 | */ |
||
| 385 | public function createRepeater( |
||
| 386 | $repeaterName, |
||
| 387 | $baseName, |
||
| 388 | $blockName, |
||
| 389 | $blockBase, |
||
| 390 | $blockString |
||
| 391 | ) { |
||
| 392 | $baseRepeater = $this->blockCollection->findByName($repeaterName); |
||
| 393 | |||
| 394 | return [ |
||
| 395 | 'baseRepeater' => $baseRepeater, |
||
| 396 | |||
| 397 | 'newRepeaterName' => ($newRepeaterName = |
||
| 398 | $blockName . Str::after($repeaterName, $baseName)), |
||
| 399 | |||
| 400 | 'newRepeaterStub' => $this->makeBlock( |
||
| 401 | $newRepeaterName, |
||
| 402 | null, |
||
| 403 | null, |
||
| 404 | $baseRepeater->contents |
||
| 405 | ), |
||
| 406 | |||
| 407 | 'newRepeaterTitle' => $this->makeBlockTitle($newRepeaterName), |
||
| 408 | |||
| 409 | 'newRepeaterPath' => $this->makeBlockPath( |
||
| 410 | $newRepeaterName, |
||
| 411 | Block::TYPE_REPEATER |
||
| 412 | ), |
||
| 413 | |||
| 414 | 'newBlockString' => ($newBlockString = str_replace( |
||
| 415 | "'{$repeaterName}'", |
||
| 416 | "'{$newRepeaterName}'", |
||
| 417 | $blockString |
||
| 418 | )), |
||
| 419 | |||
| 420 | 'newBlockStub' => str_replace( |
||
| 421 | $blockString, |
||
| 422 | $newBlockString, |
||
| 423 | $blockBase |
||
| 424 | ), |
||
| 425 | ]; |
||
| 426 | } |
||
| 427 | |||
| 428 | public function put($filePath, $contents) |
||
| 429 | { |
||
| 430 | $directory = dirname($filePath); |
||
| 431 | |||
| 432 | if (!$this->files->exists($directory)) { |
||
| 433 | $this->files->makeDirectory($directory, 0755, true); |
||
| 434 | } |
||
| 435 | |||
| 436 | $this->files->put($filePath, $contents); |
||
| 437 | } |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @param $blockName |
||
| 441 | * @param string $blockFile |
||
| 442 | * @param \Illuminate\Support\Collection $repeaters |
||
| 443 | * @param string $blockIdentifier |
||
| 444 | * @return bool |
||
| 445 | */ |
||
| 446 | protected function saveAllFiles( |
||
| 466 | } |
||
| 467 | |||
| 468 | /** |
||
| 469 | * @param \Illuminate\Console\Command $command |
||
| 470 | * @return BlockMaker |
||
| 471 | */ |
||
| 472 | public function setCommand(Command $command) |
||
| 473 | { |
||
| 474 | $this->command = $command; |
||
| 475 | |||
| 476 | return $this; |
||
| 477 | } |
||
| 478 | |||
| 479 | /** |
||
| 480 | * @param $message |
||
| 481 | */ |
||
| 482 | public function info($message) |
||
| 486 | } |
||
| 487 | } |
||
| 488 | |||
| 489 | /** |
||
| 490 | * @param $message |
||
| 491 | */ |
||
| 492 | public function error($message) |
||
| 493 | { |
||
| 494 | if ($this->command) { |
||
| 495 | $this->command->displayError($message); |
||
| 496 | } |
||
| 497 | } |
||
| 498 | } |
||
| 499 |