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,22 +96,25 @@ 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 {
130110 $ file = $ this ->file ;
131111 }
132112
133113 if (null === $ file ) {
134- return null ;
114+ $ document = $ this ->createDocument ();
115+ $ this ->persistDocument ($ document );
116+
117+ return $ document ;
135118 }
136119
137120 if ($ file instanceof UploadedFile && !$ file ->isValid ()) {
@@ -146,10 +129,10 @@ public function getDocument(bool $allowEmpty = false, bool $allowDuplicates = fa
146129 if (false !== $ fileHash && !$ allowDuplicates ) {
147130 $ existingDocument = $ this ->documentFinder ->findOneByHashAndAlgorithm ($ fileHash , $ this ->getHashAlgorithm ());
148131 if (null !== $ existingDocument ) {
149- if (
150- $ existingDocument -> isRaw () &&
151- null !== $ existingDownscaledDocument = $ existingDocument -> getDownscaledDocument ()
152- ) {
132+ /*
133+ * If existing document is a RAW, serve its downscaled version
134+ */
135+ if ( null !== $ existingDownscaledDocument = $ existingDocument -> getDownscaledDocument () ) {
153136 $ existingDocument = $ existingDownscaledDocument ;
154137 }
155138 if (null !== $ this ->folder ) {
@@ -159,7 +142,11 @@ public function getDocument(bool $allowEmpty = false, bool $allowDuplicates = fa
159142 $ this ->logger ->info (sprintf (
160143 'File %s already exists with same checksum, do not upload it twice. ' ,
161144 $ existingDocument ->getFilename ()
162- ));
145+ ), [
146+ 'path ' => $ existingDocument ->getMountPath (),
147+ ]);
148+ (new Filesystem ())->remove ($ file ->getPathname ());
149+
163150 return $ existingDocument ;
164151 }
165152 }
@@ -175,8 +162,8 @@ public function getDocument(bool $allowEmpty = false, bool $allowDuplicates = fa
175162 $ this ->parseSvgMimeType ($ document );
176163
177164 if (
178- $ document instanceof FileHashInterface &&
179- false !== $ fileHash
165+ $ document instanceof FileHashInterface
166+ && false !== $ fileHash
180167 ) {
181168 $ document ->setFileHash ($ fileHash );
182169 $ document ->setFileHashAlgorithm ($ this ->getHashAlgorithm ());
@@ -196,8 +183,6 @@ public function getDocument(bool $allowEmpty = false, bool $allowDuplicates = fa
196183 /**
197184 * Updates a document from UploadedFile, Be careful, this method does not flush.
198185 *
199- * @param DocumentInterface $document
200- * @return DocumentInterface
201186 * @throws FilesystemException
202187 */
203188 public function updateDocument (DocumentInterface $ document ): DocumentInterface
@@ -231,7 +216,7 @@ public function updateDocument(DocumentInterface $document): DocumentInterface
231216 }
232217 }
233218
234- $ document ->setFolder (\mb_substr ( hash ( " crc32b " , date ( ' YmdHi ' )), 0 , 12 ));
219+ $ document ->setFolder (DocumentFolderGenerator:: generateFolderName ( ));
235220 }
236221
237222 $ document ->setFilename ($ this ->getFileName ());
@@ -247,9 +232,6 @@ public function updateDocument(DocumentInterface $document): DocumentInterface
247232 }
248233
249234 /**
250- * @param File $localFile
251- * @param DocumentInterface $document
252- * @return void
253235 * @throws FilesystemException
254236 */
255237 public function moveFile (File $ localFile , DocumentInterface $ document ): void
@@ -267,9 +249,6 @@ public function moveFile(File $localFile, DocumentInterface $document): void
267249 }
268250 }
269251
270- /**
271- * @return string
272- */
273252 protected function getFileName (): string
274253 {
275254 $ file = $ this ->getFile ();
@@ -278,8 +257,8 @@ protected function getFileName(): string
278257 $ fileName = $ file ->getClientOriginalName ();
279258 } elseif (
280259 $ file instanceof DownloadedFile
281- && $ file ->getOriginalFilename () !== null
282- && $ file ->getOriginalFilename () !== ''
260+ && null !== $ file ->getOriginalFilename ()
261+ && '' !== $ file ->getOriginalFilename ()
283262 ) {
284263 $ fileName = $ file ->getOriginalFilename ();
285264 } else {
@@ -292,9 +271,6 @@ protected function getFileName(): string
292271 /**
293272 * Create a Document from an external URL.
294273 *
295- * @param string $downloadUrl
296- *
297- * @return DocumentInterface|null
298274 * @throws FilesystemException
299275 */
300276 public function getDocumentFromUrl (string $ downloadUrl ): ?DocumentInterface
@@ -303,6 +279,7 @@ public function getDocumentFromUrl(string $downloadUrl): ?DocumentInterface
303279 if (null !== $ downloadedFile ) {
304280 return $ this ->setFile ($ downloadedFile )->getDocument ();
305281 }
282+
306283 return null ;
307284 }
308285}
0 commit comments