Skip to content

Commit 7bba229

Browse files
author
roadiz-ci
committed
chore: Bumped
1 parent 7a0b744 commit 7bba229

104 files changed

Lines changed: 3035 additions & 2703 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.3', '8.4']
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

LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright © 2025 Ambroise Maupate
3+
Copyright © 2024 Ambroise Maupate
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
66

composer.json

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,37 @@
1818
}
1919
],
2020
"require": {
21-
"php": ">=8.3",
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.15",
30-
"intervention/image": "^3.11",
30+
"guzzlehttp/guzzle": "^7.2.0",
31+
"guzzlehttp/psr7": "^2.0",
32+
"intervention/image": "^2.5",
3133
"league/flysystem": "^3.0",
32-
"monolog/monolog": "^3.9",
33-
"symfony/asset": "7.3.*",
34-
"symfony/console": "7.3.*",
35-
"symfony/event-dispatcher": "7.3.*",
36-
"symfony/filesystem": ">=7.1",
37-
"symfony/finder": "7.3.*",
38-
"symfony/http-foundation": "7.3.*",
39-
"symfony/http-client-contracts": "^3.5",
40-
"symfony/options-resolver": "7.3.*",
41-
"symfony/serializer": "7.3.*",
42-
"twig/twig": "^3.21"
34+
"monolog/monolog": "^1.24.0 || ^2.1.1",
35+
"symfony/asset": "6.4.*",
36+
"symfony/console": "6.4.*",
37+
"symfony/event-dispatcher": "6.4.*",
38+
"symfony/filesystem": "6.4.*",
39+
"symfony/finder": "6.4.*",
40+
"symfony/http-foundation": "6.4.*",
41+
"symfony/options-resolver": "6.4.*",
42+
"symfony/serializer": "6.4.*",
43+
"twig/twig": "^3.1"
4344
},
4445
"require-dev": {
45-
"api-platform/metadata": "^4.1.18",
46-
"doctrine/doctrine-bundle": "^2.8.1",
4746
"php-coveralls/php-coveralls": "^2.4",
47+
"phpunit/phpunit": "^9.5",
48+
"api-platform/metadata": "^3.2.12",
49+
"squizlabs/php_codesniffer": "^3.5",
4850
"phpstan/phpstan": "^1.5.3",
49-
"phpstan/phpdoc-parser": "<2",
50-
"phpstan/phpstan-doctrine": "^1.3",
51-
"phpunit/phpunit": "^9.6",
52-
"symfony/http-client": "7.3.*",
53-
"symfony/process": "7.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.6.x-dev",
74-
"dev-develop": "2.7.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
}

0 commit comments

Comments
 (0)