| @@ 26-73 (lines=48) @@ | ||
| 23 | * @author Beñat Espiña <[email protected]> |
|
| 24 | * @author Gorka Laucirica <[email protected]> |
|
| 25 | */ |
|
| 26 | class RemoveFileHandler |
|
| 27 | { |
|
| 28 | /** |
|
| 29 | * The filesystem. |
|
| 30 | * |
|
| 31 | * @var Filesystem |
|
| 32 | */ |
|
| 33 | private $filesystem; |
|
| 34 | ||
| 35 | /** |
|
| 36 | * The file repository. |
|
| 37 | * |
|
| 38 | * @var FileRepository |
|
| 39 | */ |
|
| 40 | private $repository; |
|
| 41 | ||
| 42 | /** |
|
| 43 | * Constructor. |
|
| 44 | * |
|
| 45 | * @param Filesystem $filesystem The filesystem |
|
| 46 | * @param FileRepository $aRepository The file repository |
|
| 47 | */ |
|
| 48 | public function __construct(Filesystem $filesystem, FileRepository $aRepository) |
|
| 49 | { |
|
| 50 | $this->filesystem = $filesystem; |
|
| 51 | $this->repository = $aRepository; |
|
| 52 | } |
|
| 53 | ||
| 54 | /** |
|
| 55 | * Handles the given command. |
|
| 56 | * |
|
| 57 | * @param RemoveFileCommand $aCommand The command |
|
| 58 | * |
|
| 59 | * @throws FileException when file is already exists |
|
| 60 | */ |
|
| 61 | public function __invoke(RemoveFileCommand $aCommand) |
|
| 62 | { |
|
| 63 | $id = new FileId($aCommand->id()); |
|
| 64 | ||
| 65 | $file = $this->repository->fileOfId($id); |
|
| 66 | if (null === $file) { |
|
| 67 | throw FileException::idDoesNotExist($id); |
|
| 68 | } |
|
| 69 | $this->filesystem->delete($file->name()); |
|
| 70 | ||
| 71 | $this->repository->remove($file); |
|
| 72 | } |
|
| 73 | } |
|
| 74 | ||
| @@ 25-74 (lines=50) @@ | ||
| 22 | * |
|
| 23 | * @author Beñat Espiña <[email protected]> |
|
| 24 | */ |
|
| 25 | class FileOfIdHandler |
|
| 26 | { |
|
| 27 | /** |
|
| 28 | * The file data transformer. |
|
| 29 | * |
|
| 30 | * @var FileDataTransformer |
|
| 31 | */ |
|
| 32 | private $dataTransformer; |
|
| 33 | ||
| 34 | /** |
|
| 35 | * The file repository. |
|
| 36 | * |
|
| 37 | * @var FileRepository |
|
| 38 | */ |
|
| 39 | private $repository; |
|
| 40 | ||
| 41 | /** |
|
| 42 | * Constructor. |
|
| 43 | * |
|
| 44 | * @param FileRepository $aRepository The file repository |
|
| 45 | * @param FileDataTransformer $aDataTransformer The file data transformer |
|
| 46 | */ |
|
| 47 | public function __construct(FileRepository $aRepository, FileDataTransformer $aDataTransformer) |
|
| 48 | { |
|
| 49 | $this->repository = $aRepository; |
|
| 50 | $this->dataTransformer = $aDataTransformer; |
|
| 51 | } |
|
| 52 | ||
| 53 | /** |
|
| 54 | * Handles the given query. |
|
| 55 | * |
|
| 56 | * @param FileOfIdQuery $aQuery The query |
|
| 57 | * |
|
| 58 | * @throws FileException when the file id does not exist |
|
| 59 | * |
|
| 60 | * @return mixed |
|
| 61 | */ |
|
| 62 | public function __invoke(FileOfIdQuery $aQuery) |
|
| 63 | { |
|
| 64 | $fileId = new FileId($aQuery->id()); |
|
| 65 | $file = $this->repository->fileOfId($fileId); |
|
| 66 | if (null === $file) { |
|
| 67 | throw FileException::idDoesNotExist($fileId); |
|
| 68 | } |
|
| 69 | ||
| 70 | $this->dataTransformer->write($file); |
|
| 71 | ||
| 72 | return $this->dataTransformer->read(); |
|
| 73 | } |
|
| 74 | } |
|
| 75 | ||
| @@ 25-74 (lines=50) @@ | ||
| 22 | * |
|
| 23 | * @author Beñat Espiña <[email protected]> |
|
| 24 | */ |
|
| 25 | class FileOfNameHandler |
|
| 26 | { |
|
| 27 | /** |
|
| 28 | * The file data transformer. |
|
| 29 | * |
|
| 30 | * @var FileDataTransformer |
|
| 31 | */ |
|
| 32 | private $dataTransformer; |
|
| 33 | ||
| 34 | /** |
|
| 35 | * The file repository. |
|
| 36 | * |
|
| 37 | * @var FileRepository |
|
| 38 | */ |
|
| 39 | private $repository; |
|
| 40 | ||
| 41 | /** |
|
| 42 | * Constructor. |
|
| 43 | * |
|
| 44 | * @param FileRepository $aRepository The file repository |
|
| 45 | * @param FileDataTransformer $aDataTransformer The file data transformer |
|
| 46 | */ |
|
| 47 | public function __construct(FileRepository $aRepository, FileDataTransformer $aDataTransformer) |
|
| 48 | { |
|
| 49 | $this->repository = $aRepository; |
|
| 50 | $this->dataTransformer = $aDataTransformer; |
|
| 51 | } |
|
| 52 | ||
| 53 | /** |
|
| 54 | * Handles the given query. |
|
| 55 | * |
|
| 56 | * @param FileOfNameQuery $aQuery The query |
|
| 57 | * |
|
| 58 | * @throws FileException when the file name does not exist |
|
| 59 | * |
|
| 60 | * @return mixed |
|
| 61 | */ |
|
| 62 | public function __invoke(FileOfNameQuery $aQuery) |
|
| 63 | { |
|
| 64 | $fileName = new FileName($aQuery->name()); |
|
| 65 | $file = $this->repository->fileOfName($fileName); |
|
| 66 | if (null === $file) { |
|
| 67 | throw FileException::doesNotExist($fileName); |
|
| 68 | } |
|
| 69 | ||
| 70 | $this->dataTransformer->write($file); |
|
| 71 | ||
| 72 | return $this->dataTransformer->read(); |
|
| 73 | } |
|
| 74 | } |
|
| 75 | ||