Skip to content

Commit 4e8357e

Browse files
author
roadiz-ci
committed
chore: bumped
1 parent 5657627 commit 4e8357e

97 files changed

Lines changed: 1552 additions & 2322 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/run-test.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
runs-on: ubuntu-latest
2020
strategy:
2121
matrix:
22-
php-version: ['8.1', '8.2', '8.3']
22+
php-version: ['8.2', '8.3']
2323
steps:
2424
- uses: shivammathur/setup-php@v2
2525
with:
@@ -37,7 +37,5 @@ jobs:
3737
run: composer install --no-scripts --no-ansi --no-interaction --no-progress
3838
- name: Run Unit tests
3939
run: vendor/bin/phpunit -v --whitelist ./src tests
40-
- name: Run PHP Code Sniffer
41-
run: vendor/bin/phpcs -p ./src
4240
- name: Run PHPStan
4341
run: vendor/bin/phpstan analyse --no-progress -c phpstan.neon

composer.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,15 @@
1818
}
1919
],
2020
"require": {
21-
"php": ">=8.1",
21+
"php": ">=8.2",
2222
"ext-json": "*",
2323
"ext-gd": "*",
2424
"ext-dom": "*",
2525
"ext-zip": "*",
2626
"ext-simplexml": "*",
2727
"ext-fileinfo": "*",
28-
"doctrine/orm": "~2.19.0",
28+
"doctrine/orm": "~2.20.0",
2929
"enshrined/svg-sanitize": "^0.22",
30-
"guzzlehttp/guzzle": "^7.2.0",
31-
"guzzlehttp/psr7": "^2.0",
3230
"intervention/image": "^2.5",
3331
"league/flysystem": "^3.0",
3432
"monolog/monolog": "^1.24.0 || ^2.1.1",
@@ -42,15 +40,17 @@
4240
"symfony/http-foundation": "6.4.*",
4341
"symfony/options-resolver": "6.4.*",
4442
"symfony/serializer": "6.4.*",
45-
"twig/twig": "^3.1"
43+
"twig/twig": "^3.16"
4644
},
4745
"require-dev": {
46+
"api-platform/metadata": "~3.3.11",
47+
"doctrine/doctrine-bundle": "^2.8.1",
4848
"php-coveralls/php-coveralls": "^2.4",
49-
"phpunit/phpunit": "^9.5",
50-
"api-platform/metadata": "^3.2.12",
51-
"squizlabs/php_codesniffer": "^3.5",
5249
"phpstan/phpstan": "^1.5.3",
53-
"phpstan/phpstan-doctrine": "^1.3"
50+
"phpstan/phpdoc-parser": "<2",
51+
"phpstan/phpstan-doctrine": "^1.3",
52+
"phpunit/phpunit": "^9.5",
53+
"symfony/process": "6.4.*"
5454
},
5555
"autoload": {
5656
"psr-4": {
@@ -70,8 +70,8 @@
7070
},
7171
"extra": {
7272
"branch-alias": {
73-
"dev-master": "2.3.x-dev",
74-
"dev-develop": "2.4.x-dev"
73+
"dev-master": "2.5.x-dev",
74+
"dev-develop": "2.6.x-dev"
7575
}
7676
}
7777
}

phpcs.xml.dist

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/AbstractDocumentFactory.php

Lines changed: 26 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use League\Flysystem\FilesystemOperator;
99
use League\Flysystem\MountManager;
1010
use Psr\Log\LoggerInterface;
11-
use Psr\Log\NullLogger;
1211
use RZ\Roadiz\Documents\Models\DocumentInterface;
1312
use RZ\Roadiz\Documents\Models\FileHashInterface;
1413
use RZ\Roadiz\Documents\Models\FolderInterface;
@@ -23,88 +22,69 @@
2322
*/
2423
abstract 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
}

src/AbstractDocumentFinder.php

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,40 @@
66

77
abstract class AbstractDocumentFinder implements DocumentFinderInterface
88
{
9-
/**
10-
* @inheritDoc
11-
*/
129
public function findVideosWithFilename(string $fileName): iterable
1310
{
1411
$basename = pathinfo($fileName);
1512
$basename = $basename['filename'];
1613

1714
$sourcesDocsName = [
18-
$basename . '.ogg',
19-
$basename . '.ogv',
20-
$basename . '.mp4',
21-
$basename . '.mov',
22-
$basename . '.avi',
23-
$basename . '.webm',
24-
$basename . '.mkv',
15+
$basename.'.ogg',
16+
$basename.'.ogv',
17+
$basename.'.mp4',
18+
$basename.'.mov',
19+
$basename.'.avi',
20+
$basename.'.webm',
21+
$basename.'.mkv',
2522
];
2623

2724
return $this->findAllByFilenames($sourcesDocsName);
2825
}
2926

30-
/**
31-
* @inheritDoc
32-
*/
3327
public function findAudiosWithFilename(string $fileName): iterable
3428
{
3529
$basename = pathinfo($fileName);
3630
$basename = $basename['filename'];
3731

3832
$sourcesDocsName = [
39-
$basename . '.mp3',
40-
$basename . '.ogg',
41-
$basename . '.wav',
42-
$basename . '.m4a',
43-
$basename . '.aac',
33+
$basename.'.mp3',
34+
$basename.'.ogg',
35+
$basename.'.wav',
36+
$basename.'.m4a',
37+
$basename.'.aac',
4438
];
4539

4640
return $this->findAllByFilenames($sourcesDocsName);
4741
}
4842

49-
/**
50-
* @inheritDoc
51-
*/
5243
public function findPicturesWithFilename(string $fileName): iterable
5344
{
5445
$pathInfo = pathinfo($fileName);
@@ -70,7 +61,7 @@ public function findPicturesWithFilename(string $fileName): iterable
7061
$extensionsList = array_diff($extensionsList, [$currentExtension]);
7162
// list sources paths for extensions
7263
$sourcesDocsName = array_values(array_map(function ($extension) use ($basename) {
73-
return $basename . '.' . $extension;
64+
return $basename.'.'.$extension;
7465
}, $extensionsList));
7566

7667
return $this->findAllByFilenames($sourcesDocsName);

0 commit comments

Comments
 (0)