Skip to content

Commit 8b510a1

Browse files
author
roadiz-ci
committed
chore: bumped
1 parent 65c4e04 commit 8b510a1

92 files changed

Lines changed: 2240 additions & 1527 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: 3 additions & 1 deletion
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.2', '8.3']
22+
php-version: ['8.1', '8.2', '8.3']
2323
steps:
2424
- uses: shivammathur/setup-php@v2
2525
with:
@@ -37,5 +37,7 @@ 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
4042
- name: Run PHPStan
4143
run: vendor/bin/phpstan analyse --no-progress -c phpstan.neon

composer.json

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@
1818
}
1919
],
2020
"require": {
21-
"php": ">=8.2",
21+
"php": ">=8.1",
2222
"ext-json": "*",
2323
"ext-gd": "*",
2424
"ext-dom": "*",
2525
"ext-zip": "*",
2626
"ext-simplexml": "*",
2727
"ext-fileinfo": "*",
28-
"doctrine/orm": "~2.20.0",
28+
"doctrine/orm": "~2.19.0",
2929
"enshrined/svg-sanitize": "^0.22",
30+
"guzzlehttp/guzzle": "^7.2.0",
31+
"guzzlehttp/psr7": "^2.0",
3032
"intervention/image": "^2.5",
3133
"league/flysystem": "^3.0",
3234
"monolog/monolog": "^1.24.0 || ^2.1.1",
@@ -36,21 +38,17 @@
3638
"symfony/filesystem": "6.4.*",
3739
"symfony/finder": "6.4.*",
3840
"symfony/http-foundation": "6.4.*",
39-
"symfony/http-client-contracts": "^3.5",
4041
"symfony/options-resolver": "6.4.*",
4142
"symfony/serializer": "6.4.*",
42-
"twig/twig": "^3.16"
43+
"twig/twig": "^3.1"
4344
},
4445
"require-dev": {
45-
"api-platform/metadata": "~3.3.11",
46-
"doctrine/doctrine-bundle": "^2.8.1",
4746
"php-coveralls/php-coveralls": "^2.4",
48-
"phpstan/phpstan": "^1.5.3",
49-
"phpstan/phpdoc-parser": "<2",
50-
"phpstan/phpstan-doctrine": "^1.3",
5147
"phpunit/phpunit": "^9.5",
52-
"symfony/http-client": "6.4.*",
53-
"symfony/process": "6.4.*"
48+
"api-platform/metadata": "^3.2.12",
49+
"squizlabs/php_codesniffer": "^3.5",
50+
"phpstan/phpstan": "^1.5.3",
51+
"phpstan/phpstan-doctrine": "^1.3"
5452
},
5553
"autoload": {
5654
"psr-4": {
@@ -70,8 +68,8 @@
7068
},
7169
"extra": {
7270
"branch-alias": {
73-
"dev-master": "2.5.x-dev",
74-
"dev-develop": "2.6.x-dev"
71+
"dev-master": "2.3.x-dev",
72+
"dev-develop": "2.4.x-dev"
7573
}
7674
}
7775
}

phpcs.xml.dist

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd">
5+
<arg name="basepath" value="."/>
6+
<arg name="cache" value=".phpcs-cache"/>
7+
<arg name="colors"/>
8+
<arg name="extensions" value="php"/>
9+
<rule ref="PSR12">
10+
<exclude name="Generic.Files.LineLength"/>
11+
</rule>
12+
<file>src/</file>
13+
</ruleset>

src/AbstractDocumentFactory.php

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

src/AbstractDocumentFinder.php

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

77
abstract class AbstractDocumentFinder implements DocumentFinderInterface
88
{
9+
/**
10+
* @inheritDoc
11+
*/
912
public function findVideosWithFilename(string $fileName): iterable
1013
{
1114
$basename = pathinfo($fileName);
1215
$basename = $basename['filename'];
1316

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

2427
return $this->findAllByFilenames($sourcesDocsName);
2528
}
2629

30+
/**
31+
* @inheritDoc
32+
*/
2733
public function findAudiosWithFilename(string $fileName): iterable
2834
{
2935
$basename = pathinfo($fileName);
3036
$basename = $basename['filename'];
3137

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

4046
return $this->findAllByFilenames($sourcesDocsName);
4147
}
4248

49+
/**
50+
* @inheritDoc
51+
*/
4352
public function findPicturesWithFilename(string $fileName): iterable
4453
{
4554
$pathInfo = pathinfo($fileName);
@@ -61,7 +70,7 @@ public function findPicturesWithFilename(string $fileName): iterable
6170
$extensionsList = array_diff($extensionsList, [$currentExtension]);
6271
// list sources paths for extensions
6372
$sourcesDocsName = array_values(array_map(function ($extension) use ($basename) {
64-
return $basename.'.'.$extension;
73+
return $basename . '.' . $extension;
6574
}, $extensionsList));
6675

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

0 commit comments

Comments
 (0)