| @@ 29-90 (lines=62) @@ | ||
| 26 | * @author Beñat Espiña <[email protected]> |
|
| 27 | * @author Gorka Laucirica <[email protected]> |
|
| 28 | */ |
|
| 29 | class ByHashUploadFileHandler |
|
| 30 | { |
|
| 31 | /** |
|
| 32 | * The file factory. |
|
| 33 | * |
|
| 34 | * @var FileFactory |
|
| 35 | */ |
|
| 36 | private $factory; |
|
| 37 | ||
| 38 | /** |
|
| 39 | * The filesystem. |
|
| 40 | * |
|
| 41 | * @var Filesystem |
|
| 42 | */ |
|
| 43 | private $filesystem; |
|
| 44 | ||
| 45 | /** |
|
| 46 | * The file repository. |
|
| 47 | * |
|
| 48 | * @var FileRepository |
|
| 49 | */ |
|
| 50 | private $repository; |
|
| 51 | ||
| 52 | /** |
|
| 53 | * Constructor. |
|
| 54 | * |
|
| 55 | * @param Filesystem $filesystem The filesystem |
|
| 56 | * @param FileRepository $aRepository The file repository |
|
| 57 | * @param FileFactory $aFactory The file factory |
|
| 58 | */ |
|
| 59 | public function __construct(Filesystem $filesystem, FileRepository $aRepository, FileFactory $aFactory) |
|
| 60 | { |
|
| 61 | $this->factory = $aFactory; |
|
| 62 | $this->filesystem = $filesystem; |
|
| 63 | $this->repository = $aRepository; |
|
| 64 | } |
|
| 65 | ||
| 66 | /** |
|
| 67 | * Handles the given command. |
|
| 68 | * |
|
| 69 | * @param UploadFileCommand $aCommand The command |
|
| 70 | * |
|
| 71 | * @throws FileAlreadyExistsException when file is already exists |
|
| 72 | */ |
|
| 73 | public function __invoke(UploadFileCommand $aCommand) |
|
| 74 | { |
|
| 75 | $id = new FileId($aCommand->id()); |
|
| 76 | $file = $this->repository->fileOfId($id); |
|
| 77 | if (null !== $file) { |
|
| 78 | throw new FileAlreadyExistsException(); |
|
| 79 | } |
|
| 80 | $name = FileName::fromHash($aCommand->name()); |
|
| 81 | if (true === $this->filesystem->has($name)) { |
|
| 82 | throw new FileAlreadyExistsException(); |
|
| 83 | } |
|
| 84 | ||
| 85 | $this->filesystem->write($name, $aCommand->uploadedFile()); |
|
| 86 | $file = $this->factory->build($id, $name, new FileMimeType($aCommand->mimeType())); |
|
| 87 | ||
| 88 | $this->repository->persist($file); |
|
| 89 | } |
|
| 90 | } |
|
| 91 | ||
| @@ 29-90 (lines=62) @@ | ||
| 26 | * @author Beñat Espiña <[email protected]> |
|
| 27 | * @author Gorka Laucirica <[email protected]> |
|
| 28 | */ |
|
| 29 | class UploadFileHandler |
|
| 30 | { |
|
| 31 | /** |
|
| 32 | * The file factory. |
|
| 33 | * |
|
| 34 | * @var FileFactory |
|
| 35 | */ |
|
| 36 | private $factory; |
|
| 37 | ||
| 38 | /** |
|
| 39 | * The filesystem. |
|
| 40 | * |
|
| 41 | * @var Filesystem |
|
| 42 | */ |
|
| 43 | private $filesystem; |
|
| 44 | ||
| 45 | /** |
|
| 46 | * The file repository. |
|
| 47 | * |
|
| 48 | * @var FileRepository |
|
| 49 | */ |
|
| 50 | private $repository; |
|
| 51 | ||
| 52 | /** |
|
| 53 | * Constructor. |
|
| 54 | * |
|
| 55 | * @param Filesystem $filesystem The filesystem |
|
| 56 | * @param FileRepository $aRepository The file repository |
|
| 57 | * @param FileFactory $aFactory The file factory |
|
| 58 | */ |
|
| 59 | public function __construct(Filesystem $filesystem, FileRepository $aRepository, FileFactory $aFactory) |
|
| 60 | { |
|
| 61 | $this->factory = $aFactory; |
|
| 62 | $this->filesystem = $filesystem; |
|
| 63 | $this->repository = $aRepository; |
|
| 64 | } |
|
| 65 | ||
| 66 | /** |
|
| 67 | * Handles the given command. |
|
| 68 | * |
|
| 69 | * @param UploadFileCommand $aCommand The command |
|
| 70 | * |
|
| 71 | * @throws FileAlreadyExistsException when file is already exists |
|
| 72 | */ |
|
| 73 | public function __invoke(UploadFileCommand $aCommand) |
|
| 74 | { |
|
| 75 | $id = new FileId($aCommand->id()); |
|
| 76 | $file = $this->repository->fileOfId($id); |
|
| 77 | if (null !== $file) { |
|
| 78 | throw new FileAlreadyExistsException(); |
|
| 79 | } |
|
| 80 | $name = new FileName($aCommand->name()); |
|
| 81 | if (true === $this->filesystem->has($name)) { |
|
| 82 | throw new FileAlreadyExistsException(); |
|
| 83 | } |
|
| 84 | ||
| 85 | $this->filesystem->write($name, $aCommand->uploadedFile()); |
|
| 86 | $file = $this->factory->build($id, $name, new FileMimeType($aCommand->mimeType())); |
|
| 87 | ||
| 88 | $this->repository->persist($file); |
|
| 89 | } |
|
| 90 | } |
|
| 91 | ||