Skip to content

Commit

Permalink
Merge branch release/v2.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
roadiz-ci committed Dec 6, 2024
1 parent a540b71 commit 7e581d6
Show file tree
Hide file tree
Showing 88 changed files with 1,061 additions and 1,864 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/run-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php-version: ['8.1', '8.2', '8.3']
php-version: ['8.2', '8.3']
steps:
- uses: shivammathur/setup-php@v2
with:
Expand All @@ -37,7 +37,5 @@ jobs:
run: composer install --no-scripts --no-ansi --no-interaction --no-progress
- name: Run Unit tests
run: vendor/bin/phpunit -v --whitelist ./src tests
- name: Run PHP Code Sniffer
run: vendor/bin/phpcs -p ./src
- name: Run PHPStan
run: vendor/bin/phpstan analyse --no-progress -c phpstan.neon
21 changes: 10 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,15 @@
}
],
"require": {
"php": ">=8.1",
"php": ">=8.2",
"ext-json": "*",
"ext-gd": "*",
"ext-dom": "*",
"ext-zip": "*",
"ext-simplexml": "*",
"ext-fileinfo": "*",
"doctrine/orm": "~2.19.0",
"doctrine/orm": "~2.20.0",
"enshrined/svg-sanitize": "^0.15",
"guzzlehttp/guzzle": "^7.2.0",
"guzzlehttp/psr7": "^2.0",
"intervention/image": "^2.5",
"league/flysystem": "^3.0",
"monolog/monolog": "^1.24.0 || ^2.1.1",
Expand All @@ -38,17 +36,18 @@
"symfony/filesystem": "6.4.*",
"symfony/finder": "6.4.*",
"symfony/http-foundation": "6.4.*",
"symfony/http-client-contracts": "^3.5",
"symfony/options-resolver": "6.4.*",
"symfony/serializer": "6.4.*",
"twig/twig": "^3.1"
"twig/twig": "^3.16"
},
"require-dev": {
"api-platform/metadata": "~3.3.11",
"doctrine/doctrine-bundle": "^2.8.1",
"php-coveralls/php-coveralls": "^2.4",
"phpunit/phpunit": "^9.5",
"api-platform/metadata": "^3.2.12",
"squizlabs/php_codesniffer": "^3.5",
"phpstan/phpstan": "^1.5.3",
"phpstan/phpstan-doctrine": "^1.3"
"phpstan/phpstan-doctrine": "^1.3",
"phpunit/phpunit": "^9.5"
},
"autoload": {
"psr-4": {
Expand All @@ -68,8 +67,8 @@
},
"extra": {
"branch-alias": {
"dev-master": "2.3.x-dev",
"dev-develop": "2.4.x-dev"
"dev-master": "2.4.x-dev",
"dev-develop": "2.5.x-dev"
}
}
}
13 changes: 0 additions & 13 deletions phpcs.xml.dist

This file was deleted.

