| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace allejo\stakx\System; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Symfony\Component\Filesystem\Exception\FileNotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Component\Finder\SplFileInfo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * All folder paths stored inside of this class will **not** have the ending DIRECTORY_SEPARATOR | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @internal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class Folder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     protected $fs; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     protected $absolutePath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     protected $targetDirectories; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 | 5 |  |     public function __construct ($folderPath) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 5 |  |         $this->fs = new Filesystem(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 5 |  |         $this->targetDirectories = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         // Setup the absolute path to the directory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 5 |  |         if (substr($folderPath, 0, 1) === DIRECTORY_SEPARATOR) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 5 |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             $this->absolutePath = $folderPath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         else | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 5 |  |             $this->absolutePath = $this->fs->absolutePath($folderPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         // Ensure our directory paths will don't end with a '/' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 5 |  |         $this->absolutePath = rtrim($this->absolutePath, DIRECTORY_SEPARATOR); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 5 |  |         if (!$this->fs->isDir($this->absolutePath)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 5 |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             throw new \InvalidArgumentException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 5 |  |         if (!$this->fs->exists($this->absolutePath)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 5 |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             throw new FileNotFoundException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 5 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     public function __toString () | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         return rtrim($this->absolutePath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * Set a base folder that will be prefixed before all file writes and copies | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @param string $folderName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @since 0.1.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     public function setTargetDirectory ($folderName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         if (is_null($folderName) || empty($folderName)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             $this->targetDirectories = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         else | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             $this->targetDirectories[] = trim($folderName, DIRECTORY_SEPARATOR); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * Copy a file from an absolute file to a path relative to the Folder's location | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @param string $absolutePath The absolute path of the file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @param string $targetPath   The relative file path to the Folder's location | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @since 0.1.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     public function copyFile ($absolutePath, $targetPath) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $targetPath = ltrim($targetPath, DIRECTORY_SEPARATOR); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $this->fs->copy( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             $absolutePath, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             $this->buildPath($this->getCwd(), $targetPath), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             true | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * Write a file with the specified content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @param string $relativePath The file path relative to this Folder's location | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @param string $content      The content that will be written to the file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * @since 0.1.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @return SplFileInfo | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 99 | 5 | View Code Duplication |     public function writeFile ($relativePath, $content) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $outputFolder   = $this->fs->getFolderPath($relativePath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         $targetFileName = $this->fs->getFileName($outputFolder); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $absoluteFolderPath = $this->buildPath($this->getCwd(), $outputFolder); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         if (!file_exists($absoluteFolderPath)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             mkdir($absoluteFolderPath, 0755, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         file_put_contents( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             $this->buildPath($this->getCwd(), $relativePath), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             $content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         return (new SplFileInfo( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 5 |  |             $targetFileName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             $absoluteFolderPath, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             $this->buildPath($absoluteFolderPath, $targetFileName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * @param string $pathFragments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     private function buildPath ($pathFragments) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         $paths = func_get_args(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         return implode(DIRECTORY_SEPARATOR, $paths); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * Returns the absolute path of where files will be placed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 139 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 140 |  |  |     private function getCwd () | 
            
                                                        
            
                                    
            
            
                | 141 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 142 |  |  |         $location = array_merge(array($this->absolutePath), $this->targetDirectories); | 
            
                                                        
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 144 |  |  |         return implode(DIRECTORY_SEPARATOR, $location); | 
            
                                                        
            
                                    
            
            
                | 145 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 146 |  |  | } | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.