88use League \Flysystem \FilesystemOperator ;
99use League \Flysystem \MountManager ;
1010use Psr \Log \LoggerInterface ;
11- use Psr \Log \NullLogger ;
1211use RZ \Roadiz \Documents \Models \DocumentInterface ;
1312use RZ \Roadiz \Documents \Models \FileHashInterface ;
1413use RZ \Roadiz \Documents \Models \FolderInterface ;
2322 */
2423abstract class AbstractDocumentFactory
2524{
26- private LoggerInterface $ logger ;
2725 private ?File $ file = null ;
2826 private ?FolderInterface $ folder = null ;
29- private FilesystemOperator $ documentsStorage ;
30- private DocumentFinderInterface $ documentFinder ;
3127
3228 public function __construct (
33- FilesystemOperator $ documentsStorage ,
34- DocumentFinderInterface $ documentFinder ,
35- ? LoggerInterface $ logger = null
29+ protected readonly FilesystemOperator $ documentsStorage ,
30+ protected readonly DocumentFinderInterface $ documentFinder ,
31+ protected readonly LoggerInterface $ logger,
3632 ) {
3733 if (!$ documentsStorage instanceof MountManager) {
3834 trigger_error ('Document Storage must be a MountManager to address public and private files. ' , E_USER_WARNING );
3935 }
40- $ this ->documentsStorage = $ documentsStorage ;
41- $ this ->documentFinder = $ documentFinder ;
42- $ this ->logger = $ logger ?? new NullLogger ();
4336 }
4437
45- /**
46- * @return File
47- */
4838 public function getFile (): File
4939 {
5040 if (null === $ this ->file ) {
5141 throw new \BadMethodCallException ('File should be defined before using it. ' );
5242 }
43+
5344 return $ this ->file ;
5445 }
5546
5647 /**
57- * @param File $file
5848 * @return $this
5949 */
6050 public function setFile (File $ file ): static
6151 {
6252 $ this ->file = $ file ;
53+
6354 return $ this ;
6455 }
6556
66- /**
67- * @return FolderInterface|null
68- */
6957 public function getFolder (): ?FolderInterface
7058 {
7159 return $ this ->folder ;
7260 }
7361
7462 /**
75- * @param FolderInterface|null $folder
7663 * @return $this
7764 */
7865 public function setFolder (?FolderInterface $ folder = null ): static
7966 {
8067 $ this ->folder = $ folder ;
68+
8169 return $ this ;
8270 }
8371
8472 /**
8573 * Special case for SVG without XML statement.
86- *
87- * @param DocumentInterface $document
8874 */
8975 protected function parseSvgMimeType (DocumentInterface $ document ): void
9076 {
9177 if (
92- ($ document ->getMimeType () === 'text/plain ' || $ document ->getMimeType () === ' text/html ' ) &&
93- preg_match ('#\.svg$# ' , $ document ->getFilename ())
78+ (' text/plain ' === $ document ->getMimeType () || 'text/html ' === $ document ->getMimeType ())
79+ && preg_match ('#\.svg$# ' , $ document ->getFilename ())
9480 ) {
9581 $ this ->logger ->debug ('Uploaded a SVG without xml declaration. Presuming it’s a valid SVG file. ' );
9682 $ document ->setMimeType ('image/svg+xml ' );
9783 }
9884 }
9985
100- /**
101- * @return DocumentInterface
102- */
10386 abstract protected function createDocument (): DocumentInterface ;
10487
105- /**
106- * @param DocumentInterface $document
107- */
10888 abstract protected function persistDocument (DocumentInterface $ document ): void ;
10989
11090 protected function getHashAlgorithm (): string
@@ -116,14 +96,14 @@ protected function getHashAlgorithm(): string
11696 * Create a document from UploadedFile, Be careful, this method does not flush, only
11797 * persists current Document.
11898 *
119- * @param bool $allowEmpty Default false, requires a local file to create new document entity
99+ * @param bool $allowEmpty Default false, requires a local file to create new document entity
120100 * @param bool $allowDuplicates Default false, always import new document even if file already exists
121- * @return null|DocumentInterface
101+ *
122102 * @throws FilesystemException
123103 */
124104 public function getDocument (bool $ allowEmpty = false , bool $ allowDuplicates = false ): ?DocumentInterface
125105 {
126- if ($ allowEmpty === false ) {
106+ if (false === $ allowEmpty ) {
127107 // Getter throw exception on null file
128108 $ file = $ this ->getFile ();
129109 } else {
@@ -146,10 +126,10 @@ public function getDocument(bool $allowEmpty = false, bool $allowDuplicates = fa
146126 if (false !== $ fileHash && !$ allowDuplicates ) {
147127 $ existingDocument = $ this ->documentFinder ->findOneByHashAndAlgorithm ($ fileHash , $ this ->getHashAlgorithm ());
148128 if (null !== $ existingDocument ) {
149- if (
150- $ existingDocument -> isRaw () &&
151- null !== $ existingDownscaledDocument = $ existingDocument -> getDownscaledDocument ()
152- ) {
129+ /*
130+ * If existing document is a RAW, serve its downscaled version
131+ */
132+ if ( null !== $ existingDownscaledDocument = $ existingDocument -> getDownscaledDocument () ) {
153133 $ existingDocument = $ existingDownscaledDocument ;
154134 }
155135 if (null !== $ this ->folder ) {
@@ -159,7 +139,11 @@ public function getDocument(bool $allowEmpty = false, bool $allowDuplicates = fa
159139 $ this ->logger ->info (sprintf (
160140 'File %s already exists with same checksum, do not upload it twice. ' ,
161141 $ existingDocument ->getFilename ()
162- ));
142+ ), [
143+ 'path ' => $ existingDocument ->getMountPath (),
144+ ]);
145+ (new Filesystem ())->remove ($ file ->getPathname ());
146+
163147 return $ existingDocument ;
164148 }
165149 }
@@ -175,8 +159,8 @@ public function getDocument(bool $allowEmpty = false, bool $allowDuplicates = fa
175159 $ this ->parseSvgMimeType ($ document );
176160
177161 if (
178- $ document instanceof FileHashInterface &&
179- false !== $ fileHash
162+ $ document instanceof FileHashInterface
163+ && false !== $ fileHash
180164 ) {
181165 $ document ->setFileHash ($ fileHash );
182166 $ document ->setFileHashAlgorithm ($ this ->getHashAlgorithm ());
@@ -196,8 +180,6 @@ public function getDocument(bool $allowEmpty = false, bool $allowDuplicates = fa
196180 /**
197181 * Updates a document from UploadedFile, Be careful, this method does not flush.
198182 *
199- * @param DocumentInterface $document
200- * @return DocumentInterface
201183 * @throws FilesystemException
202184 */
203185 public function updateDocument (DocumentInterface $ document ): DocumentInterface
@@ -231,7 +213,7 @@ public function updateDocument(DocumentInterface $document): DocumentInterface
231213 }
232214 }
233215
234- $ document ->setFolder (\mb_substr ( hash ( " crc32b " , date ( ' YmdHi ' )), 0 , 12 ));
216+ $ document ->setFolder (DocumentFolderGenerator:: generateFolderName ( ));
235217 }
236218
237219 $ document ->setFilename ($ this ->getFileName ());
@@ -247,9 +229,6 @@ public function updateDocument(DocumentInterface $document): DocumentInterface
247229 }
248230
249231 /**
250- * @param File $localFile
251- * @param DocumentInterface $document
252- * @return void
253232 * @throws FilesystemException
254233 */
255234 public function moveFile (File $ localFile , DocumentInterface $ document ): void
@@ -267,9 +246,6 @@ public function moveFile(File $localFile, DocumentInterface $document): void
267246 }
268247 }
269248
270- /**
271- * @return string
272- */
273249 protected function getFileName (): string
274250 {
275251 $ file = $ this ->getFile ();
@@ -278,8 +254,8 @@ protected function getFileName(): string
278254 $ fileName = $ file ->getClientOriginalName ();
279255 } elseif (
280256 $ file instanceof DownloadedFile
281- && $ file ->getOriginalFilename () !== null
282- && $ file ->getOriginalFilename () !== ''
257+ && null !== $ file ->getOriginalFilename ()
258+ && '' !== $ file ->getOriginalFilename ()
283259 ) {
284260 $ fileName = $ file ->getOriginalFilename ();
285261 } else {
@@ -292,9 +268,6 @@ protected function getFileName(): string
292268 /**
293269 * Create a Document from an external URL.
294270 *
295- * @param string $downloadUrl
296- *
297- * @return DocumentInterface|null
298271 * @throws FilesystemException
299272 */
300273 public function getDocumentFromUrl (string $ downloadUrl ): ?DocumentInterface
@@ -303,6 +276,7 @@ public function getDocumentFromUrl(string $downloadUrl): ?DocumentInterface
303276 if (null !== $ downloadedFile ) {
304277 return $ this ->setFile ($ downloadedFile )->getDocument ();
305278 }
279+
306280 return null ;
307281 }
308282}
0 commit comments