58 changes: 18 additions & 40 deletions src/AbstractDocumentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ abstract class AbstractDocumentFactory
public function __construct(
FilesystemOperator $documentsStorage,
DocumentFinderInterface $documentFinder,
?LoggerInterface $logger = null
?LoggerInterface $logger = null,
) {
if (!$documentsStorage instanceof MountManager) {
trigger_error('Document Storage must be a MountManager to address public and private files.', E_USER_WARNING);
Expand All @@ -42,69 +42,56 @@ public function __construct(
$this->logger = $logger ?? new NullLogger();
}

/**
* @return File
*/
public function getFile(): File
{
if (null === $this->file) {
throw new \BadMethodCallException('File should be defined before using it.');
}

return $this->file;
}

/**
* @param File $file
* @return $this
*/
public function setFile(File $file): static
{
$this->file = $file;

return $this;
}

/**
* @return FolderInterface|null
*/
public function getFolder(): ?FolderInterface
{
return $this->folder;
}

/**
* @param FolderInterface|null $folder
* @return $this
*/
public function setFolder(?FolderInterface $folder = null): static
{
$this->folder = $folder;

return $this;
}

/**
* Special case for SVG without XML statement.
*
* @param DocumentInterface $document
*/
protected function parseSvgMimeType(DocumentInterface $document): void
{
if (
($document->getMimeType() === 'text/plain' || $document->getMimeType() === 'text/html') &&
preg_match('#\.svg$#', $document->getFilename())
('text/plain' === $document->getMimeType() || 'text/html' === $document->getMimeType())
&& preg_match('#\.svg$#', $document->getFilename())
) {
$this->logger->debug('Uploaded a SVG without xml declaration. Presuming it’s a valid SVG file.');
$document->setMimeType('image/svg+xml');
}
}

/**
* @return DocumentInterface
*/
abstract protected function createDocument(): DocumentInterface;

/**
* @param DocumentInterface $document
*/
abstract protected function persistDocument(DocumentInterface $document): void;

protected function getHashAlgorithm(): string
Expand All @@ -116,14 +103,14 @@ protected function getHashAlgorithm(): string
* Create a document from UploadedFile, Be careful, this method does not flush, only
* persists current Document.
*
* @param bool $allowEmpty Default false, requires a local file to create new document entity
* @param bool $allowEmpty Default false, requires a local file to create new document entity
* @param bool $allowDuplicates Default false, always import new document even if file already exists
* @return null|DocumentInterface
*
* @throws FilesystemException
*/
public function getDocument(bool $allowEmpty = false, bool $allowDuplicates = false): ?DocumentInterface
{
if ($allowEmpty === false) {
if (false === $allowEmpty) {
// Getter throw exception on null file
$file = $this->getFile();
} else {
Expand All @@ -147,8 +134,8 @@ public function getDocument(bool $allowEmpty = false, bool $allowDuplicates = fa
$existingDocument = $this->documentFinder->findOneByHashAndAlgorithm($fileHash, $this->getHashAlgorithm());
if (null !== $existingDocument) {
if (
$existingDocument->isRaw() &&
null !== $existingDownscaledDocument = $existingDocument->getDownscaledDocument()
$existingDocument->isRaw()
&& null !== $existingDownscaledDocument = $existingDocument->getDownscaledDocument()
) {
$existingDocument = $existingDownscaledDocument;
}
Expand All @@ -160,6 +147,7 @@ public function getDocument(bool $allowEmpty = false, bool $allowDuplicates = fa
'File %s already exists with same checksum, do not upload it twice.',
$existingDocument->getFilename()
));

return $existingDocument;
}
}
Expand All @@ -175,8 +163,8 @@ public function getDocument(bool $allowEmpty = false, bool $allowDuplicates = fa
$this->parseSvgMimeType($document);

if (
$document instanceof FileHashInterface &&
false !== $fileHash
$document instanceof FileHashInterface
&& false !== $fileHash
) {
$document->setFileHash($fileHash);
$document->setFileHashAlgorithm($this->getHashAlgorithm());
Expand All @@ -196,8 +184,6 @@ public function getDocument(bool $allowEmpty = false, bool $allowDuplicates = fa
/**
* Updates a document from UploadedFile, Be careful, this method does not flush.
*
* @param DocumentInterface $document
* @return DocumentInterface
* @throws FilesystemException
*/
public function updateDocument(DocumentInterface $document): DocumentInterface
Expand Down Expand Up @@ -231,7 +217,7 @@ public function updateDocument(DocumentInterface $document): DocumentInterface
}
}

$document->setFolder(\mb_substr(hash("crc32b", date('YmdHi')), 0, 12));
$document->setFolder(\mb_substr(hash('crc32b', date('YmdHi')), 0, 12));
}

$document->setFilename($this->getFileName());
Expand All @@ -247,9 +233,6 @@ public function updateDocument(DocumentInterface $document): DocumentInterface
}

/**
* @param File $localFile
* @param DocumentInterface $document
* @return void
* @throws FilesystemException
*/
public function moveFile(File $localFile, DocumentInterface $document): void
Expand All @@ -267,9 +250,6 @@ public function moveFile(File $localFile, DocumentInterface $document): void
}
}

/**
* @return string
*/
protected function getFileName(): string
{
$file = $this->getFile();
Expand All @@ -278,8 +258,8 @@ protected function getFileName(): string
$fileName = $file->getClientOriginalName();
} elseif (
$file instanceof DownloadedFile
&& $file->getOriginalFilename() !== null
&& $file->getOriginalFilename() !== ''
&& null !== $file->getOriginalFilename()
&& '' !== $file->getOriginalFilename()
) {
$fileName = $file->getOriginalFilename();
} else {
Expand All @@ -292,9 +272,6 @@ protected function getFileName(): string
/**
* Create a Document from an external URL.
*
* @param string $downloadUrl
*
* @return DocumentInterface|null
* @throws FilesystemException
*/
public function getDocumentFromUrl(string $downloadUrl): ?DocumentInterface
Expand All @@ -303,6 +280,7 @@ public function getDocumentFromUrl(string $downloadUrl): ?DocumentInterface
if (null !== $downloadedFile) {
return $this->setFile($downloadedFile)->getDocument();
}

return null;
}
}
35 changes: 13 additions & 22 deletions src/AbstractDocumentFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,40 @@

