Skip to content

Commit

Permalink
CLEANUP: Move new rootline element creation to factories
Browse files Browse the repository at this point in the history
  • Loading branch information
d-g-codappix committed Apr 9, 2024
1 parent ff1e07c commit 5c93a02
Show file tree
Hide file tree
Showing 13 changed files with 337 additions and 292 deletions.
54 changes: 54 additions & 0 deletions Classes/Domain/Factory/BackendLayoutFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Codappix\ResponsiveImages\Domain\Factory;

/*
* Copyright (C) 2024 Daniel Gohlke <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/

use Codappix\ResponsiveImages\Configuration\ConfigurationManager;
use Codappix\ResponsiveImages\Domain\Model\BackendLayout;
use Codappix\ResponsiveImages\Domain\Model\RootlineElementInterface;

class BackendLayoutFactory
{
public function __construct(
private ConfigurationManager $configurationManager,
private readonly ScalingFactory $scalingFactory
) {
}

public function create(string $configurationPath): RootlineElementInterface
{
$scaling = $this->scalingFactory->getByConfigurationPath($configurationPath);

$columns = $this->determineColumns($configurationPath . '.columns');

return new BackendLayout($scaling, $columns);
}


private function determineColumns(string $configurationPath): array
{
$columns = $this->configurationManager->getByPath($configurationPath);
assert(is_array($columns));
return array_map(static fn ($column): int => (int) $column, array_keys($columns));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

declare(strict_types=1);

namespace Codappix\ResponsiveImages\Sizes;
namespace Codappix\ResponsiveImages\Domain\Factory;

/*
* Copyright (C) 2024 Justus Moroni <[email protected]>
* Copyright (C) 2024 Daniel Gohlke <[email protected]>
*
* This program is free software; you can redistribute it and/or
Expand All @@ -23,17 +24,30 @@
* 02110-1301, USA.
*/

final class Container extends AbstractContentElement
use Codappix\ResponsiveImages\Configuration\ConfigurationManager;
use Codappix\ResponsiveImages\Domain\Model\Container;

