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 */ 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 */ 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; } }