abstract class AbstractDocumentFinder implements DocumentFinderInterface
{
/**
* @inheritDoc
*/
public function findVideosWithFilename(string $fileName): iterable
{
$basename = pathinfo($fileName);
$basename = $basename['filename'];

$sourcesDocsName = [
$basename . '.ogg',
$basename . '.ogv',
$basename . '.mp4',
$basename . '.mov',
$basename . '.avi',
$basename . '.webm',
$basename . '.mkv',
$basename.'.ogg',
$basename.'.ogv',
$basename.'.mp4',
$basename.'.mov',
$basename.'.avi',
$basename.'.webm',
$basename.'.mkv',
];

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

/**
* @inheritDoc
*/
public function findAudiosWithFilename(string $fileName): iterable
{
$basename = pathinfo($fileName);
$basename = $basename['filename'];

$sourcesDocsName = [
$basename . '.mp3',
$basename . '.ogg',
$basename . '.wav',
$basename . '.m4a',
$basename . '.aac',
$basename.'.mp3',
$basename.'.ogg',
$basename.'.wav',
$basename.'.m4a',
$basename.'.aac',
];

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

/**
* @inheritDoc
*/
public function findPicturesWithFilename(string $fileName): iterable
{
$pathInfo = pathinfo($fileName);
Expand All @@ -70,7 +61,7 @@ public function findPicturesWithFilename(string $fileName): iterable
$extensionsList = array_diff($extensionsList, [$currentExtension]);
// list sources paths for extensions
$sourcesDocsName = array_values(array_map(function ($extension) use ($basename) {
return $basename . '.' . $extension;
return $basename.'.'.$extension;
}, $extensionsList));

return $this->findAllByFilenames($sourcesDocsName);
Expand Down
4 changes: 2 additions & 2 deletions src/ArrayDocumentFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function __construct()

/**
* @param array<string> $fileNames
*
* @return ArrayCollection<int, DocumentInterface>
*/
public function findAllByFilenames(array $fileNames): ArrayCollection
Expand All @@ -49,16 +50,15 @@ public function findOneByHashAndAlgorithm(string $hash, string $algorithm): ?Doc
return null;
}


/**
* @param DocumentInterface $document
* @return $this
*/
public function addDocument(DocumentInterface $document): self
{
if (!$this->documents->contains($document)) {
$this->documents->add($document);
}

return $this;
}
}
Loading

0 comments on commit 7e581d6

Please sign in to comment.