Skip to content

Commit

Permalink
chore: Bumped
Browse files Browse the repository at this point in the history
  • Loading branch information
roadiz-ci committed Feb 17, 2025
1 parent 5f00b7e commit 73ebafd
Show file tree
Hide file tree
Showing 88 changed files with 1,864 additions and 1,062 deletions.
4 changes: 3 additions & 1 deletion .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.2', '8.3']
php-version: ['8.1', '8.2', '8.3']
steps:
- uses: shivammathur/setup-php@v2
with:
Expand All @@ -37,5 +37,7 @@ 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
22 changes: 11 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@
}
],
"require": {
"php": ">=8.2",
"php": ">=8.1",
"ext-json": "*",
"ext-gd": "*",
"ext-dom": "*",
"ext-zip": "*",
"ext-simplexml": "*",
"ext-fileinfo": "*",
"doctrine/orm": "~2.20.0",
"doctrine/orm": "~2.19.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 @@ -36,19 +38,17 @@
"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.16"
"twig/twig": "^3.1"
},
"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/phpdoc-parser": "<2",
"phpstan/phpstan-doctrine": "^1.3",
"phpunit/phpunit": "^9.5"
"phpstan/phpstan-doctrine": "^1.3"
},
"autoload": {
"psr-4": {
Expand All @@ -68,8 +68,8 @@
},
"extra": {
"branch-alias": {
"dev-master": "2.4.x-dev",
"dev-develop": "2.5.x-dev"
"dev-master": "2.3.x-dev",
"dev-develop": "2.4.x-dev"
}
}
}
13 changes: 13 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>

<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd">
<arg name="basepath" value="."/>
<arg name="cache" value=".phpcs-cache"/>
<arg name="colors"/>
<arg name="extensions" value="php"/>
<rule ref="PSR12">
<exclude name="Generic.Files.LineLength"/>
</rule>
<file>src/</file>
</ruleset>
58 changes: 40 additions & 18 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,56 +42,69 @@ 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 (
('text/plain' === $document->getMimeType() || 'text/html' === $document->getMimeType())
&& preg_match('#\.svg$#', $document->getFilename())
($document->getMimeType() === 'text/plain' || $document->getMimeType() === 'text/html') &&
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 @@ -103,14 +116,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 (false === $allowEmpty) {
if ($allowEmpty === false) {
// Getter throw exception on null file
$file = $this->getFile();
} else {
Expand All @@ -134,8 +147,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 @@ -147,7 +160,6 @@ 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 @@ -163,8 +175,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 @@ -184,6 +196,8 @@ 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 @@ -217,7 +231,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 @@ -233,6 +247,9 @@ 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 @@ -250,6 +267,9 @@ public function moveFile(File $localFile, DocumentInterface $document): void
}
}

/**
* @return string
*/
protected function getFileName(): string
{
$file = $this->getFile();
Expand All @@ -258,8 +278,8 @@ protected function getFileName(): string
$fileName = $file->getClientOriginalName();
} elseif (
$file instanceof DownloadedFile
&& null !== $file->getOriginalFilename()
&& '' !== $file->getOriginalFilename()
&& $file->getOriginalFilename() !== null
&& $file->getOriginalFilename() !== ''
) {
$fileName = $file->getOriginalFilename();
} else {
Expand All @@ -272,6 +292,9 @@ 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 @@ -280,7 +303,6 @@ public function getDocumentFromUrl(string $downloadUrl): ?DocumentInterface
if (null !== $downloadedFile) {
return $this->setFile($downloadedFile)->getDocument();
}

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

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 @@ -61,7 +70,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,7 +24,6 @@ public function __construct()

/**
* @param array<string> $fileNames
*
* @return ArrayCollection<int, DocumentInterface>
*/
public function findAllByFilenames(array $fileNames): ArrayCollection
Expand All @@ -50,15 +49,16 @@ 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 73ebafd

Please sign in to comment.