Passed
Push — master ( ed42e4...b3b06a )
by Kirill
04:44
created

SequenceCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B runSequence() 0 25 7
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Console\Command;
13
14
use Psr\Container\ContainerInterface;
15
use Spiral\Console\Command;
16
use Spiral\Console\SequenceInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Throwable;
19
20
abstract class SequenceCommand extends Command
21
{
22
    public const    OPTIONS = [
23
        ['ignore', 'i', InputOption::VALUE_NONE, 'Ignore any errors'],
24
        ['break', 'b', InputOption::VALUE_NONE, 'Break on first error, works if ignore is disabled']
25
    ];
26
27
    /**
28
     * @param iterable|SequenceInterface[] $commands
29
     * @param ContainerInterface           $container
30
     * @return int
31
     */
32
    protected function runSequence(iterable $commands, ContainerInterface $container): int
33
    {
34
        $errors = 0;
35
        foreach ($commands as $sequence) {
36
            $sequence->writeHeader($this->output);
37
38
            try {
39
                $sequence->execute($container, $this->output);
40
                $sequence->whiteFooter($this->output);
41
            } catch (Throwable $e) {
42
                $errors++;
43
                $this->sprintf("<error>%s</error>\n", $e);
44
                if (!$this->option('ignore') && $this->option('break')) {
45
                    $this->writeln('<fg=red>Aborting.</fg=red>');
46
47
                    return 1;
48
                }
49
            }
50
51
            $this->writeln('');
52
        }
53
54
        $this->writeln('<info>All done!</info>');
55
56
        return ($errors && !$this->option('ignore')) ? 1 : 0;
57
    }
58
}
59