diff --git a/composer.json b/composer.json index 90d089c..a6e5462 100644 --- a/composer.json +++ b/composer.json @@ -26,10 +26,10 @@ "webinertia/webinertia-db": "^0.0.3 || ^0.0.4 || ^0.0.5" }, "require-dev": { - "laminas/laminas-coding-standard": "^2.5.0", - "phpunit/phpunit": "10.2.6", + "laminas/laminas-coding-standard": "^2.5", + "phpunit/phpunit": "^10.2", "psalm/plugin-phpunit": "^0.18.4", - "phpstan/phpstan": "1.10.25", + "phpstan/phpstan": "^1.10", "laminas/laminas-test": "^4.8", "phpstan/extension-installer": "^1.2", "slam/phpstan-laminas-framework": "^1.4" diff --git a/src/AbstractHandler.php b/src/AbstractHandler.php index eaf5730..2493398 100644 --- a/src/AbstractHandler.php +++ b/src/AbstractHandler.php @@ -16,6 +16,11 @@ abstract class AbstractHandler extends AbstractModel implements UploaderHandlerI public const BASE_PUBLIC_PATH = __DIR__ . '/upload'; public const BASE_LOCAL_PATH = __DIR__ . '/../../../../public/upload'; + /** @var array $messageTemplate */ + protected $messageTemplate = [ + 'FILE_NOT_FOUND' => 'The requested file could not be found.', + 'FILE_NOT_DELETED' => 'File could not be deleted.' + ]; /** @var array $columns */ protected array $columnMap = [ 'id', diff --git a/src/Exception/Exception.php b/src/Exception/Exception.php new file mode 100644 index 0000000..9c045c4 --- /dev/null +++ b/src/Exception/Exception.php @@ -0,0 +1,11 @@ + $uploaded */ + protected array $uploaded = []; + + public function handleUpload(UploaderEvent $event): array { $request = $event->getRequest(); $this->exchangeArray($event->getParams()); $files = $request->getUploadedFiles(); $iterator = new RecursiveArrayIterator($files); iterator_apply($iterator, self::class . '::fileSearch', [$iterator]); - return 'last'; + return $this->uploaded; } - public function handleDelete(UploaderEvent $event) + /** + * @param UploaderEvent $event + * @return array + * @throws Exception\RunTimeException + * @throws RecordNotFound + */ + public function handleDelete(UploaderEvent $event): array { + $deleted = []; $params = $event->getParams(); if (! isset($params['uuid'])) { throw new Exception\RunTimeException('Delete Event requires the file uuid to be passed as a param'); } - $data = $this->fetchByColumn('uuid', $params['uuid']); - if ($data instanceof self) { - $fileInfo = new SplFileInfo($data->target . $data->fileName); - if ($fileInfo->isFile()) { - if(unlink($fileInfo->getRealPath())) { - // this must use offsetGet due to uuid being a class property - if ($this->delete(['uuid' => $data->offsetGet('uuid')])) { - return $data->offsetGet('uuid'); - } else { + /** @var ResultSetInterface|HydratingResultSet $fileData */ + $resultSet = $this->fetchByColumn('uuid', $params['uuid']); + if ($resultSet->count() === 0) { + throw new RecordNotFound('The requested records could not be found'); + } - } + foreach ($resultSet as $file) { + if ($file instanceof self) { + $data = $file->getArrayCopy(); + $result = $this->deleteFile(new SplFileInfo($data['target'] . $data['fileName'])); + if ($result && $this->delete(['uuid' => $data['uuid']]) !== 0) { + $deleted[] = [$data['uuid'] => $data['fileName']]; } } } + return $deleted; } + protected function deleteFile(SplFileInfo $file): bool + { + if (! $file->isFile()) { + throw new Exception\RunTimeException('File not found.'); + } + return unlink($file->getRealPath()); + } protected function fileSearch($iterator) { while ($iterator->valid() ) { if ($iterator->hasChildren()) { $current = $iterator->current(); if ($current instanceof UploadedFile) { - $this->processUploadedFile($current); + $this->uploaded[] = $this->processUploadedFile($current); } $this->fileSearch($iterator->getChildren()); } else { $current = $iterator->current(); if ($current instanceof UploadedFile) { - $this->processUploadedFile($current); + $this->uploaded[] = $this->processUploadedFile($current); } } $iterator->next(); } + return $this->uploaded; } protected function processUploadedFile(UploadedFile $file) @@ -83,5 +103,6 @@ protected function processUploadedFile(UploadedFile $file) $this->offsetSet('uuid', $uuid->filter(null)); $this->save($this); } + return $this->getArrayCopy(); } }