Files
Alexandre Gain b138056e43 style: fix indentation, alignment, and docblock annotations
- Correct getChoices() docblock to reflect translated label => enum
  backing value order instead of value => label
- Re-indent Smart Links menu config to match actual array nesting
- Align assignment operators in RefreshStatsCommand
- Add missing newline at end of RefreshStatsCommand.php
2026-11-21 11:23:33 +01:00

136 lines
4.7 KiB
PHP

<?php
declare(strict_types=1);
namespace MauticPlugin\MT3SMarketingProgramBundle\Command;
use Doctrine\ORM\EntityManagerInterface;
use MauticPlugin\MT3SMarketingProgramBundle\Entity\Program;
use MauticPlugin\MT3SMarketingProgramBundle\Entity\ProgramAllocationItem;
use MauticPlugin\MT3SMarketingProgramBundle\Entity\ProgramAudience;
use MauticPlugin\MT3SMarketingProgramBundle\Entity\ProgramDescriptionItem;
use MauticPlugin\MT3SMarketingProgramBundle\Entity\ProgramObjective;
use MauticPlugin\MT3SMarketingProgramBundle\Entity\ProgramStakeholder;
use MauticPlugin\MT3SMarketingProgramBundle\Entity\Task;
use MauticPlugin\MT3SMarketingProgramBundle\Entity\TaskStatus;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand(
name: 'mt3s:marketing-program:refresh-stats',
description: 'Recomputes and prints the per-program statistics (tasks, objectives, insights, allocations, stakeholders, audiences) from the tables.'
)]
class RefreshStatsCommand extends Command
{
public function __construct(private readonly EntityManagerInterface $entityManager)
{
parent::__construct();
}
protected function configure(): void
{
$this->addOption('program', null, InputOption::VALUE_REQUIRED, 'Limit the report to one program ID.');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$programId = $input->getOption('program');
$programs = $this->entityManager->getRepository(Program::class)->findBy(
null !== $programId ? ['id' => (int) $programId] : []
);
if ([] === $programs) {
$io->warning(null !== $programId ? sprintf('No program with the ID %s.', $programId) : 'No programs.');
return Command::SUCCESS;
}
$counts = [
'tasks' => $this->countByProgram(Task::class),
'objectives' => $this->countByProgram(ProgramObjective::class),
'insights' => $this->countByProgram(ProgramDescriptionItem::class),
'allocations' => $this->countByProgram(ProgramAllocationItem::class),
'stakeholders' => $this->countByProgram(ProgramStakeholder::class),
'audiences' => $this->countByProgram(ProgramAudience::class),
];
$done = $this->doneByProgram();
$rows = [];
foreach ($programs as $program) {
$id = (string) $program->getId();
$rows[] = [
$id,
$program->getName(),
sprintf('%d / %d', $done[$id] ?? 0, $counts['tasks'][$id] ?? 0),
$counts['objectives'][$id] ?? 0,
$counts['insights'][$id] ?? 0,
$counts['allocations'][$id] ?? 0,
$counts['stakeholders'][$id] ?? 0,
$counts['audiences'][$id] ?? 0,
];
}
$io->title('MT3S Marketing Program statistics');
$io->table(
['ID', 'Name', 'Done / tasks', 'Objectives', 'Insights', 'Allocations', 'Stakeholders', 'Audiences'],
$rows
);
return Command::SUCCESS;
}
/**
* Rows of the given entity class grouped by their program: [program id => count].
*
* @param class-string $entityClass
*
* @return array<string, int>
*/
private function countByProgram(string $entityClass): array
{
$rows = $this->entityManager->getRepository($entityClass)
->createQueryBuilder('c')
->select('IDENTITY(c.program) AS program_id, COUNT(c.id) AS total')
->groupBy('program_id')
->getQuery()
->getArrayResult();
$counts = [];
foreach ($rows as $row) {
$counts[(string) $row['program_id']] = (int) $row['total'];
}
return $counts;
}
/**
* Done tasks per program: [program id => done count].
*
* @return array<string, int>
*/
private function doneByProgram(): array
{
$rows = $this->entityManager->getRepository(Task::class)
->createQueryBuilder('t')
->select('IDENTITY(t.program) AS program_id, SUM(CASE WHEN t.status = :done THEN 1 ELSE 0 END) AS done')
->setParameter('done', TaskStatus::Done)
->groupBy('program_id')
->getQuery()
->getArrayResult();
$done = [];
foreach ($rows as $row) {
$done[(string) $row['program_id']] = (int) $row['done'];
}
return $done;
}
}