88use League \Flysystem \FilesystemOperator ;
99use League \Flysystem \MountManager ;
1010use Psr \Log \LoggerInterface ;
11+ use Psr \Log \NullLogger ;
1112use RZ \Roadiz \Documents \Models \DocumentInterface ;
1213use RZ \Roadiz \Documents \Models \FileHashInterface ;
1314use RZ \Roadiz \Documents \Models \FolderInterface ;
2223 */
2324abstract class AbstractDocumentFactory
2425{
26+ private LoggerInterface $ logger ;
2527 private ?File $ file = null ;
2628 private ?FolderInterface $ folder = null ;
29+ private FilesystemOperator $ documentsStorage ;
30+ private DocumentFinderInterface $ documentFinder ;
2731
2832 public function __construct (
29- protected readonly FilesystemOperator $ documentsStorage ,
30- protected readonly DocumentFinderInterface $ documentFinder ,
31- protected readonly LoggerInterface $ logger,
33+ FilesystemOperator $ documentsStorage ,
34+ DocumentFinderInterface $ documentFinder ,
35+ ? LoggerInterface $ logger = null
3236 ) {
3337 if (!$ documentsStorage instanceof MountManager) {
3438 trigger_error ('Document Storage must be a MountManager to address public and private files. ' , E_USER_WARNING );
3539 }
40+ $ this ->documentsStorage = $ documentsStorage ;
41+ $ this ->documentFinder = $ documentFinder ;
42+ $ this ->logger = $ logger ?? new NullLogger ();
3643 }
3744
45+ /**
46+ * @return File
47+ */
3848 public function getFile (): File
3949 {
4050 if (null === $ this ->file ) {
4151 throw new \BadMethodCallException ('File should be defined before using it. ' );
4252 }
43-
4453 return $ this ->file ;
4554 }
4655
4756 /**
57+ * @param File $file
4858 * @return $this
4959 */
5060 public function setFile (File $ file ): static
5161 {
5262 $ this ->file = $ file ;
53-
5463 return $ this ;
5564 }
5665
66+ /**
67+ * @return FolderInterface|null
68+ */
5769 public function getFolder (): ?FolderInterface
5870 {
5971 return $ this ->folder ;
6072 }
6173
6274 /**
75+ * @param FolderInterface|null $folder
6376 * @return $this
6477 */
6578 public function setFolder (?FolderInterface $ folder = null ): static
6679 {
6780 $ this ->folder = $ folder ;
68-
6981 return $ this ;
7082 }
7183
7284 /**
7385 * Special case for SVG without XML statement.
86+ *
87+ * @param DocumentInterface $document
7488 */
7589 protected function parseSvgMimeType (DocumentInterface $ document ): void
7690 {
7791 if (
78- (' text/plain ' === $ document ->getMimeType () || 'text/html ' === $ document ->getMimeType ())
79- && preg_match ('#\.svg$# ' , $ document ->getFilename ())
92+ ($ document ->getMimeType () === 'text/plain ' || $ document ->getMimeType () === ' text/html ' ) &&
93+ preg_match ('#\.svg$# ' , $ document ->getFilename ())
8094 ) {
8195 $ this ->logger ->debug ('Uploaded a SVG without xml declaration. Presuming it’s a valid SVG file. ' );
8296 $ document ->setMimeType ('image/svg+xml ' );
8397 }
8498 }
8599
100+ /**
101+ * @return DocumentInterface
102+ */
86103 abstract protected function createDocument (): DocumentInterface ;
87104
105+ /**
106+ * @param DocumentInterface $document
107+ */
88108 abstract protected function persistDocument (DocumentInterface $ document ): void ;
89109
90110 protected function getHashAlgorithm (): string
@@ -96,14 +116,14 @@ protected function getHashAlgorithm(): string
96116 * Create a document from UploadedFile, Be careful, this method does not flush, only
97117 * persists current Document.
98118 *
99- * @param bool $allowEmpty Default false, requires a local file to create new document entity
119+ * @param bool $allowEmpty Default false, requires a local file to create new document entity
100120 * @param bool $allowDuplicates Default false, always import new document even if file already exists
101- *
121+ * @return null|DocumentInterface
102122 * @throws FilesystemException
103123 */
104124 public function getDocument (bool $ allowEmpty = false , bool $ allowDuplicates = false ): ?DocumentInterface
105125 {
106- if (false === $ allowEmpty ) {
126+ if ($ allowEmpty === false ) {
107127 // Getter throw exception on null file
108128 $ file = $ this ->getFile ();
109129 } else {
@@ -126,10 +146,10 @@ public function getDocument(bool $allowEmpty = false, bool $allowDuplicates = fa
126146 if (false !== $ fileHash && !$ allowDuplicates ) {
127147 $ existingDocument = $ this ->documentFinder ->findOneByHashAndAlgorithm ($ fileHash , $ this ->getHashAlgorithm ());
128148 if (null !== $ existingDocument ) {
129- /*
130- * If existing document is a RAW, serve its downscaled version
131- */
132- if ( null !== $ existingDownscaledDocument = $ existingDocument -> getDownscaledDocument () ) {
149+ if (
150+ $ existingDocument -> isRaw () &&
151+ null !== $ existingDownscaledDocument = $ existingDocument -> getDownscaledDocument ()
152+ ) {
133153 $ existingDocument = $ existingDownscaledDocument ;
134154 }
135155 if (null !== $ this ->folder ) {
@@ -139,11 +159,7 @@ public function getDocument(bool $allowEmpty = false, bool $allowDuplicates = fa
139159 $ this ->logger ->info (sprintf (
140160 'File %s already exists with same checksum, do not upload it twice. ' ,
141161 $ existingDocument ->getFilename ()
142- ), [
143- 'path ' => $ existingDocument ->getMountPath (),
144- ]);
145- (new Filesystem ())->remove ($ file ->getPathname ());
146-
162+ ));
147163 return $ existingDocument ;
148164 }
149165 }
@@ -159,8 +175,8 @@ public function getDocument(bool $allowEmpty = false, bool $allowDuplicates = fa
159175 $ this ->parseSvgMimeType ($ document );
160176
161177 if (
162- $ document instanceof FileHashInterface
163- && false !== $ fileHash
178+ $ document instanceof FileHashInterface &&
179+ false !== $ fileHash
164180 ) {
165181 $ document ->setFileHash ($ fileHash );
166182 $ document ->setFileHashAlgorithm ($ this ->getHashAlgorithm ());
@@ -180,6 +196,8 @@ public function getDocument(bool $allowEmpty = false, bool $allowDuplicates = fa
180196 /**
181197 * Updates a document from UploadedFile, Be careful, this method does not flush.
182198 *
199+ * @param DocumentInterface $document
200+ * @return DocumentInterface
183201 * @throws FilesystemException
184202 */
185203 public function updateDocument (DocumentInterface $ document ): DocumentInterface
@@ -213,7 +231,7 @@ public function updateDocument(DocumentInterface $document): DocumentInterface
213231 }
214232 }
215233
216- $ document ->setFolder (DocumentFolderGenerator:: generateFolderName ( ));
234+ $ document ->setFolder (\mb_substr ( hash ( " crc32b " , date ( ' YmdHi ' )), 0 , 12 ));
217235 }
218236
219237 $ document ->setFilename ($ this ->getFileName ());
@@ -229,6 +247,9 @@ public function updateDocument(DocumentInterface $document): DocumentInterface
229247 }
230248
231249 /**
250+ * @param File $localFile
251+ * @param DocumentInterface $document
252+ * @return void
232253 * @throws FilesystemException
233254 */
234255 public function moveFile (File $ localFile , DocumentInterface $ document ): void
@@ -246,6 +267,9 @@ public function moveFile(File $localFile, DocumentInterface $document): void
246267 }
247268 }
248269
270+ /**
271+ * @return string
272+ */
249273 protected function getFileName (): string
250274 {
251275 $ file = $ this ->getFile ();
@@ -254,8 +278,8 @@ protected function getFileName(): string
254278 $ fileName = $ file ->getClientOriginalName ();
255279 } elseif (
256280 $ file instanceof DownloadedFile
257- && null !== $ file ->getOriginalFilename ()
258- && '' !== $ file ->getOriginalFilename ()
281+ && $ file ->getOriginalFilename () !== null
282+ && $ file ->getOriginalFilename () !== ''
259283 ) {
260284 $ fileName = $ file ->getOriginalFilename ();
261285 } else {
@@ -268,6 +292,9 @@ protected function getFileName(): string
268292 /**
269293 * Create a Document from an external URL.
270294 *
295+ * @param string $downloadUrl
296+ *
297+ * @return DocumentInterface|null
271298 * @throws FilesystemException
272299 */
273300 public function getDocumentFromUrl (string $ downloadUrl ): ?DocumentInterface
@@ -276,7 +303,6 @@ public function getDocumentFromUrl(string $downloadUrl): ?DocumentInterface
276303 if (null !== $ downloadedFile ) {
277304 return $ this ->setFile ($ downloadedFile )->getDocument ();
278305 }
279-
280306 return null ;
281307 }
282308}
0 commit comments