1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Files\Service\Execute\Api; |
6
|
|
|
|
7
|
|
|
use AbterPhp\Admin\Service\Execute\RepoServiceAbstract; |
8
|
|
|
use AbterPhp\Files\Domain\Entities\File as Entity; |
9
|
|
|
use AbterPhp\Files\Domain\Entities\FileCategory; |
10
|
|
|
use AbterPhp\Files\Orm\FileCategoryRepo; |
11
|
|
|
use AbterPhp\Files\Orm\FileRepo as GridRepo; |
12
|
|
|
use AbterPhp\Files\Validation\Factory\Api\File as ValidatorFactory; |
13
|
|
|
use AbterPhp\Framework\Domain\Entities\IStringerEntity; |
14
|
|
|
use AbterPhp\Framework\Filesystem\Uploader; |
15
|
|
|
use Cocur\Slugify\Slugify; |
16
|
|
|
use Opulence\Events\Dispatchers\IEventDispatcher; |
17
|
|
|
use Opulence\Http\Requests\UploadedFile; |
18
|
|
|
use Opulence\Orm\IUnitOfWork; |
19
|
|
|
use Opulence\Orm\OrmException; |
20
|
|
|
|
21
|
|
|
class File extends RepoServiceAbstract |
22
|
|
|
{ |
23
|
|
|
const INPUT_NAME_FILE = 'file'; |
24
|
|
|
|
25
|
|
|
/** @var Slugify */ |
26
|
|
|
protected $slugify; |
27
|
|
|
|
28
|
|
|
/** @var FileCategoryRepo */ |
29
|
|
|
protected $fileCategoryRepo; |
30
|
|
|
|
31
|
|
|
/** @var Uploader */ |
32
|
|
|
protected $uploader; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* File constructor. |
36
|
|
|
* |
37
|
|
|
* @param GridRepo $repo |
38
|
|
|
* @param ValidatorFactory $validatorFactory |
39
|
|
|
* @param IUnitOfWork $unitOfWork |
40
|
|
|
* @param IEventDispatcher $eventDispatcher |
41
|
|
|
* @param Slugify $slugify |
42
|
|
|
* @param FileCategoryRepo $fileCategoryRepo |
43
|
|
|
* @param Uploader $uploader |
44
|
|
|
*/ |
45
|
|
|
public function __construct( |
46
|
|
|
GridRepo $repo, |
47
|
|
|
ValidatorFactory $validatorFactory, |
48
|
|
|
IUnitOfWork $unitOfWork, |
49
|
|
|
IEventDispatcher $eventDispatcher, |
50
|
|
|
Slugify $slugify, |
51
|
|
|
FileCategoryRepo $fileCategoryRepo, |
52
|
|
|
Uploader $uploader |
53
|
|
|
) { |
54
|
|
|
parent::__construct($repo, $validatorFactory, $unitOfWork, $eventDispatcher); |
55
|
|
|
|
56
|
|
|
$this->slugify = $slugify; |
57
|
|
|
$this->fileCategoryRepo = $fileCategoryRepo; |
58
|
|
|
$this->uploader = $uploader; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string[] $postData |
63
|
|
|
* @param UploadedFile[] $fileData |
64
|
|
|
* |
65
|
|
|
* @return Entity |
66
|
|
|
* @throws OrmException |
67
|
|
|
*/ |
68
|
|
|
public function create(array $postData, array $fileData): IStringerEntity |
69
|
|
|
{ |
70
|
|
|
$entity = $this->fillEntity($this->createEntity(''), $postData, $fileData); |
71
|
|
|
|
72
|
|
|
$this->uploadFile($entity, $fileData); |
73
|
|
|
|
74
|
|
|
if ($this->uploader->getErrors()) { |
75
|
|
|
return $entity; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->repo->add($entity); |
79
|
|
|
|
80
|
|
|
$this->commitCreate($entity); |
81
|
|
|
|
82
|
|
|
return $entity; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param IStringerEntity $entity |
87
|
|
|
* @param string[] $postData |
88
|
|
|
* @param UploadedFile[] $fileData |
89
|
|
|
* |
90
|
|
|
* @return bool |
91
|
|
|
* @throws OrmException |
92
|
|
|
*/ |
93
|
|
|
public function update(IStringerEntity $entity, array $postData, array $fileData): bool |
94
|
|
|
{ |
95
|
|
|
assert($entity instanceof Entity, new \InvalidArgumentException()); |
96
|
|
|
|
97
|
|
|
$this->fillEntity($entity, $postData, $fileData); |
98
|
|
|
|
99
|
|
|
if (!empty($fileData)) { |
100
|
|
|
$this->deleteFile($entity); |
101
|
|
|
$this->uploadFile($entity, $fileData); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$this->commitUpdate($entity); |
105
|
|
|
|
106
|
|
|
return true; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param IStringerEntity $entity |
111
|
|
|
* |
112
|
|
|
* @return bool |
113
|
|
|
* @throws OrmException |
114
|
|
|
*/ |
115
|
|
|
public function delete(IStringerEntity $entity): bool |
116
|
|
|
{ |
117
|
|
|
assert($entity instanceof Entity, new \InvalidArgumentException()); |
118
|
|
|
|
119
|
|
|
$this->deleteFile($entity); |
120
|
|
|
|
121
|
|
|
$this->repo->delete($entity); |
122
|
|
|
|
123
|
|
|
$this->commitDelete($entity); |
124
|
|
|
|
125
|
|
|
return true; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param IStringerEntity $entity |
130
|
|
|
*/ |
131
|
|
|
public function deleteFile(IStringerEntity $entity) |
132
|
|
|
{ |
133
|
|
|
assert($entity instanceof Entity, new \InvalidArgumentException()); |
134
|
|
|
|
135
|
|
|
$this->uploader->delete($entity->getOldFilesystemName()); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param IStringerEntity $entity |
140
|
|
|
* @param UploadedFile[] $fileData |
141
|
|
|
*/ |
142
|
|
|
public function uploadFile(IStringerEntity $entity, array $fileData) |
143
|
|
|
{ |
144
|
|
|
assert($entity instanceof Entity, new \InvalidArgumentException()); |
145
|
|
|
|
146
|
|
|
$paths = $this->uploader->persist($fileData); |
147
|
|
|
|
148
|
|
|
if (!$paths) { |
149
|
|
|
return; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$entity->setFilesystemName($paths[static::INPUT_NAME_FILE]); |
153
|
|
|
$entity->setPublicName($fileData[static::INPUT_NAME_FILE]->getTempFilename()); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param string $entityId |
158
|
|
|
* |
159
|
|
|
* @return Entity |
160
|
|
|
*/ |
161
|
|
|
public function createEntity(string $entityId): IStringerEntity |
162
|
|
|
{ |
163
|
|
|
$fileCategory = new FileCategory('', '', '', false, []); |
164
|
|
|
|
165
|
|
|
return new Entity($entityId, '', '', '', '', $fileCategory, null); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
170
|
|
|
* |
171
|
|
|
* @param IStringerEntity $entity |
172
|
|
|
* @param array $postData |
173
|
|
|
* @param UploadedFile[] $fileData |
174
|
|
|
* |
175
|
|
|
* @return Entity |
176
|
|
|
* @throws OrmException |
177
|
|
|
*/ |
178
|
|
|
protected function fillEntity(IStringerEntity $entity, array $postData, array $fileData): IStringerEntity |
179
|
|
|
{ |
180
|
|
|
assert($entity instanceof Entity, new \InvalidArgumentException()); |
181
|
|
|
|
182
|
|
|
$categoryId = $postData['category_id']; |
183
|
|
|
$description = $postData['description']; |
184
|
|
|
$filesystemName = $postData['filesystem_name']; |
185
|
|
|
$publicName = $postData['public_name']; |
186
|
|
|
$mime = $postData['mime']; |
187
|
|
|
|
188
|
|
|
/** @var FileCategory $fileCategory */ |
189
|
|
|
$fileCategory = $this->fileCategoryRepo->getById($categoryId); |
190
|
|
|
|
191
|
|
|
$entity |
192
|
|
|
->setDescription($description) |
193
|
|
|
->setCategory($fileCategory) |
194
|
|
|
->setFilesystemName($filesystemName) |
195
|
|
|
->setPublicName($publicName) |
196
|
|
|
->setMime($mime); |
197
|
|
|
|
198
|
|
|
return $entity; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|