From d4414a5c4e0600b9e269d16bc83935fea527baa7 Mon Sep 17 00:00:00 2001 From: Daniel Gohlke Date: Tue, 9 Apr 2024 11:02:43 +0200 Subject: [PATCH] CLEANUP: Use DI for ContainerRepository --- .../ResponsiveImagesProcessor.php | 12 ++++++------ .../Domain/Repository/ContainerRepository.php | 11 ++++++----- Classes/Sizes/Rootline.php | 5 +---- Configuration/Services.yaml | 19 +++++++++++++++++++ 4 files changed, 32 insertions(+), 15 deletions(-) diff --git a/Classes/DataProcessing/ResponsiveImagesProcessor.php b/Classes/DataProcessing/ResponsiveImagesProcessor.php index 0d4dc8b..3191551 100644 --- a/Classes/DataProcessing/ResponsiveImagesProcessor.php +++ b/Classes/DataProcessing/ResponsiveImagesProcessor.php @@ -24,6 +24,7 @@ */ use Codappix\ResponsiveImages\Configuration\ConfigurationManager; +use Codappix\ResponsiveImages\Domain\Repository\ContainerRepository; use Codappix\ResponsiveImages\Sizes\Breakpoint; use Codappix\ResponsiveImages\Sizes\Rootline; use TYPO3\CMS\Core\Resource\FileInterface; @@ -33,8 +34,6 @@ final class ResponsiveImagesProcessor implements DataProcessorInterface { - private readonly ConfigurationManager $configurationManager; - /** * @var FileInterface[] */ @@ -44,9 +43,10 @@ final class ResponsiveImagesProcessor implements DataProcessorInterface private array $contentElementSizes = []; - public function __construct() - { - $this->configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class); + public function __construct( + private ConfigurationManager $configurationManager, + private ContainerRepository $containerRepository + ) { } public function process( @@ -76,7 +76,7 @@ public function process( return $processedData; } - $this->contentElementSizes = (new Rootline($processedData['data'], $fieldName))->getFinalSizes(); + $this->contentElementSizes = (new Rootline($this->containerRepository, $processedData['data'], $fieldName))->getFinalSizes(); $this->calculateFileDimensions(); $targetFieldName = (string) $cObj->stdWrapValue( diff --git a/Classes/Domain/Repository/ContainerRepository.php b/Classes/Domain/Repository/ContainerRepository.php index 7b8da08..ab4522b 100644 --- a/Classes/Domain/Repository/ContainerRepository.php +++ b/Classes/Domain/Repository/ContainerRepository.php @@ -5,21 +5,22 @@ namespace Codappix\ResponsiveImages\Domain\Repository; use TYPO3\CMS\Core\Database\Connection; -use TYPO3\CMS\Core\Database\ConnectionPool; -use TYPO3\CMS\Core\Database\Query\QueryBuilder; use TYPO3\CMS\Core\Error\Exception; -use TYPO3\CMS\Core\Utility\GeneralUtility; class ContainerRepository { + public function __construct( + private Connection $connection + ) { + } + /** * @throws \Doctrine\DBAL\Exception * @throws Exception */ public function findByIdentifier(int $identifier): array { - /** @var QueryBuilder $queryBuilder */ - $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tt_content'); + $queryBuilder = $this->connection->createQueryBuilder(); $rawData = $queryBuilder ->select('*') ->from('tt_content') diff --git a/Classes/Sizes/Rootline.php b/Classes/Sizes/Rootline.php index 2e0dd90..649de4f 100644 --- a/Classes/Sizes/Rootline.php +++ b/Classes/Sizes/Rootline.php @@ -36,8 +36,6 @@ final class Rootline private BackendLayout $backendLayout; - private ContainerRepository $containerRepository; - private array $finalSizes = []; private string $fieldName; @@ -45,11 +43,10 @@ final class Rootline private string $backendLayoutIdentifier; public function __construct( + private ContainerRepository $containerRepository, array $data, string $fieldName ) { - $this->containerRepository = new ContainerRepository(); - $this->determineBackendLayout(); $this->fieldName = $fieldName; $this->contentElement = $this->determineContentElement(null, $data); diff --git a/Configuration/Services.yaml b/Configuration/Services.yaml index ade47c2..a66d1e9 100644 --- a/Configuration/Services.yaml +++ b/Configuration/Services.yaml @@ -8,6 +8,14 @@ services: Codappix\ResponsiveImages\: resource: '../Classes/*' + dbconnection.tt_content: + class: 'TYPO3\CMS\Core\Database\Connection' + factory: + - '@TYPO3\CMS\Core\Database\ConnectionPool' + - 'getConnectionForTable' + arguments: + - 'tt_content' + extbaseSettings.ResponsiveImages: class: 'array' factory: @@ -18,7 +26,18 @@ services: $extensionName: 'ResponsiveImages' $pluginName: '' + Codappix\ResponsiveImages\DataProcessing\ResponsiveImagesProcessor: + public: true + arguments: + $configurationManager: '@Codappix\ResponsiveImages\Configuration\ConfigurationManager' + $containerRepository: '@Codappix\ResponsiveImages\Domain\Repository\ContainerRepository' + Codappix\ResponsiveImages\Configuration\ConfigurationManager: public: true arguments: $settings: '@extbaseSettings.ResponsiveImages' + + Codappix\ResponsiveImages\Domain\Repository\ContainerRepository: + public: true + arguments: + - '@dbconnection.tt_content'