Refactor ActivityReasonAggregator: can be applied also on export for Activities linked with accompanying period

This commit renames the ActivityReasonAggregator's namespace from PersonAggregators to Aggregator and modifies its methods. The join method in the query builder has been changed from innerJoin to leftJoin, 'group by' part is simplified, and the applyOn method now returns Declarations::ACTIVITY instead of Declarations::ACTIVITY_PERSON. Further modifications apply to the getFormDefaultData method and the corresponding test.
This commit is contained in:
Julien Fastré 2024-02-07 16:40:03 +01:00
parent c05451bfe9
commit 67c3de733f
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
4 changed files with 32 additions and 24 deletions

View File

@ -0,0 +1,5 @@
kind: Feature
body: Allow to group activities linked with accompanying period by reason
time: 2024-02-07T16:40:38.408575109+01:00
custom:
Issue: "229"

View File

@ -9,7 +9,7 @@ declare(strict_types=1);
* the LICENSE file that was distributed with this source code. * the LICENSE file that was distributed with this source code.
*/ */
namespace Chill\ActivityBundle\Export\Aggregator\PersonAggregators; namespace Chill\ActivityBundle\Export\Aggregator;
use Chill\ActivityBundle\Export\Declarations; use Chill\ActivityBundle\Export\Declarations;
use Chill\ActivityBundle\Repository\ActivityReasonCategoryRepository; use Chill\ActivityBundle\Repository\ActivityReasonCategoryRepository;
@ -25,8 +25,11 @@ use Symfony\Component\Validator\Context\ExecutionContextInterface;
class ActivityReasonAggregator implements AggregatorInterface, ExportElementValidatedInterface class ActivityReasonAggregator implements AggregatorInterface, ExportElementValidatedInterface
{ {
public function __construct(protected ActivityReasonCategoryRepository $activityReasonCategoryRepository, protected ActivityReasonRepository $activityReasonRepository, protected TranslatableStringHelper $translatableStringHelper) public function __construct(
{ protected ActivityReasonCategoryRepository $activityReasonCategoryRepository,
protected ActivityReasonRepository $activityReasonRepository,
protected TranslatableStringHelper $translatableStringHelper
) {
} }
public function addRole(): ?string public function addRole(): ?string
@ -51,7 +54,7 @@ class ActivityReasonAggregator implements AggregatorInterface, ExportElementVali
// make a jointure only if needed // make a jointure only if needed
if (!\in_array('actreasons', $qb->getAllAliases(), true)) { if (!\in_array('actreasons', $qb->getAllAliases(), true)) {
$qb->innerJoin('activity.reasons', 'actreasons'); $qb->leftJoin('activity.reasons', 'actreasons');
} }
// join category if necessary // join category if necessary
@ -62,19 +65,12 @@ class ActivityReasonAggregator implements AggregatorInterface, ExportElementVali
} }
} }
// add the "group by" part $qb->addGroupBy($alias);
$groupBy = $qb->getDQLPart('groupBy');
if (\count($groupBy) > 0) {
$qb->addGroupBy($alias);
} else {
$qb->groupBy($alias);
}
} }
public function applyOn(): string public function applyOn(): string
{ {
return Declarations::ACTIVITY_PERSON; return Declarations::ACTIVITY;
} }
public function buildForm(FormBuilderInterface $builder) public function buildForm(FormBuilderInterface $builder)
@ -96,7 +92,9 @@ class ActivityReasonAggregator implements AggregatorInterface, ExportElementVali
public function getFormDefaultData(): array public function getFormDefaultData(): array
{ {
return []; return [
'level' => 'reasons',
];
} }
public function getLabels($key, array $values, $data) public function getLabels($key, array $values, $data)

View File

@ -9,10 +9,10 @@ declare(strict_types=1);
* the LICENSE file that was distributed with this source code. * the LICENSE file that was distributed with this source code.
*/ */
namespace Chill\ActivityBundle\Tests\Export\Aggregator\PersonAggregators; namespace Chill\ActivityBundle\Tests\Export\Aggregator;
use Chill\ActivityBundle\Entity\Activity; use Chill\ActivityBundle\Entity\Activity;
use Chill\ActivityBundle\Export\Aggregator\PersonAggregators\ActivityReasonAggregator; use Chill\ActivityBundle\Export\Aggregator\ActivityReasonAggregator;
use Chill\MainBundle\Test\Export\AbstractAggregatorTest; use Chill\MainBundle\Test\Export\AbstractAggregatorTest;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\PhpUnit\ProphecyTrait;
@ -33,14 +33,14 @@ final class ActivityReasonAggregatorTest extends AbstractAggregatorTest
self::bootKernel(); self::bootKernel();
$this->aggregator = self::$container->get(ActivityReasonAggregator::class); $this->aggregator = self::$container->get(ActivityReasonAggregator::class);
/*
$request = $this->prophesize()
->willExtend(\Symfony\Component\HttpFoundation\Request::class);
$request = $this->prophesize() $request->getLocale()->willReturn('fr');
->willExtend(\Symfony\Component\HttpFoundation\Request::class);
$request->getLocale()->willReturn('fr'); self::$container->get('request_stack')
->push($request->reveal());*/
self::$container->get('request_stack')
->push($request->reveal());
} }
public function getAggregator() public function getAggregator()
@ -65,7 +65,12 @@ final class ActivityReasonAggregatorTest extends AbstractAggregatorTest
return [ return [
$em->createQueryBuilder() $em->createQueryBuilder()
->select('count(activity.id)') ->select('count(activity.id)')
->from(Activity::class, 'activity'), ->from(Activity::class, 'activity')
->join('activity.person', 'person'),
$em->createQueryBuilder()
->select('count(activity.id)')
->from(Activity::class, 'activity')
->join('activity.accompanyingPeriod', 'accompanyingPeriod'),
$em->createQueryBuilder() $em->createQueryBuilder()
->select('count(activity.id)') ->select('count(activity.id)')
->from(Activity::class, 'activity') ->from(Activity::class, 'activity')

View File

@ -153,7 +153,7 @@ services:
## Aggregators ## Aggregators
Chill\ActivityBundle\Export\Aggregator\PersonAggregators\ActivityReasonAggregator: Chill\ActivityBundle\Export\Aggregator\ActivityReasonAggregator:
tags: tags:
- { name: chill.export_aggregator, alias: activity_reason_aggregator } - { name: chill.export_aggregator, alias: activity_reason_aggregator }