class ContainerFactory
{
public function __construct(array $data)
public function __construct(
private readonly ConfigurationManager $configurationManager

Check failure on line 33 in Classes/Domain/Factory/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / code-quality (8.1, ^12.4)

Property Codappix\ResponsiveImages\Domain\Factory\ContainerFactory::$configurationManager is never read, only written.

Check failure on line 33 in Classes/Domain/Factory/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / code-quality (8.2, ^12.4)

Property Codappix\ResponsiveImages\Domain\Factory\ContainerFactory::$configurationManager is never read, only written.

Check failure on line 33 in Classes/Domain/Factory/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / code-quality (8.3, ^12.4)

Property Codappix\ResponsiveImages\Domain\Factory\ContainerFactory::$configurationManager is never read, only written.
) {
}

public function createFromData(array $data): Container
{
parent::__construct($data);
$this->contentType = $data['CType'];

Check failure on line 39 in Classes/Domain/Factory/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / code-quality (8.1, ^12.4)

Access to an undefined property Codappix\ResponsiveImages\Domain\Factory\ContainerFactory::$contentType.

Check failure on line 39 in Classes/Domain/Factory/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / code-quality (8.2, ^12.4)

Access to an undefined property Codappix\ResponsiveImages\Domain\Factory\ContainerFactory::$contentType.

Check failure on line 39 in Classes/Domain/Factory/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / code-quality (8.3, ^12.4)

Access to an undefined property Codappix\ResponsiveImages\Domain\Factory\ContainerFactory::$contentType.
$this->colPos = $data['colPos'];

Check failure on line 40 in Classes/Domain/Factory/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / code-quality (8.1, ^12.4)

Access to an undefined property Codappix\ResponsiveImages\Domain\Factory\ContainerFactory::$colPos.

Check failure on line 40 in Classes/Domain/Factory/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / code-quality (8.2, ^12.4)

Access to an undefined property Codappix\ResponsiveImages\Domain\Factory\ContainerFactory::$colPos.

Check failure on line 40 in Classes/Domain/Factory/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / code-quality (8.3, ^12.4)

Access to an undefined property Codappix\ResponsiveImages\Domain\Factory\ContainerFactory::$colPos.

$this->scalingConfiguration = $this->readConfigurationByPath(
$scalingConfiguration = $this->readConfigurationByPath(

Check failure on line 42 in Classes/Domain/Factory/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / code-quality (8.1, ^12.4)

Call to an undefined method Codappix\ResponsiveImages\Domain\Factory\ContainerFactory::readConfigurationByPath().

Check failure on line 42 in Classes/Domain/Factory/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / code-quality (8.2, ^12.4)

Call to an undefined method Codappix\ResponsiveImages\Domain\Factory\ContainerFactory::readConfigurationByPath().

Check failure on line 42 in Classes/Domain/Factory/ContainerFactory.php

View workflow job for this annotation

GitHub Actions / code-quality (8.3, ^12.4)

Call to an undefined method Codappix\ResponsiveImages\Domain\Factory\ContainerFactory::readConfigurationByPath().
implode('.', [
'container',
$this->contentType,
])
);

return new Container(
$scalingConfiguration
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Codappix\ResponsiveImages\Sizes;
namespace Codappix\ResponsiveImages\Domain\Factory;

/*
* Copyright (C) 2024 Daniel Gohlke <[email protected]>
Expand All @@ -23,21 +23,20 @@
* 02110-1301, USA.
*/

class ContainerColumn extends AbstractContentElement
use Codappix\ResponsiveImages\Domain\Model\RootlineElement;
use Codappix\ResponsiveImages\Domain\Model\RootlineElementInterface;

class RootlineElementFactory
{
public function __construct(
array $data,
int $colPos
private readonly ScalingFactory $scalingFactory
) {
parent::__construct($data);
}

public function create(array $data, string $configurationPath): RootlineElementInterface
{
$scaling = $this->scalingFactory->getByConfigurationPath($configurationPath);

$this->scalingConfiguration = $this->readConfigurationByPath(
implode('.', [
'container',
$this->contentType,
'columns',
(string) $colPos,
])
);
return new RootlineElement($scaling, $data);
}
}
111 changes: 87 additions & 24 deletions Classes/Domain/Factory/RootlineFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,25 @@
*/

use B13\Container\Tca\Registry;
use Codappix\ResponsiveImages\Domain\Model\RootlineElementInterface;
use Codappix\ResponsiveImages\Domain\Repository\ContainerRepository;
use Codappix\ResponsiveImages\Sizes\BackendLayout;
use Codappix\ResponsiveImages\Sizes\BackendLayoutColumn;
use Codappix\ResponsiveImages\Sizes\Container;
use Codappix\ResponsiveImages\Sizes\ContainerColumn;
use Codappix\ResponsiveImages\Sizes\ContentElement;
use Codappix\ResponsiveImages\Sizes\ContentElementInterface;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Page\PageLayoutResolver;

final class RootlineFactory
{
private BackendLayout $backendLayout;
private RootlineElementInterface $backendLayout;

private string $fieldName;

private string $backendLayoutIdentifier;

public function __construct(
private readonly ContainerRepository $containerRepository,
private readonly PageLayoutResolver $pageLayoutResolver
private readonly PageLayoutResolver $pageLayoutResolver,
private readonly BackendLayoutFactory $backendLayoutFactory,
private readonly RootlineElementFactory $rootlineElementFactory
) {
}

Expand All @@ -72,46 +69,67 @@ public function getFinalSizes(

private function determineBackendLayout(): void
{
$typoscriptFrontendController = $GLOBALS['TSFE'];
$tsfe = $GLOBALS['TSFE'];

$this->backendLayoutIdentifier = $this->pageLayoutResolver->getLayoutForPage(
$typoscriptFrontendController->page,
$typoscriptFrontendController->rootLine
$tsfe->page,
$tsfe->rootLine
);

$this->backendLayout = new BackendLayout($this->backendLayoutIdentifier);
$this->backendLayout = $this->backendLayoutFactory->create(
$this->getConfigPathForBackendLayout()
);
}

private function determineContentElement(
?ContentElementInterface $contentElement,
?RootlineElementInterface $contentElement,
array $data
): ContentElementInterface {
): RootlineElementInterface {
if (
class_exists(Registry::class)
&& GeneralUtility::makeInstance(Registry::class)->isContainerElement($data['CType'])
&& !is_null($contentElement)
) {
$newContainerColumn = new ContainerColumn($data, $contentElement->getColPos());
$contentElement->setParent($newContainerColumn);

$newContainer = new Container($data);
$newContainerColumn->setParent($newContainer);

return $newContainer;
return $this->determineContainer($data, $contentElement);
}

$newContentElement = new ContentElement($data, $this->fieldName);
$newContentElement = $this->rootlineElementFactory->create(
$data,
$this->getConfigPathForContentElement($data['CType'])
);

if (!is_null($contentElement)) {
$contentElement->setParent($newContentElement);
}

return $newContentElement;
}

private function determineRootline(ContentElementInterface $contentElement): void
private function determineContainer(array $data, RootlineElementInterface $contentElement): RootlineElementInterface
{
$newContainerColumn = $this->rootlineElementFactory->create(
$data,
$this->getConfigPathForContainerColumn($data['CType'], $contentElement)
);
$contentElement->setParent($newContainerColumn);

$newContainer = $this->rootlineElementFactory->create(
$data,
$this->getConfigPathForContainer($data['CType'])
);
$newContainerColumn->setParent($newContainer);

return $newContainer;
}

private function determineRootline(RootlineElementInterface $contentElement): void
{
if (in_array($contentElement->getColPos(), $this->backendLayout->getColumns(), true)) {

Check failure on line 127 in Classes/Domain/Factory/RootlineFactory.php

View workflow job for this annotation

GitHub Actions / code-quality (8.1, ^12.4)

Call to an undefined method Codappix\ResponsiveImages\Domain\Model\RootlineElementInterface::getColPos().

Check failure on line 127 in Classes/Domain/Factory/RootlineFactory.php

View workflow job for this annotation

GitHub Actions / code-quality (8.1, ^12.4)

Call to an undefined method Codappix\ResponsiveImages\Domain\Model\RootlineElementInterface::getColumns().

Check failure on line 127 in Classes/Domain/Factory/RootlineFactory.php

View workflow job for this annotation

GitHub Actions / code-quality (8.2, ^12.4)

Call to an undefined method Codappix\ResponsiveImages\Domain\Model\RootlineElementInterface::getColPos().

Check failure on line 127 in Classes/Domain/Factory/RootlineFactory.php

View workflow job for this annotation

GitHub Actions / code-quality (8.2, ^12.4)

Call to an undefined method Codappix\ResponsiveImages\Domain\Model\RootlineElementInterface::getColumns().

Check failure on line 127 in Classes/Domain/Factory/RootlineFactory.php

View workflow job for this annotation

GitHub Actions / code-quality (8.3, ^12.4)

Call to an undefined method Codappix\ResponsiveImages\Domain\Model\RootlineElementInterface::getColPos().

Check failure on line 127 in Classes/Domain/Factory/RootlineFactory.php

View workflow job for this annotation

GitHub Actions / code-quality (8.3, ^12.4)

Call to an undefined method Codappix\ResponsiveImages\Domain\Model\RootlineElementInterface::getColumns().
$newBackendLayoutColumn = new BackendLayoutColumn($this->backendLayoutIdentifier, $contentElement->getColPos());
$newBackendLayoutColumn = $this->rootlineElementFactory->create(
[],
$this->getConfigPathForBackendLayoutColumn($contentElement)
);

$newBackendLayoutColumn->setParent($this->backendLayout);
$contentElement->setParent($newBackendLayoutColumn);

Expand All @@ -130,4 +148,49 @@ private function determineRootline(ContentElementInterface $contentElement): voi
$this->determineRootline($parent);
}
}

private function getConfigPathForContentElement(string $CType): string
{
return implode('.', [
'contentelements',
$CType,
$this->fieldName,
]);
}

private function getConfigPathForContainer(string $CType): string
{
return implode('.', [
'container',
$CType,
]);
}

private function getConfigPathForContainerColumn(string $CType, RootlineElementInterface $contentElement): string
{
return implode('.', [
'container',
$CType,
'columns',
(string) $contentElement->getColPos(),

Check failure on line 175 in Classes/Domain/Factory/RootlineFactory.php

View workflow job for this annotation

GitHub Actions / code-quality (8.1, ^12.4)

Call to an undefined method Codappix\ResponsiveImages\Domain\Model\RootlineElementInterface::getColPos().

Check failure on line 175 in Classes/Domain/Factory/RootlineFactory.php

View workflow job for this annotation

GitHub Actions / code-quality (8.2, ^12.4)

Call to an undefined method Codappix\ResponsiveImages\Domain\Model\RootlineElementInterface::getColPos().

Check failure on line 175 in Classes/Domain/Factory/RootlineFactory.php

View workflow job for this annotation

GitHub Actions / code-quality (8.3, ^12.4)

Call to an undefined method Codappix\ResponsiveImages\Domain\Model\RootlineElementInterface::getColPos().
]);
}

private function getConfigPathForBackendLayout(): string
{
return implode('.', [
'backendlayouts',
$this->backendLayoutIdentifier,
]);
}

private function getConfigPathForBackendLayoutColumn(RootlineElementInterface $contentElement): string
{
return implode('.', [
'backendlayouts',
$this->backendLayoutIdentifier,
'columns',
$contentElement->getColPos(),

Check failure on line 193 in Classes/Domain/Factory/RootlineFactory.php

View workflow job for this annotation

GitHub Actions / code-quality (8.1, ^12.4)

Call to an undefined method Codappix\ResponsiveImages\Domain\Model\RootlineElementInterface::getColPos().

Check failure on line 193 in Classes/Domain/Factory/RootlineFactory.php

View workflow job for this annotation

GitHub Actions / code-quality (8.2, ^12.4)

Call to an undefined method Codappix\ResponsiveImages\Domain\Model\RootlineElementInterface::getColPos().

Check failure on line 193 in Classes/Domain/Factory/RootlineFactory.php

View workflow job for this annotation

GitHub Actions / code-quality (8.3, ^12.4)

Call to an undefined method Codappix\ResponsiveImages\Domain\Model\RootlineElementInterface::getColPos().
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

declare(strict_types=1);

namespace Codappix\ResponsiveImages\Sizes;
namespace Codappix\ResponsiveImages\Domain\Factory;

/*
* Copyright (C) 2020 Justus Moroni <[email protected]>
* Copyright (C) 2024 Justus Moroni <[email protected]>
* Copyright (C) 2024 Daniel Gohlke <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
Expand All @@ -23,26 +24,23 @@
* 02110-1301, USA.
*/

final class BackendLayoutColumn extends AbstractRootlineElement implements RootlineElementInterface
use Codappix\ResponsiveImages\Configuration\ConfigurationManager;
use Codappix\ResponsiveImages\Domain\Model\Scaling;

final class ScalingFactory
{
public function __construct(
protected string $identifier,
protected int $column
private ConfigurationManager $configurationManager
) {
parent::__construct();

$this->scalingConfiguration = $this->readConfigurationByPath(
implode('.', [
'backendlayouts',
$this->identifier,
'columns',
(string) $this->column,
])
);
}

public function getColumn(): int
public function getByConfigurationPath(string $configurationPath): Scaling
{
return $this->column;
$configuration = $this->configurationManager->getByPath($configurationPath);

$multiplier = $configuration['multiplier'] ?? [];

Check failure on line 41 in Classes/Domain/Factory/ScalingFactory.php

View workflow job for this annotation

GitHub Actions / code-quality (8.1, ^12.4)

Cannot access offset 'multiplier' on mixed.

Check failure on line 41 in Classes/Domain/Factory/ScalingFactory.php

View workflow job for this annotation

GitHub Actions / code-quality (8.2, ^12.4)

Cannot access offset 'multiplier' on mixed.

Check failure on line 41 in Classes/Domain/Factory/ScalingFactory.php

View workflow job for this annotation

GitHub Actions / code-quality (8.3, ^12.4)

Cannot access offset 'multiplier' on mixed.
$sizes = $configuration['sizes'] ?? [];

return new Scaling($multiplier, $sizes);
}
}
Loading

0 comments on commit 5c93a02

Please sign in to comment.