@@ -29,8 +29,8 @@ discard block |
||
| 29 | 29 | /** |
| 30 | 30 | * @param $storageDirectory |
| 31 | 31 | */ |
| 32 | - public function __construct( $storageDirectory ){ |
|
| 33 | - if( $storageDirectory{-1} !== '/' ){ |
|
| 32 | + public function __construct($storageDirectory) { |
|
| 33 | + if ($storageDirectory{-1} !== '/') { |
|
| 34 | 34 | $storageDirectory .= '/'; |
| 35 | 35 | } |
| 36 | 36 | $this->storageDirectory = $storageDirectory; |
@@ -43,28 +43,28 @@ discard block |
||
| 43 | 43 | * @return string The path that $path was renamed to. |
| 44 | 44 | * @throws \Exception |
| 45 | 45 | */ |
| 46 | - public function moveToBin( $path ){ |
|
| 46 | + public function moveToBin($path) { |
|
| 47 | 47 | |
| 48 | - $path = Path::canonicalize( $path ); |
|
| 48 | + $path = Path::canonicalize($path); |
|
| 49 | 49 | |
| 50 | - $this->pathSafetyCheck( $path ); |
|
| 50 | + $this->pathSafetyCheck($path); |
|
| 51 | 51 | |
| 52 | 52 | $date = new \DateTimeImmutable("today"); |
| 53 | 53 | $dateStr = $date->format("c"); |
| 54 | - $unique = ( (string) microtime(true) ) .'-'. ( (string) rand(0, 100000 ) ); |
|
| 55 | - $basename = basename( $path ); |
|
| 54 | + $unique = ((string) microtime(true)).'-'.((string) rand(0, 100000)); |
|
| 55 | + $basename = basename($path); |
|
| 56 | 56 | |
| 57 | - $todayDir = $this->storageDirectory . $dateStr; |
|
| 57 | + $todayDir = $this->storageDirectory.$dateStr; |
|
| 58 | 58 | $finalRestingPlaceDir = "{$todayDir}/{$unique}"; |
| 59 | 59 | $finalRestingPlace = "{$finalRestingPlaceDir}/{$basename}"; |
| 60 | 60 | |
| 61 | - $this->ensureDirUsable( $this->storageDirectory ); |
|
| 62 | - $this->ensureDirUsable( $todayDir ); |
|
| 63 | - $this->ensureDirUsable( $finalRestingPlaceDir ); |
|
| 61 | + $this->ensureDirUsable($this->storageDirectory); |
|
| 62 | + $this->ensureDirUsable($todayDir); |
|
| 63 | + $this->ensureDirUsable($finalRestingPlaceDir); |
|
| 64 | 64 | |
| 65 | 65 | // Would use PHP's rename but... it doesn't always work |
| 66 | 66 | // when moving a directory to another device. |
| 67 | - exec( "mv {$path} {$finalRestingPlace}" ); |
|
| 67 | + exec("mv {$path} {$finalRestingPlace}"); |
|
| 68 | 68 | return $finalRestingPlace; |
| 69 | 69 | } |
| 70 | 70 | |
@@ -73,22 +73,22 @@ discard block |
||
| 73 | 73 | * @param $dir |
| 74 | 74 | * @throws \Exception |
| 75 | 75 | */ |
| 76 | - private function ensureDirUsable( $dir ) |
|
| 76 | + private function ensureDirUsable($dir) |
|
| 77 | 77 | { |
| 78 | - if (!file_exists( $dir )) { |
|
| 79 | - mkdir( $dir ); |
|
| 80 | - if (!file_exists( $dir )) { |
|
| 81 | - throw new \Exception( "{$dir} does not exist and could not be created." ); |
|
| 78 | + if (!file_exists($dir)) { |
|
| 79 | + mkdir($dir); |
|
| 80 | + if (!file_exists($dir)) { |
|
| 81 | + throw new \Exception("{$dir} does not exist and could not be created."); |
|
| 82 | 82 | } |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | - $this->pathSafetyCheck( $dir ); |
|
| 85 | + $this->pathSafetyCheck($dir); |
|
| 86 | 86 | |
| 87 | - $tmpFilename = tempnam( $dir, "___" ); |
|
| 88 | - if (!file_exists( $tmpFilename )) { |
|
| 89 | - throw new \Exception( "{$dir} does not appear to be writable." ); |
|
| 87 | + $tmpFilename = tempnam($dir, "___"); |
|
| 88 | + if (!file_exists($tmpFilename)) { |
|
| 89 | + throw new \Exception("{$dir} does not appear to be writable."); |
|
| 90 | 90 | } |
| 91 | - unlink( $tmpFilename ); |
|
| 91 | + unlink($tmpFilename); |
|
| 92 | 92 | } |
| 93 | 93 | |
| 94 | 94 | /** |
@@ -101,17 +101,17 @@ discard block |
||
| 101 | 101 | * |
| 102 | 102 | * @param int $keepDays 1 = today (back to 00:00 from now). |
| 103 | 103 | */ |
| 104 | - public function emptyBin( $keepDays = 1 ){ |
|
| 104 | + public function emptyBin($keepDays = 1) { |
|
| 105 | 105 | |
| 106 | - $items = $this->readDir( $this->storageDirectory ); |
|
| 106 | + $items = $this->readDir($this->storageDirectory); |
|
| 107 | 107 | // Items exclude parent directory! |
| 108 | - $toDelete = $this->generateDeletionList( $keepDays, $items ); |
|
| 108 | + $toDelete = $this->generateDeletionList($keepDays, $items); |
|
| 109 | 109 | |
| 110 | - if( count( $toDelete ) > 0 ){ |
|
| 111 | - foreach( $toDelete as $itemToDelete){ |
|
| 110 | + if (count($toDelete) > 0) { |
|
| 111 | + foreach ($toDelete as $itemToDelete) { |
|
| 112 | 112 | $fullPathToDelete = "{$this->storageDirectory}/{$itemToDelete}"; |
| 113 | - $this->pathSafetyCheck( $fullPathToDelete ); |
|
| 114 | - exec( "rm -rf {$fullPathToDelete}" ); |
|
| 113 | + $this->pathSafetyCheck($fullPathToDelete); |
|
| 114 | + exec("rm -rf {$fullPathToDelete}"); |
|
| 115 | 115 | } |
| 116 | 116 | } |
| 117 | 117 | } |
@@ -123,13 +123,13 @@ discard block |
||
| 123 | 123 | * @return array |
| 124 | 124 | * @throws \Exception |
| 125 | 125 | */ |
| 126 | - private function readDir( $directoryPath ) |
|
| 126 | + private function readDir($directoryPath) |
|
| 127 | 127 | { |
| 128 | 128 | $ret = []; |
| 129 | - $this->ensureDirUsable( $directoryPath ); |
|
| 130 | - $d = dir( $directoryPath ); |
|
| 131 | - while( false !== ( $entry = $d->read() ) ){ |
|
| 132 | - if( $entry === '.' || $entry === '..' ){ |
|
| 129 | + $this->ensureDirUsable($directoryPath); |
|
| 130 | + $d = dir($directoryPath); |
|
| 131 | + while (false !== ($entry = $d->read())) { |
|
| 132 | + if ($entry === '.' || $entry === '..') { |
|
| 133 | 133 | continue; |
| 134 | 134 | } |
| 135 | 135 | $ret[] = $entry; |
@@ -146,28 +146,28 @@ discard block |
||
| 146 | 146 | * @param $items |
| 147 | 147 | * @return array |
| 148 | 148 | */ |
| 149 | - private function generateDeletionList( $keepDays, $items ) |
|
| 149 | + private function generateDeletionList($keepDays, $items) |
|
| 150 | 150 | { |
| 151 | 151 | $keepStrings = []; |
| 152 | - if( $keepDays > 0 ){ |
|
| 153 | - for( $day = 0; $day < $keepDays; $day++ ){ |
|
| 152 | + if ($keepDays > 0) { |
|
| 153 | + for ($day = 0; $day < $keepDays; $day++) { |
|
| 154 | 154 | $dateTmp = new \DateTimeImmutable("today"); |
| 155 | - $dateTmp = $dateTmp->modify( "-{$day} days" ); |
|
| 155 | + $dateTmp = $dateTmp->modify("-{$day} days"); |
|
| 156 | 156 | $keepStrings[] = $dateTmp->format("c"); |
| 157 | 157 | } |
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | $toDelete = []; |
| 161 | - foreach( $items as $entry ){ |
|
| 161 | + foreach ($items as $entry) { |
|
| 162 | 162 | // In exclusion list? |
| 163 | - if( in_array( $entry, $keepStrings, true ) ){ |
|
| 163 | + if (in_array($entry, $keepStrings, true)) { |
|
| 164 | 164 | continue; |
| 165 | 165 | } |
| 166 | 166 | // Is a valid ISO 8601 date? |
| 167 | - if( preg_match( |
|
| 167 | + if (preg_match( |
|
| 168 | 168 | '/^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24\:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/', |
| 169 | 169 | $entry) |
| 170 | - ){ |
|
| 170 | + ) { |
|
| 171 | 171 | $toDelete[] = $entry; |
| 172 | 172 | } |
| 173 | 173 | } |
@@ -180,17 +180,17 @@ discard block |
||
| 180 | 180 | * @param $path |
| 181 | 181 | * @throws \Exception |
| 182 | 182 | */ |
| 183 | - private function pathSafetyCheck( $path ) |
|
| 183 | + private function pathSafetyCheck($path) |
|
| 184 | 184 | { |
| 185 | 185 | // Duplication is intentional for belt-and-braces approach |
| 186 | - $path = Path::canonicalize( $path ); |
|
| 187 | - if( ! file_exists( $path ) ){ |
|
| 188 | - throw new \Exception( "{$path} doesn't exist." ); |
|
| 186 | + $path = Path::canonicalize($path); |
|
| 187 | + if (!file_exists($path)) { |
|
| 188 | + throw new \Exception("{$path} doesn't exist."); |
|
| 189 | 189 | } |
| 190 | - if( is_dir( $path ) ){ |
|
| 190 | + if (is_dir($path)) { |
|
| 191 | 191 | // Path::canonicalize() leaves no trailing slash. |
| 192 | 192 | $path .= '/'; |
| 193 | - if( mb_substr_count( $path, '/', "UTF-8" ) < 3 ){ |
|
| 193 | + if (mb_substr_count($path, '/', "UTF-8") < 3) { |
|
| 194 | 194 | throw new \Exception("Can't use root level directories as recycle area or move or delete them: {$path}"); |
| 195 | 195 | } |
| 196 | 196 | } |