Completed
Push — master ( 67a92e...b03a00 )
by Grégoire
01:54
created

src/Command/RestartCommand.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\NotificationBundle\Command;
15
16
use Sonata\NotificationBundle\Backend\BackendInterface;
17
use Sonata\NotificationBundle\Event\IterateEvent;
18
use Sonata\NotificationBundle\Iterator\ErroneousMessageIterator;
19
use Sonata\NotificationBundle\Model\MessageManagerInterface;
20
use Sonata\NotificationBundle\Selector\ErroneousMessagesSelector;
21
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
22
use Symfony\Component\Console\Input\InputInterface;
23
use Symfony\Component\Console\Input\InputOption;
24
use Symfony\Component\Console\Output\OutputInterface;
25
26
class RestartCommand extends ContainerAwareCommand
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function configure(): void
32
    {
33
        $this->setName('sonata:notification:restart');
34
        $this->setDescription('Restart messages with erroneous statuses, only for doctrine backends');
35
        $this->addOption('type', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'List of messages types to restart (separate multiple types with a space)');
36
        $this->addOption('max-attempts', null, InputOption::VALUE_REQUIRED, 'Maximum number of attempts', 6);
37
        $this->addOption('attempt-delay', null, InputOption::VALUE_OPTIONAL, 'Min seconds between two attempts', 10);
38
        $this->addOption('pulling', null, InputOption::VALUE_NONE, 'Run the command as an infinite pulling loop');
39
        $this->addOption('pause', null, InputOption::VALUE_OPTIONAL, 'Seconds between each data pull (used only when pulling option is set)', 500000);
40
        $this->addOption('batch-size', null, InputOption::VALUE_OPTIONAL, 'Number of message to process on each pull (used only when pulling option is set)', 10);
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function execute(InputInterface $input, OutputInterface $output): void
47
    {
48
        $output->writeln('<info>Starting... </info>');
49
50
        if (!is_numeric($input->getOption('max-attempts'))) {
51
            throw new \Exception('Option "max-attempts" is invalid (integer value needed).');
52
        }
53
54
        $pullMode = $input->getOption('pulling');
55
        $manager = $this->getMessageManager();
56
57
        if ($pullMode) {
58
            $messages = new ErroneousMessageIterator(
59
                $manager,
60
                $input->getOption('type'),
61
                $input->getOption('pause'),
62
                $input->getOption('batch-size'),
63
                $input->getOption('max-attempts'),
64
                $input->getOption('attempt-delay'));
65
        } else {
66
            $messages = $this->getErroneousMessageSelector()->getMessages(
67
                $input->getOption('type'),
68
                $input->getOption('max-attempts')
69
            );
70
        }
71
72
        if (0 == \count($messages)) {
73
            $output->writeln('Nothing to restart, bye.');
74
75
            return;
76
        }
77
78
        $eventDispatcher = $this->getContainer()->get('event_dispatcher');
79
80
        foreach ($messages as $message) {
81
            $id = $message->getId();
82
83
            $newMessage = $manager->restart($message);
84
85
            $this->getBackend()->publish($newMessage);
86
87
            $output->writeln(sprintf(
88
                'Reset Message %s <info>#%d</info>, new id %d. Attempt #%d',
89
                $newMessage->getType(),
90
                $id,
91
                $newMessage->getId(),
92
                $newMessage->getRestartCount()
93
            ));
94
95
            if ($pullMode) {
96
                $eventDispatcher->dispatch(IterateEvent::EVENT_NAME, new IterateEvent($messages, null, $newMessage));
0 ignored issues
show
It seems like $messages defined by $this->getErroneousMessa...Option('max-attempts')) on line 66 can also be of type array; however, Sonata\NotificationBundl...ateEvent::__construct() does only seem to accept object<Sonata\Notificati...ssageIteratorInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
97
            }
98
        }
99
100
        $output->writeln('<info>Done!</info>');
101
    }
102
103
    /**
104
     * Return the erroneous message selector service.
105
     *
106
     * @return ErroneousMessagesSelector
107
     */
108
    protected function getErroneousMessageSelector()
109
    {
110
        return $this->getContainer()->get('sonata.notification.erroneous_messages_selector');
111
    }
112
113
    /**
114
     * @return MessageManagerInterface
115
     */
116
    protected function getMessageManager()
117
    {
118
        return $this->getContainer()->get('sonata.notification.manager.message');
119
    }
120
121
    /**
122
     * @return BackendInterface
123
     */
124
    protected function getBackend()
125
    {
126
        return $this->getContainer()->get('sonata.notification.backend');
127
    }
128
}
129