Passed
Pull Request — master (#5843)
by
unknown
07:56
created

SendScheduledAnnouncementsCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 54
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A execute() 0 35 5
A configure() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Command;
8
9
use Chamilo\CoreBundle\Framework\Container;
10
use Chamilo\CoreBundle\Repository\Node\AccessUrlRepository;
11
use Chamilo\CoreBundle\Service\ScheduledAnnouncementService;
12
use Database;
13
use Doctrine\ORM\EntityManager;
14
use Symfony\Component\Console\Command\Command;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Console\Style\SymfonyStyle;
18
19
class SendScheduledAnnouncementsCommand extends Command
20
{
21
    protected static $defaultName = 'app:send-scheduled-announcements';
22
23
    public function __construct(
24
        private readonly AccessUrlRepository          $accessUrlRepository,
25
        private readonly ScheduledAnnouncementService $scheduledAnnouncementService,
26
        private readonly EntityManager $em
27
    ) {
28
        parent::__construct();
29
    }
30
31
    protected function configure(): void
32
    {
33
        $this
34
            ->setDescription('Send scheduled announcements to all users.')
35
            ->addOption('debug', null, null, 'If set, debug messages will be shown.');
36
    }
37
38
    protected function execute(InputInterface $input, OutputInterface $output): int
39
    {
40
        Database::setManager($this->em);
41
42
        $container = $this->getApplication()->getKernel()->getContainer();
43
        Container::setContainer($container);
44
45
        $io = new SymfonyStyle($input, $output);
46
        $debug = (bool) $input->getOption('debug');
47
        $urlList = $this->accessUrlRepository->findAll();
48
49
        if (empty($urlList)) {
50
            $io->warning('No access URLs found.');
51
            return Command::SUCCESS;
52
        }
53
54
        foreach ($urlList as $url) {
55
            $urlId = $url->getId();
56
            $io->writeln("Portal: #".$urlId." - ".$url->getUrl());
57
58
            try {
59
                $messagesSent = $this->scheduledAnnouncementService->sendPendingMessages($urlId, $debug);
60
                $io->writeln("Messages sent: $messagesSent");
61
62
                if ($debug) {
63
                    $io->writeln("Debug: Processed portal with ID ".$urlId);
64
                }
65
            } catch (\Exception $e) {
66
                $io->error('Error processing portal with ID ' . $urlId . ': ' . $e->getMessage());
67
                return Command::FAILURE;
68
            }
69
        }
70
71
        $io->success('All scheduled announcements have been sent.');
72
        return Command::SUCCESS;
73
    }
74
}
75