Skip to content

Commit

Permalink
modernize php, more typing, modernize DI
Browse files Browse the repository at this point in the history
  • Loading branch information
mmunz committed Jan 9, 2025
1 parent 91fd088 commit 17c2a81
Show file tree
Hide file tree
Showing 18 changed files with 115 additions and 413 deletions.
39 changes: 8 additions & 31 deletions Classes/Service/SettingsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,20 @@
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;

/**
* Class SettingsService
*
*/
class SettingsService
{
/**
* @var mixed
*/
protected $frameworkConfiguration = null;

/**
* @var mixed
*/
protected $settings = null;

/**
* @var ConfigurationManagerInterface
*/
protected $configurationManager;
protected ?array $frameworkConfiguration = null;
protected ?array $settings = null;

public function __construct(ConfigurationManager $configurationManager)
{
$this->configurationManager = $configurationManager;
public function __construct(
private readonly ConfigurationManager $configurationManager
) {
}

/**
* Returns the framework configuration.
*
* @return array
*/
public function getFrameworkConfiguration()
public function getFrameworkConfiguration(): array
{
if ($this->frameworkConfiguration === null) {
$this->frameworkConfiguration = $this->configurationManager->getConfiguration(
Expand All @@ -55,21 +37,16 @@ public function getFrameworkConfiguration()
* "settings.foo" would return $this->frameworkConfiguration['settings']['foo'].
*
* If the path is invalid or no entry is found, false is returned.
*
* @param string $path
* @return mixed
*/
public function getByPath($path)
public function getByPath(string $path): mixed
{
return ObjectAccess::getPropertyPath($this->getFrameworkConfiguration(), $path);
}

/**
* Returns all TypoScript settings.
*
* @return array
*/
public function getSettings()
public function getSettings(): array
{
if ($this->settings === null) {
$this->settings = $this->getByPath('settings');
Expand Down
47 changes: 10 additions & 37 deletions Classes/Utility/CropVariantUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,20 @@
use TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection;
use TYPO3\CMS\Core\Resource\FileInterface;

/**
* Class CropVariantUtility
*/
class CropVariantUtility
{
/**
* @var CropVariantCollection $cropVariantCollection
*/
protected $cropVariantCollection;

/**
* @var FileInterface $file
*/
protected $file;
protected CropVariantCollection $cropVariantCollection;
protected FileInterface $file;

/**
* @var MathUtility
*/
protected $mathUtility;

public function __construct(MathUtility $mathUtility)
{
$this->mathUtility = $mathUtility;
public function __construct(
private readonly MathUtility $mathUtility
) {
}

/**
* Create a CropVariantCollection from file reference.
*
* @param FileInterface $file
* @return void
*/
public function setCropVariantCollection(FileInterface $file)
public function setCropVariantCollection(FileInterface $file): void
{
$cropString = '';
$this->file = $file;
Expand All @@ -49,32 +31,25 @@ public function setCropVariantCollection(FileInterface $file)

/**
* Get property cropVariantCollection
* @return CropVariantCollection
*/
public function getCropVariantCollection()
public function getCropVariantCollection(): CropVariantCollection
{
return $this->cropVariantCollection;
}

/**
* Returns a calculated Area with coordinates for cropping the actual image
*
* @param string $key
* @return null|Area
*/
public function getCropAreaForVariant(string $key)
public function getCropAreaForVariant(string $key): ?Area
{
$cropArea = $this->cropVariantCollection->getCropArea($key);
return $cropArea->isEmpty() ? null : $cropArea->makeAbsoluteBasedOnFile($this->file);
}

/**
* Returns a calculated array with coordinates for cropping the actual image
*
* @param string $key
* @return null|array
*/
public function getCropAreaForVariantAsArray(string $key)
public function getCropAreaForVariantAsArray(string $key): ?array
{
$area = $this->getCropAreaForVariant($key);
if (!is_null($area)) {
Expand All @@ -85,10 +60,8 @@ public function getCropAreaForVariantAsArray(string $key)

/**
* Return a CropVariants array (beware: not related to TYPO3's CropVariants, needs a better naming - ToDo)
* @param array $mediaQueries
* @return array
*/
public function getCropVariants(array $mediaQueries)
public function getCropVariants(array $mediaQueries): array
{
$cropVariants = [];

Expand Down
10 changes: 1 addition & 9 deletions Classes/Utility/DebugUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,12 @@
declare(strict_types=1);
namespace C1\AdaptiveImages\Utility;

/**
* Class DebugUtility
*/
class DebugUtility
{
/**
* returns a string with debug information for additionalParameters of a processing configuration
* @param int $height
* @param int $width
* @param int|float $ratio
* @param string $processor
* @return string
*/
public function getDebugAnnotation($width, $height, $ratio, $processor = null)
public function getDebugAnnotation(int|string $width, int|string $height, float|int $ratio, ?string $processor = null): string
{
if (!$processor) {
$processor = $GLOBALS['TYPO3_CONF_VARS']['GFX']['processor'] ?? null;
Expand Down
67 changes: 19 additions & 48 deletions Classes/Utility/ImageUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,25 @@
use TYPO3\CMS\Extbase\Exception;
use TYPO3\CMS\Extbase\Service\ImageService;

/**
* Class ImageUtility
*/
class ImageUtility
{
private array $options;
private ?array $options;

private array $settings;

private SettingsService $settingsService;

private ImageService $imageService;

private CropVariantUtility $cropVariantUtility;

private DebugUtility $debugUtility;

private MathUtility $mathUtility;

private FileInterface $originalFile;

private array $cropVariants = [];

public function __construct(
SettingsService $settingsService,
ImageService $imageService,
CropVariantUtility $cropVariantUtility,
DebugUtility $debugUtility,
MathUtility $mathUtility,
private readonly SettingsService $settingsService,
private readonly ImageService $imageService,
private readonly CropVariantUtility $cropVariantUtility,
private readonly DebugUtility $debugUtility,
private readonly MathUtility $mathUtility,
?array $options = null,
?array $settings = null
) {
$this->settingsService = $settingsService;
$this->imageService = $imageService;
$this->cropVariantUtility = $cropVariantUtility;
$this->debugUtility = $debugUtility;
$this->mathUtility = $mathUtility;

if ($options) {
// @extensionScannerIgnoreLine
Expand All @@ -63,11 +45,7 @@ public function __construct(
}
}

/**
* @param array $options
* @return void
*/
public function init($options = null)
public function init(?array $options = null): void
{
if ($options) {
// @extensionScannerIgnoreLine
Expand All @@ -76,11 +54,7 @@ public function init($options = null)
$this->cropVariants = $this->options['cropVariants'] ?? [];
}

/**
* @param array $options
* @return void
*/
public function setOptions($options)
public function setOptions(?array $options): void
{
$this->options = $options;
}
Expand All @@ -90,12 +64,7 @@ public function setOriginalFile(FileInterface $file): void
$this->originalFile = $file;
}

/**
* @param array $processingConfiguration
* @return array
* @throws Exception
*/
public function processImage($processingConfiguration)
public function processImage(array $processingConfiguration): array
{
/** @var FileReference $processedImage */
$processedImage = $this->imageService->applyProcessingInstructions(
Expand All @@ -107,7 +76,7 @@ public function processImage($processingConfiguration)
$processedImage->getProperty('height'),
$processedImage->getProperty('width')
);
if ($this->options['debug'] && $this->options['debug'] === true) {
if ($this->options && $this->options['debug'] && $this->options['debug'] === true) {
if (!isset($processingConfiguration['additionalParameters'])) {
$processingConfiguration['additionalParameters'] = '';
}
Expand All @@ -134,6 +103,7 @@ public function processImage($processingConfiguration)

/**
* Renders a source tag (set of srcset candidates for one cropVariant)
* @throws Exception
*/
public function processSrcsetImages(string $key, array $cropVariantConfig): array
{
Expand Down Expand Up @@ -166,7 +136,12 @@ public function processSrcsetImages(string $key, array $cropVariantConfig): arra
}

$localProcessingConfiguration['width'] = $width . 'c';
if (isset($cropVariantConfig['image_format']) && $cropVariantConfig['image_format'] > 0) {
if (
is_array($this->options)
&& array_key_exists('image_format', $this->options)
&& isset($cropVariantConfig['image_format'])
&& $cropVariantConfig['image_format'] > 0
) {
$img_format = $this->options['image_format'];
$localProcessingConfiguration['height'] = round(intval($width) / $img_format) . 'c';
}
Expand Down Expand Up @@ -196,26 +171,22 @@ public function getSrcSetString(array $candidates): string
*
* Because all candidates have the same ratio we can just return the 'ratio' from the first child of the candidates
* array.
*
* @return array
*/
public function getRatioFromFirstCandidate(array $candidates)
public function getRatioFromFirstCandidate(array $candidates): float
{
return reset($candidates)['ratio'];
}

/**
* @return array|mixed
* @throws Exception
*/
public function getCropVariants()
public function getCropVariants(): array
{
foreach ($this->cropVariants as $key => $cropVariantConfig) {
$candidates = $this->processSrcsetImages($key, $cropVariantConfig);
$this->cropVariants[$key]['candidates'] = $candidates;
$this->cropVariants[$key]['srcset'] = $this->getSrcSetString($candidates);
$this->cropVariants[$key]['ratio'] = $this->getRatioFromFirstCandidate($candidates);
//$this->cropVariants[$key]['svgPlaceholder'] = $this->getSvgPlaceholderFromFirstCandidate($candidates);
// update srcsetWidths with actually generated candidate widths. Some of the configured sizes might
// have been skipped for smaller images or when maxWidth for the image was reached.
$this->cropVariants[$key]['srcsetWidths'] = implode(',', array_keys($candidates));
Expand Down
12 changes: 2 additions & 10 deletions Classes/Utility/MathUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;

/**
* Class MathUtility
*/
class MathUtility implements LoggerAwareInterface
{
use LoggerAwareTrait;
Expand All @@ -19,15 +16,10 @@ class MathUtility implements LoggerAwareInterface
*
* Returns a float which is the percentage of height compared to the width
* Rounded to 2 decimals by default.
*
* @param int|float $height
* @param int|float $width
* @param int $precision
* @return float
*/
public function calculateRatio($height, $width, int $precision = 2)
public function calculateRatio(float|int $height, float|int $width, int $precision = 2): float
{
// Corrupted or empty images don't have width or height or it is 0 which caused division by zero errors, see #17.
// Corrupted or empty images don't have width or height, or it is 0 which caused division by zero errors, see #17.
// This should ideally be handled before calling this method.
if (!$width || !$height || $width == 0) {
$this->logger->warning(
Expand Down
Loading

0 comments on commit 17c2a81

Please sign in to comment.