| Conditions | 14 |
| Paths | 24 |
| Total Lines | 48 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 168 | public function delete(?Asset $asset = null): void |
||
| 169 | { |
||
| 170 | if (null === $asset) { |
||
| 171 | return; |
||
| 172 | } |
||
| 173 | |||
| 174 | // If it is a SCORM package, try to remove its on-disk content (folder or ZIP) |
||
| 175 | if (Asset::SCORM === $asset->getCategory()) { |
||
| 176 | $path = $this->getFolder($asset); // may be an extracted folder or a .zip file |
||
| 177 | |||
| 178 | if ($path) { |
||
| 179 | try { |
||
| 180 | if ($this->filesystem->directoryExists($path)) { |
||
| 181 | $this->filesystem->deleteDirectory($path); |
||
| 182 | } elseif ($this->filesystem->fileExists($path)) { |
||
| 183 | $this->filesystem->delete($path); |
||
| 184 | } else { |
||
| 185 | // Local filesystem fallbacks (log only on true failure) |
||
| 186 | if (@is_dir($path)) { |
||
| 187 | $it = new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS); |
||
| 188 | $ri = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::CHILD_FIRST); |
||
| 189 | foreach ($ri as $file) { |
||
| 190 | $ok = $file->isDir() |
||
| 191 | ? @rmdir($file->getPathname()) |
||
| 192 | : @unlink($file->getPathname()); |
||
| 193 | if (!$ok) { |
||
| 194 | error_log('[AssetRepository::delete] Failed to remove path: '.$file->getPathname()); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | if (!@rmdir($path)) { |
||
| 198 | error_log('[AssetRepository::delete] Failed to remove directory: '.$path); |
||
| 199 | } |
||
| 200 | } elseif (@is_file($path)) { |
||
| 201 | if (!@unlink($path)) { |
||
| 202 | error_log('[AssetRepository::delete] Failed to remove file: '.$path); |
||
| 203 | } |
||
| 204 | } |
||
| 205 | } |
||
| 206 | } catch (\Throwable $e) { |
||
| 207 | error_log('[AssetRepository::delete] Exception while removing SCORM path '.$path.' - '.$e->getMessage()); |
||
| 208 | } |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | // Remove the asset record from the database |
||
| 213 | $em = $this->getEntityManager(); |
||
| 214 | $em->remove($asset); |
||
| 215 | $em->flush(); |
||
| 216 | } |
||
| 218 |