| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Created by jensk on 17-3-2017. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | namespace CloudControl\Cms\storage\storage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class FilesStorage extends AbstractStorage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     protected $filesDir; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     public function __construct($repository, $filesDir) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         parent::__construct($repository); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         $this->filesDir = $filesDir; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	 * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	public function getFiles() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 		$files = $this->repository->files; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 		usort($files, array($this, 'compareFiles')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 		return $files; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	 * @param $postValues | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	 * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	public function addFile($postValues) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 		$destinationPath = $this->getDestinationPath(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 		$filename = $this->validateFilename($postValues['name'], $destinationPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 		$destination = $destinationPath . '/' . $filename; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 		if ($postValues['error'] != '0') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 			throw new \Exception('Error uploading file. Error code: ' . $postValues['error']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 		if (move_uploaded_file($postValues['tmp_name'], $destination)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 			$file = new \stdClass(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 			$file->file = $filename; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 			$file->type = $postValues['type']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 			$file->size = $postValues['size']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 			$files = $this->repository->files; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 			$files[] = $file; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 			$this->repository->files = $files; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 			$this->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 			throw new \Exception('Error moving uploaded file'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	 * @param $filename | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	 * @return null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	public function getFileByName($filename) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		$files = $this->getFiles(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		foreach ($files as $file) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 			if ($filename == $file->file) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 				return $file; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 		return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	 * @param $filename | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 	 * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 83 |  | View Code Duplication | 	public function deleteFileByName($filename) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 		$destinationPath = $this->getDestinationPath(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		$destination = $destinationPath . '/' . $filename; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 		if (file_exists($destination)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 			$files = $this->getFiles(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 			foreach ($files as $key => $file) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 				if ($file->file == $filename) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 					unlink($destination); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 					unset($files[$key]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 			$files = array_values($files); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 			$this->repository->files = $files; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 			$this->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @return mixed | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 105 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |     public function getFilesDir() | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |         return $this->filesDir; | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 	 * @param $a | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 	 * @param $b | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 	 * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 	private function compareFiles($a, $b) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 		return strcmp($a->file, $b->file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     protected function getDestinationPath() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $destinationPath = realpath($this->filesDir . DIRECTORY_SEPARATOR); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         return $destinationPath; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 127 |  |  | } | 
            
                        
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.