Completed
Push — 2.x-dev-kit ( 8d77e1 )
by
unknown
28:22 queued 25:50
created

RestartCommand::getBackend()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata project.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\NotificationBundle\Command;
13
14
use Sonata\NotificationBundle\Backend\BackendInterface;
15
use Sonata\NotificationBundle\Event\IterateEvent;
16
use Sonata\NotificationBundle\Iterator\ErroneousMessageIterator;
17
use Sonata\NotificationBundle\Model\MessageManagerInterface;
18
use Sonata\NotificationBundle\Selector\ErroneousMessagesSelector;
19
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Input\InputOption;
22
use Symfony\Component\Console\Output\OutputInterface;
23
24
class RestartCommand extends ContainerAwareCommand
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function configure()
30
    {
31
        $this->setName('sonata:notification:restart');
32
        $this->setDescription('Restart messages with erroneous statuses, only for doctrine backends');
33
        $this->addOption('type', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'List of messages types to restart (separate multiple types with a space)');
34
        $this->addOption('max-attempts', null, InputOption::VALUE_REQUIRED, 'Maximum number of attempts', 6);
35
        $this->addOption('attempt-delay', null, InputOption::VALUE_OPTIONAL, 'Min seconds between two attempts', 10);
36
        $this->addOption('pulling', null, InputOption::VALUE_NONE, 'Run the command as an infinite pulling loop');
37
        $this->addOption('pause', null, InputOption::VALUE_OPTIONAL, 'Seconds between each data pull (used only when pulling option is set)', 500000);
38
        $this->addOption('batch-size', null, InputOption::VALUE_OPTIONAL, 'Number of message to process on each pull (used only when pulling option is set)', 10);
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function execute(InputInterface $input, OutputInterface $output)
45
    {
46
        $output->writeln('<info>Starting... </info>');
47
48
        if (!is_numeric($input->getOption('max-attempts'))) {
49
            throw new \Exception('Option "max-attempts" is invalid (integer value needed).');
50
        }
51
52
        $pullMode = $input->getOption('pulling');
53
        $manager = $this->getMessageManager();
54
55
        if ($pullMode) {
56
            $messages = new ErroneousMessageIterator(
57
                $manager,
58
                $input->getOption('type'),
0 ignored issues
show
Documentation introduced by
$input->getOption('type') is of type integer|double|string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
59
                $input->getOption('pause'),
60
                $input->getOption('batch-size'),
61
                $input->getOption('max-attempts'),
62
                $input->getOption('attempt-delay'));
63
        } else {
64
            $messages = $this->getErroneousMessageSelector()->getMessages($input->getOption('type'), $input->getOption('max-attempts'));
0 ignored issues
show
Documentation introduced by
$input->getOption('type') is of type integer|double|string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
65
        }
66
67
        if (0 == count($messages)) {
68
            $output->writeln('Nothing to restart, bye.');
69
70
            return;
71
        }
72
73
        $eventDispatcher = $this->getContainer()->get('event_dispatcher');
74
75
        foreach ($messages as $message) {
76
            $id = $message->getId();
77
78
            $newMessage = $manager->restart($message);
79
80
            $this->getBackend()->publish($newMessage);
81
82
            $output->writeln(sprintf('Reset Message %s <info>#%d</info>, new id %d. Attempt #%d', $newMessage->getType(), $id, $newMessage->getId(), $newMessage->getRestartCount()));
83
84
            if ($pullMode) {
85
                $eventDispatcher->dispatch(IterateEvent::EVENT_NAME, new IterateEvent($messages, null, $newMessage));
0 ignored issues
show
Bug introduced by
It seems like $messages defined by $this->getErroneousMessa...Option('max-attempts')) on line 64 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...
86
            }
87
        }
88
89
        $output->writeln('<info>Done!</info>');
90
    }
91
92
    /**
93
     * Return the erroneous message selector service.
94
     *
95
     * @return ErroneousMessagesSelector
96
     */
97
    protected function getErroneousMessageSelector()
98
    {
99
        return $this->getContainer()->get('sonata.notification.erroneous_messages_selector');
100
    }
101
102
    /**
103
     * @return MessageManagerInterface
104
     */
105
    protected function getMessageManager()
106
    {
107
        return $this->getContainer()->get('sonata.notification.manager.message');
108
    }
109
110
    /**
111
     * @return BackendInterface
112
     */
113
    protected function getBackend()
114
    {
115
        return $this->getContainer()->get('sonata.notification.backend');
116
    }
117
}
118