| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Antidot\Installer\Template; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use RuntimeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use function file_exists; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use function file_put_contents; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use function is_dir; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use function is_writable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use function method_exists; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use function mkdir; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use function sprintf; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use function unlink; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  | abstract class CommonFileStructure implements FileStructureFactory | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     public const COMMUNITY_FILES = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |         '/CHANGELOG.md', | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |         '/CODE_OF_CONDUCT.md', | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |         '/CONTRIBUTING.md', | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         '/PULL_REQUEST_TEMPLATE.md', | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |         '/LICENSE', | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     public const NOT_WRITABLE_MESSAGE = 'Given directory "%s" is not writable.'; | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     public const NOT_PERMISSIONS_MESSAGE = 'Directory "%s" was not created by permission issues.'; | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 31 | 6 |  |     protected function verifyInstallationPath(string $installationPath): void | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 33 | 6 |  |         if (!is_dir($installationPath) && !mkdir($dir = $installationPath, 0755, true) && !is_dir($dir)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 34 | 1 |  |             throw new RuntimeException(sprintf(self::NOT_PERMISSIONS_MESSAGE, $dir)); | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 37 | 5 |  |         if (!is_writable($installationPath)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 1 |  |             throw new RuntimeException(sprintf(self::NOT_WRITABLE_MESSAGE, $installationPath)); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 40 | 4 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      * @param string $installationPath | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      * @param array<string> $directories | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 4 |  |     protected function createDirectories(string $installationPath, array $directories): void | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 4 |  |         foreach ($directories as $directory) { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 4 |  |             if (!mkdir($dir = sprintf('%s/%s', $installationPath, $directory), 0755, true) && !is_dir($dir)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |                 throw new RuntimeException(sprintf('Directory "%s" was not created', $dir)); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 4 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      * @param string $installationPath | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      * @param array<string, string> $files | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 4 |  |     protected function createFiles(string $installationPath, array $files): void | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 4 |  |         foreach ($files as $method => $filename) { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 | 4 |  |             file_put_contents(sprintf('%s/%s', $installationPath, $filename), $this->$method()); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 64 | 4 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 2 |  |     protected function removeCommunityFiles(string $installationPath): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 2 |  |         foreach (self::COMMUNITY_FILES as $fileToDelete) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 2 |  |             $filePath = $installationPath . $fileToDelete; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 2 |  |             if (file_exists($filePath)) { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 71 | 1 |  |                 unlink($filePath); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 72 |  |  |             } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 74 | 2 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 75 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |  |