1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the BenGorFile package. |
5
|
|
|
* |
6
|
|
|
* (c) Beñat Espiña <[email protected]> |
7
|
|
|
* (c) Gorka Laucirica <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace BenGorFile\File\Application\Command\Upload; |
14
|
|
|
|
15
|
|
|
use BenGorFile\File\Domain\Model\FileAlreadyExistsException; |
16
|
|
|
use BenGorFile\File\Domain\Model\FileFactory; |
17
|
|
|
use BenGorFile\File\Domain\Model\FileId; |
18
|
|
|
use BenGorFile\File\Domain\Model\FileMimeType; |
19
|
|
|
use BenGorFile\File\Domain\Model\FileName; |
20
|
|
|
use BenGorFile\File\Domain\Model\FileRepository; |
21
|
|
|
use BenGorFile\File\Domain\Model\FileSpecificationFactory; |
22
|
|
|
use BenGorFile\File\Domain\Model\Filesystem; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Upload with suffix number file handler class. |
26
|
|
|
* |
27
|
|
|
* @author Beñat Espiña <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class SuffixNumberUploadFileHandler |
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
|
|
|
* The specification factory. |
54
|
|
|
* |
55
|
|
|
* @var FileSpecificationFactory |
56
|
|
|
*/ |
57
|
|
|
private $specificationFactory; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Constructor. |
61
|
|
|
* |
62
|
|
|
* @param Filesystem $filesystem The filesystem |
63
|
|
|
* @param FileRepository $aRepository The file repository |
64
|
|
|
* @param FileSpecificationFactory $aSpecificationFactory The file specification factory |
65
|
|
|
* @param FileFactory $aFactory The file factory |
66
|
|
|
*/ |
67
|
|
|
public function __construct( |
68
|
|
|
Filesystem $filesystem, |
69
|
|
|
FileRepository $aRepository, |
70
|
|
|
FileSpecificationFactory $aSpecificationFactory, |
71
|
|
|
FileFactory $aFactory |
72
|
|
|
) { |
73
|
|
|
$this->factory = $aFactory; |
74
|
|
|
$this->filesystem = $filesystem; |
75
|
|
|
$this->repository = $aRepository; |
76
|
|
|
$this->specificationFactory = $aSpecificationFactory; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Handles the given command. |
81
|
|
|
* |
82
|
|
|
* @param SuffixNumberUploadFileCommand $aCommand The command |
83
|
|
|
* |
84
|
|
|
* @throws FileAlreadyExistsException when file is already exists |
85
|
|
|
*/ |
86
|
|
|
public function __invoke(SuffixNumberUploadFileCommand $aCommand) |
87
|
|
|
{ |
88
|
|
|
$id = $aCommand->id(); |
89
|
|
|
$name = $aCommand->name(); |
90
|
|
|
$uploadedFile = $aCommand->uploadedFile(); |
91
|
|
|
$mimeType = $aCommand->mimeType(); |
92
|
|
|
|
93
|
|
|
$fileId = new FileId($id); |
94
|
|
|
$fileName = new FileName($name); |
95
|
|
|
$fileMimeType = new FileMimeType($mimeType); |
96
|
|
|
|
97
|
|
|
$this->checkFileExists($fileId); |
98
|
|
|
$fileName = $this->buildName($fileName); |
99
|
|
|
$this->filesystem->write($fileName, $uploadedFile); |
100
|
|
|
$file = $this->factory->build($fileId, $fileName, $fileMimeType); |
101
|
|
|
$this->repository->persist($file); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
private function checkFileExists(FileId $id) |
105
|
|
|
{ |
106
|
|
|
$file = $this->repository->fileOfId($id); |
107
|
|
|
if (null !== $file) { |
108
|
|
|
throw new FileAlreadyExistsException(); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
private function buildName(FileName $fileName) |
113
|
|
|
{ |
114
|
|
|
$name = $fileName->name(); |
115
|
|
|
$extension = $fileName->extension(); |
116
|
|
|
|
117
|
|
|
$numberOfFiles = $this->repository->count( |
118
|
|
|
$this->specificationFactory->buildByNameSpecification($fileName) |
119
|
|
|
); |
120
|
|
|
|
121
|
|
|
if ($numberOfFiles > 0) { |
122
|
|
|
$name = sprintf('%s-%s', $name, $numberOfFiles + 1); |
123
|
|
|
} |
124
|
|
|
$fileName = sprintf('%s.%s', $name, $extension); |
125
|
|
|
|
126
|
|
|
return new FileName($fileName); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|