diff --git a/Classes/Utility/FileUtility.php b/Classes/Utility/FileUtility.php index 55ea3652..622ebd08 100644 --- a/Classes/Utility/FileUtility.php +++ b/Classes/Utility/FileUtility.php @@ -27,51 +27,22 @@ use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer; use TYPO3\CMS\Frontend\Typolink\LinkResultInterface; -/** - * Class FileUtility - */ class FileUtility { public const RETINA_RATIO = 2; public const LQIP_RATIO = 0.1; - /** - * @var ContentObjectRenderer - */ - protected $contentObjectRenderer; - - /** - * @param RendererRegistry - */ - protected $rendererRegistry; - - /** - * @var ImageService - */ - protected $imageService; - - /** - * @var ServerRequestInterface - */ - protected $serverRequest; - - /** - * @var array - */ - protected $errors = []; + protected ContentObjectRenderer $contentObjectRenderer; + protected RendererRegistry $rendererRegistry; + protected ImageService $imageService; + protected ?ServerRequestInterface $serverRequest; + protected EventDispatcherInterface $eventDispatcher; /** - * @var EventDispatcherInterface + * @var array> */ - private EventDispatcherInterface $eventDispatcher; + protected array $errors = []; - /** - * @param ContentObjectRenderer|null $contentObjectRenderer - * @param RendererRegistry|null $rendererRegistry - * @param ImageService|null $imageService - * @param ServerRequestInterface|null $serverRequest - * @param EventDispatcherInterface|null $eventDispatcher - */ public function __construct( ?ContentObjectRenderer $contentObjectRenderer = null, ?RendererRegistry $rendererRegistry = null, @@ -98,14 +69,11 @@ public function processFile(FileInterface $fileReference, array $dimensions = [] $fileRenderer = $this->rendererRegistry->getRenderer($fileReference); $crop = $fileReference->getProperty('crop'); $originalFileUrl = $fileReference->getPublicUrl(); - - $metaData = $fileReference->toArray(); - - $link = null; + $link = $fileReference->getProperty('link'); $linkData = null; - if (!empty($metaData['link'])) { - $linkData = $this->contentObjectRenderer->typoLink('', ['parameter' => $metaData['link'], 'returnLast' => 'result']); + if (!empty($link)) { + $linkData = $this->contentObjectRenderer->typoLink('', ['parameter' => $link, 'returnLast' => 'result']); $link = $linkData instanceof LinkResultInterface ? $linkData->getUrl() : null; } @@ -113,7 +81,7 @@ public function processFile(FileInterface $fileReference, array $dimensions = [] 'title' => $fileReference->getProperty('title'), 'alternative' => $fileReference->getProperty('alternative'), 'description' => $fileReference->getProperty('description'), - 'link' => $link ?? null, + 'link' => $link === '' ? null : $link, 'linkData' => $linkData ?? null, ]; @@ -145,10 +113,8 @@ public function processFile(FileInterface $fileReference, array $dimensions = [] 'height' => $this->getCroppedDimensionalProperty($fileReference, 'height', $cropVariant) ], 'crop' => $crop, - 'autoplay' => $fileReference->hasProperty('autoplay') - ? $fileReference->getProperty('autoplay') : null, - 'extension' => $fileReference->hasProperty('extension') - ? $fileReference->getProperty('extension') : null, + 'autoplay' => $fileReference->getProperty('autoplay'), + 'extension' => $fileReference->getProperty('extension'), ]; $properties = $this->eventDispatcher->dispatch( @@ -168,20 +134,13 @@ public function processFile(FileInterface $fileReference, array $dimensions = [] } /** - * @param FileInterface $fileReference - * @param array $arguments - * @param string $cropVariant - * @return ProcessedFile + * @param array $arguments */ public function processImageFile(FileInterface $image, array $arguments = [], string $cropVariant = 'default'): ProcessedFile { try { $properties = $image->getProperties(); - $cropString = $properties['crop']; - if ($image->hasProperty('crop') && $image->getProperty('crop')) { - $cropString = $image->getProperty('crop'); - } - $cropVariantCollection = $this->createCropVariant((string)$cropString); + $cropVariantCollection = $this->createCropVariant((string)$image->getProperty('crop')); $cropVariant = $cropVariant ?: 'default'; $cropArea = $cropVariantCollection->getCropArea($cropVariant); $processingInstructions = [ @@ -204,10 +163,6 @@ public function processImageFile(FileInterface $image, array $arguments = [], st } } - /** - * @param string $fileUrl - * @return string - */ public function getAbsoluteUrl(string $fileUrl): string { $siteUrl = $this->getNormalizedParams()->getSiteUrl(); @@ -221,7 +176,7 @@ public function getAbsoluteUrl(string $fileUrl): string return $fileUrl; } - public function getErrors() + public function getErrors(): array { return $this->errors; } @@ -229,12 +184,6 @@ public function getErrors() /** * When retrieving the height or width for a media file * a possible cropping needs to be taken into account. - * - * @param FileInterface $fileObject - * @param string $dimensionalProperty 'width' or 'height' - * - * @param string $cropVariant defaults to 'default' variant - * @return int */ protected function getCroppedDimensionalProperty( FileInterface $fileObject, @@ -251,10 +200,6 @@ protected function getCroppedDimensionalProperty( )[$dimensionalProperty]; } - /** - * @param int $value - * @return string - */ protected function calculateKilobytesToFileSize(int $value): string { $units = $this->translate('viewhelper.format.bytes.units', 'fluid'); @@ -267,9 +212,6 @@ protected function calculateKilobytesToFileSize(int $value): string return number_format(round($bytes, 4 * 2)) . ' ' . $units[$pow]; } - /** - * @return NormalizedParams - */ protected function getNormalizedParams(): NormalizedParams { return $this->serverRequest->getAttribute('normalizedParams'); diff --git a/Tests/Unit/Utility/FileUtilityTest.php b/Tests/Unit/Utility/FileUtilityTest.php index 69733c58..6ea77b5e 100644 --- a/Tests/Unit/Utility/FileUtilityTest.php +++ b/Tests/Unit/Utility/FileUtilityTest.php @@ -34,6 +34,8 @@ use TYPO3\CMS\Frontend\Typolink\LinkResult; use TYPO3\TestingFramework\Core\Unit\UnitTestCase; +use function array_merge; + class FileUtilityTest extends UnitTestCase { use ProphecyTrait; @@ -264,6 +266,10 @@ protected function getMockFileForData($data, array $overrideToArray = []) $file->method('getUid')->willReturn($data['uid']); $file->method('getPublicUrl')->willReturn('/fileadmin/test-file.jpg'); if ($overrideToArray !== []) { + $_data = array_merge($data, $overrideToArray); + $file->method('getProperty')->willReturnCallback(static function ($key) use ($_data) { + return $_data[$key] ?? null; + }); $file->method('toArray')->willReturn($overrideToArray); } else { $file->method('toArray')->willReturn(