mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-28 21:16:13 +00:00
Refactor aggregator and filter retrieval into a new class
Moved aggregator and filter retrieval logic from ExportGenerator to the newly introduced ExportConfigProcessor class. This improves separation of concerns, simplifies ExportGenerator, and enhances code maintainability and readability. Updated related tests accordingly.
This commit is contained in:
parent
73496e0e1f
commit
d49058805a
49
src/Bundle/ChillMainBundle/Export/ExportConfigProcessor.php
Normal file
49
src/Bundle/ChillMainBundle/Export/ExportConfigProcessor.php
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Chill is a software for social workers
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view
|
||||||
|
* the LICENSE file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Chill\MainBundle\Export;
|
||||||
|
|
||||||
|
class ExportConfigProcessor
|
||||||
|
{
|
||||||
|
public function __construct(private readonly ExportManager $exportManager) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return iterable<string, AggregatorInterface>
|
||||||
|
*/
|
||||||
|
public function retrieveUsedAggregators(mixed $data): iterable
|
||||||
|
{
|
||||||
|
if (null === $data) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($data as $alias => $aggregatorData) {
|
||||||
|
if ($this->exportManager->hasAggregator($alias) && true === $aggregatorData['enabled']) {
|
||||||
|
yield $alias => $this->exportManager->getAggregator($alias);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return iterable<string, FilterInterface>
|
||||||
|
*/
|
||||||
|
public function retrieveUsedFilters(mixed $data): iterable
|
||||||
|
{
|
||||||
|
if (null === $data) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($data as $alias => $filterData) {
|
||||||
|
if ($this->exportManager->hasFilter($alias) && true === $filterData['enabled']) {
|
||||||
|
yield $alias => $this->exportManager->getFilter($alias);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -33,6 +33,7 @@ final readonly class ExportGenerator
|
|||||||
private LoggerInterface $logger,
|
private LoggerInterface $logger,
|
||||||
private AuthorizationHelperInterface $authorizationHelper,
|
private AuthorizationHelperInterface $authorizationHelper,
|
||||||
private CenterRegroupementResolver $centerRegroupementResolver,
|
private CenterRegroupementResolver $centerRegroupementResolver,
|
||||||
|
private ExportConfigProcessor $exportConfigProcessor,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public function generate(string $exportAlias, array $configuration, ?User $byUser = null): FormattedExportGeneration
|
public function generate(string $exportAlias, array $configuration, ?User $byUser = null): FormattedExportGeneration
|
||||||
@ -94,10 +95,10 @@ final readonly class ExportGenerator
|
|||||||
$aggregatorsData = [];
|
$aggregatorsData = [];
|
||||||
|
|
||||||
if ($query instanceof QueryBuilder) {
|
if ($query instanceof QueryBuilder) {
|
||||||
foreach ($this->retrieveUsedAggregators($data[ExportType::AGGREGATOR_KEY]) as $alias => $aggregator) {
|
foreach ($this->exportConfigProcessor->retrieveUsedAggregators($data[ExportType::AGGREGATOR_KEY]) as $alias => $aggregator) {
|
||||||
$aggregatorsData[$alias] = $data[ExportType::AGGREGATOR_KEY][$alias]['form'];
|
$aggregatorsData[$alias] = $data[ExportType::AGGREGATOR_KEY][$alias]['form'];
|
||||||
}
|
}
|
||||||
foreach ($this->retrieveUsedFilters($data[ExportType::FILTER_KEY]) as $alias => $filter) {
|
foreach ($this->exportConfigProcessor->retrieveUsedFilters($data[ExportType::FILTER_KEY]) as $alias => $filter) {
|
||||||
$filtersData[$alias] = $data[ExportType::FILTER_KEY][$alias]['form'];
|
$filtersData[$alias] = $data[ExportType::FILTER_KEY][$alias]['form'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -184,7 +185,7 @@ final readonly class ExportGenerator
|
|||||||
|
|
||||||
$usedTypes = [];
|
$usedTypes = [];
|
||||||
|
|
||||||
foreach ($this->retrieveUsedFilters($data) as $filter) {
|
foreach ($this->exportConfigProcessor->retrieveUsedFilters($data) as $filter) {
|
||||||
if (!\in_array($filter->applyOn(), $usedTypes, true)) {
|
if (!\in_array($filter->applyOn(), $usedTypes, true)) {
|
||||||
$usedTypes[] = $filter->applyOn();
|
$usedTypes[] = $filter->applyOn();
|
||||||
}
|
}
|
||||||
@ -204,7 +205,7 @@ final readonly class ExportGenerator
|
|||||||
|
|
||||||
$usedTypes = [];
|
$usedTypes = [];
|
||||||
|
|
||||||
foreach ($this->retrieveUsedAggregators($data) as $alias => $aggregator) {
|
foreach ($this->exportConfigProcessor->retrieveUsedAggregators($data) as $alias => $aggregator) {
|
||||||
if (!\in_array($aggregator->applyOn(), $usedTypes, true)) {
|
if (!\in_array($aggregator->applyOn(), $usedTypes, true)) {
|
||||||
$usedTypes[] = $aggregator->applyOn();
|
$usedTypes[] = $aggregator->applyOn();
|
||||||
}
|
}
|
||||||
@ -213,38 +214,6 @@ final readonly class ExportGenerator
|
|||||||
return $usedTypes;
|
return $usedTypes;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return iterable<string, AggregatorInterface>
|
|
||||||
*/
|
|
||||||
private function retrieveUsedAggregators(mixed $data): iterable
|
|
||||||
{
|
|
||||||
if (null === $data) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($data as $alias => $aggregatorData) {
|
|
||||||
if ($this->exportManager->hasAggregator($alias) && true === $aggregatorData['enabled']) {
|
|
||||||
yield $alias => $this->exportManager->getAggregator($alias);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return iterable<string, FilterInterface>
|
|
||||||
*/
|
|
||||||
private function retrieveUsedFilters(mixed $data): iterable
|
|
||||||
{
|
|
||||||
if (null === $data) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($data as $alias => $filterData) {
|
|
||||||
if ($this->exportManager->hasFilter($alias) && true === $filterData['enabled']) {
|
|
||||||
yield $alias => $this->exportManager->getFilter($alias);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alter the query with selected aggregators.
|
* Alter the query with selected aggregators.
|
||||||
*/
|
*/
|
||||||
@ -253,7 +222,7 @@ final readonly class ExportGenerator
|
|||||||
array $data,
|
array $data,
|
||||||
ExportGenerationContext $context,
|
ExportGenerationContext $context,
|
||||||
): void {
|
): void {
|
||||||
foreach ($this->retrieveUsedAggregators($data) as $alias => $aggregator) {
|
foreach ($this->exportConfigProcessor->retrieveUsedAggregators($data) as $alias => $aggregator) {
|
||||||
$formData = $data[$alias];
|
$formData = $data[$alias];
|
||||||
$aggregator->alterQuery($qb, $formData['form'], $context);
|
$aggregator->alterQuery($qb, $formData['form'], $context);
|
||||||
}
|
}
|
||||||
@ -267,7 +236,7 @@ final readonly class ExportGenerator
|
|||||||
mixed $data,
|
mixed $data,
|
||||||
ExportGenerationContext $context,
|
ExportGenerationContext $context,
|
||||||
): void {
|
): void {
|
||||||
foreach ($this->retrieveUsedFilters($data) as $alias => $filter) {
|
foreach ($this->exportConfigProcessor->retrieveUsedFilters($data) as $alias => $filter) {
|
||||||
$formData = $data[$alias];
|
$formData = $data[$alias];
|
||||||
|
|
||||||
$filter->alterQuery($qb, $formData['form'], $context);
|
$filter->alterQuery($qb, $formData['form'], $context);
|
||||||
|
@ -17,6 +17,7 @@ use Chill\MainBundle\Entity\User;
|
|||||||
use Chill\MainBundle\Export\AggregatorInterface;
|
use Chill\MainBundle\Export\AggregatorInterface;
|
||||||
use Chill\MainBundle\Export\DirectExportInterface;
|
use Chill\MainBundle\Export\DirectExportInterface;
|
||||||
use Chill\MainBundle\Export\ExportConfigNormalizer;
|
use Chill\MainBundle\Export\ExportConfigNormalizer;
|
||||||
|
use Chill\MainBundle\Export\ExportConfigProcessor;
|
||||||
use Chill\MainBundle\Export\ExportGenerationContext;
|
use Chill\MainBundle\Export\ExportGenerationContext;
|
||||||
use Chill\MainBundle\Export\ExportGenerator;
|
use Chill\MainBundle\Export\ExportGenerator;
|
||||||
use Chill\MainBundle\Export\ExportInterface;
|
use Chill\MainBundle\Export\ExportInterface;
|
||||||
@ -131,7 +132,14 @@ class ExportGeneratorTest extends TestCase
|
|||||||
$authorizationHelper = $this->prophesize(AuthorizationHelperInterface::class);
|
$authorizationHelper = $this->prophesize(AuthorizationHelperInterface::class);
|
||||||
$authorizationHelper->getReachableCenters($user, 'dummy_role')->willReturn([$centerA, $centerB]);
|
$authorizationHelper->getReachableCenters($user, 'dummy_role')->willReturn([$centerA, $centerB]);
|
||||||
|
|
||||||
$generator = new ExportGenerator($exportManager->reveal(), $exportConfigNormalizer->reveal(), new NullLogger(), $authorizationHelper->reveal(), new CenterRegroupementResolver());
|
$generator = new ExportGenerator(
|
||||||
|
$exportManager->reveal(),
|
||||||
|
$exportConfigNormalizer->reveal(),
|
||||||
|
new NullLogger(),
|
||||||
|
$authorizationHelper->reveal(),
|
||||||
|
new CenterRegroupementResolver(),
|
||||||
|
new ExportConfigProcessor($exportManager->reveal())
|
||||||
|
);
|
||||||
|
|
||||||
$actual = $generator->generate('dummy', $initialData, $user);
|
$actual = $generator->generate('dummy', $initialData, $user);
|
||||||
|
|
||||||
@ -191,7 +199,14 @@ class ExportGeneratorTest extends TestCase
|
|||||||
$authorizationHelper = $this->prophesize(AuthorizationHelperInterface::class);
|
$authorizationHelper = $this->prophesize(AuthorizationHelperInterface::class);
|
||||||
$authorizationHelper->getReachableCenters($user, 'dummy_role')->willReturn([$centerA, $centerB]);
|
$authorizationHelper->getReachableCenters($user, 'dummy_role')->willReturn([$centerA, $centerB]);
|
||||||
|
|
||||||
$generator = new ExportGenerator($exportManager->reveal(), $exportConfigNormalizer->reveal(), new NullLogger(), $authorizationHelper->reveal(), new CenterRegroupementResolver());
|
$generator = new ExportGenerator(
|
||||||
|
$exportManager->reveal(),
|
||||||
|
$exportConfigNormalizer->reveal(),
|
||||||
|
new NullLogger(),
|
||||||
|
$authorizationHelper->reveal(),
|
||||||
|
new CenterRegroupementResolver(),
|
||||||
|
new ExportConfigProcessor($exportManager->reveal())
|
||||||
|
);
|
||||||
|
|
||||||
$actual = $generator->generate('dummy', $initialData, $user);
|
$actual = $generator->generate('dummy', $initialData, $user);
|
||||||
|
|
||||||
@ -233,7 +248,14 @@ class ExportGeneratorTest extends TestCase
|
|||||||
$authorizationHelper = $this->prophesize(AuthorizationHelperInterface::class);
|
$authorizationHelper = $this->prophesize(AuthorizationHelperInterface::class);
|
||||||
$authorizationHelper->getReachableCenters($user, 'dummy_role')->willReturn([$centerA, $centerB]);
|
$authorizationHelper->getReachableCenters($user, 'dummy_role')->willReturn([$centerA, $centerB]);
|
||||||
|
|
||||||
$generator = new ExportGenerator($exportManager->reveal(), $exportConfigNormalizer->reveal(), new NullLogger(), $authorizationHelper->reveal(), new CenterRegroupementResolver());
|
$generator = new ExportGenerator(
|
||||||
|
$exportManager->reveal(),
|
||||||
|
$exportConfigNormalizer->reveal(),
|
||||||
|
new NullLogger(),
|
||||||
|
$authorizationHelper->reveal(),
|
||||||
|
new CenterRegroupementResolver(),
|
||||||
|
new ExportConfigProcessor($exportManager->reveal()),
|
||||||
|
);
|
||||||
|
|
||||||
$actual = $generator->generate('dummy', $initialData, $user);
|
$actual = $generator->generate('dummy', $initialData, $user);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user