| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Fabiang\ExceptionGenerator\Generator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Fabiang\ExceptionGenerator\Event\CreateExceptionEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Fabiang\ExceptionGenerator\Generator\ExceptionClassNames; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Fabiang\ExceptionGenerator\Generator\TemplateRenderer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use function file_put_contents; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use function is_dir; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use function is_file; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use function mkdir; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class CreateException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     protected TemplateRenderer $templateRenderer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     protected EventDispatcherInterface $eventDispatcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     protected bool $overwrite = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * for skipping confirmation to overwrite existing files | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     protected bool $skipAll = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 5 |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         EventDispatcherInterface $eventDispatcher, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         TemplateRenderer $templateRenderer, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         bool $overwrite = false | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 5 |  |         $this->templateRenderer = $templateRenderer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 5 |  |         $this->eventDispatcher  = $eventDispatcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 5 |  |         $this->overwrite        = $overwrite; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * creates the exception classes and the exception folder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 5 |  |     public function create(string $namespace, string $path, ?string $usePath = null): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 5 |  |         $exceptionNames = ExceptionClassNames::getExceptionClassNames(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         //create the dir for exception classes if not already exists | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 5 |  |         $path .= '/'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 5 |  |         if (! is_dir($path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 1 |  |             mkdir($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 5 |  |         if (null !== $usePath) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             $usePath .= '\\'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 5 |  |         if ($this->overwrite) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 1 |  |             $this->eventDispatcher->dispatch(new CreateExceptionEvent($path), 'overwrite.all'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 5 |  |         foreach ($exceptionNames as $name) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 5 |  |             $fileName = $path . $name . '.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 5 |  |             if ($this->validate($fileName)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 4 |  |                 $specifiedUsePath = null !== $usePath ? $usePath . $name : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 4 |  |                 $content          = $this->templateRenderer->render($namespace, $specifiedUsePath, $name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 4 |  |                 $event            = new CreateExceptionEvent($fileName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 4 |  |                 $this->eventDispatcher->dispatch($event, 'write.file'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 4 |  |                 file_put_contents($fileName, $content); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 1 |  |                 $event = new CreateExceptionEvent($fileName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 1 |  |                 $this->eventDispatcher->dispatch($event, 'creation.skipped'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 5 |  |         $fileName = $path . 'ExceptionInterface.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 5 |  |         if ($this->validate($fileName)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 4 |  |             $specifiedUsePath = null !== $usePath ? $usePath . 'ExceptionInterface' : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 4 |  |             $content          = $this->templateRenderer->render($namespace, $specifiedUsePath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 4 |  |             $event            = new CreateExceptionEvent($fileName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 4 |  |             $this->eventDispatcher->dispatch($event, 'write.file'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 4 |  |             file_put_contents($fileName, $content); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 1 |  |             $event = new CreateExceptionEvent($fileName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  |             $this->eventDispatcher->dispatch($event, 'creation.skipped'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * Check if file exists, and if so ask for overwrite confirmation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 5 |  |     protected function validate(string $fileName): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 5 |  |         $fileExists = is_file($fileName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         // if user has set overwrite argument or file doesnt already exists return early | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 5 |  |         if ($this->overwrite || ! $fileExists) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 3 |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         // if user has chosen to skip overwriting all existing files, then return early | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 3 |  |         if ($this->skipAll) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 3 |  |         $overwrite = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 3 |  |         $confirm   = $this->confirm($fileName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         switch ($confirm) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 3 |  |             case 'all': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 1 |  |                 $this->overwrite = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 1 |  |                 $overwrite       = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 1 |  |                 $this->eventDispatcher->dispatch(new CreateExceptionEvent($fileName), 'overwrite.all'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 1 |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 2 |  |             case 'yes': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 1 |  |                 $overwrite = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 1 |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 1 |  |             case 'nall': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                 $this->skipAll = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                 $overwrite     = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                 $this->eventDispatcher->dispatch(new CreateExceptionEvent($fileName), 'skip.all'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 1 |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 3 |  |         return $overwrite; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * Ask for user confirmation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 3 |  |     protected function confirm(string $fileName): ?string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 3 |  |         $event = new CreateExceptionEvent($fileName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 3 |  |         $this->eventDispatcher->dispatch($event, 'overwrite.confirm'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 3 |  |         return $event->getConfirm(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * Set that create overwrites classes. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 142 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 143 | 5 |  |     public function setOverwrite(bool $overwrite): void | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 145 | 5 |  |         $this->overwrite = $overwrite; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 147 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 148 |  |  |  |