Passed
Push — master ( 648c95...abce15 )
by Gaetano
07:02
created

AbstractCommand::writeErrorln()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 2
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Command;
4
5
use Symfony\Component\Console\Output\OutputInterface;
6
use Symfony\Component\Console\Output\ConsoleOutputInterface;
7
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
8
use Kaliop\eZMigrationBundle\Core\MigrationService;
9
10
/**
11
 * Base command class that all migration commands extend from.
12
 */
13
abstract class AbstractCommand extends ContainerAwareCommand
14
{
15
    /**
16
     * @var MigrationService
17
     */
18
    private $migrationService;
19
20 78
    /** @var OutputInterface $output */
21
    protected $output;
22 78
    /** @var OutputInterface $output */
23 78
    protected $errOutput;
24
    protected $verbosity = OutputInterface::VERBOSITY_NORMAL;
25
26 78
    /**
27
     * @return MigrationService
28
     */
29
    public function getMigrationService()
30
    {
31
        if (!$this->migrationService) {
32
            $this->migrationService = $this->getContainer()->get('ez_migration_bundle.migration_service');
33
        }
34
35
        return $this->migrationService;
36
    }
37
38
    protected function setOutput(OutputInterface $output)
39
    {
40
        $this->output = $output;
41
        $this->errOutput = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output;
42
    }
43
44
    protected function setVerbosity($verbosity)
45
    {
46
        $this->verbosity = $verbosity;
47
    }
48
49
    /**
50
     * Small trick to allow us to:
51
     * - lower verbosity between NORMAL and QUIET
52
     * - have a decent writeln API, even with old SF versions
53
     * @param string|array $message The message as an array of lines or a single string
54
     * @param int $verbosity
55
     */
56
    protected function writeln($message, $verbosity = OutputInterface::VERBOSITY_NORMAL)
57
    {
58
        if ($this->verbosity >= $verbosity) {
59
            $this->output->writeln($message);
60
        }
61
    }
62
63
    protected function writeErrorln($message, $verbosity = OutputInterface::VERBOSITY_NORMAL)
64
    {
65
        if ($this->verbosity >= $verbosity) {
66
            $this->errOutput->writeln($message);
67
        }
68
    }
69
}
70