Passed
Pull Request — master (#225)
by
unknown
06:11
created

AbstractCommand::getMigrationService()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
ccs 3
cts 4
cp 0.75
crap 2.0625
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
    /** @var OutputInterface $output */
21
    protected $output;
22
    /** @var OutputInterface $output */
23
    protected $errOutput;
24
    protected $verbosity = OutputInterface::VERBOSITY_NORMAL;
25
26
    /**
27
     * @return MigrationService
28
     */
29 101
    public function getMigrationService()
30
    {
31 101
        if (!$this->migrationService) {
32 101
            $this->migrationService = $this->getContainer()->get('ez_migration_bundle.migration_service');
33
        }
34
35
        return $this->migrationService;
36
    }
37
38 101
    protected function setOutput(OutputInterface $output)
39
    {
40 101
        $this->output = $output;
41 101
        $this->errOutput = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output;
42 101
    }
43
44 101
    protected function setVerbosity($verbosity)
45
    {
46 101
        $this->verbosity = $verbosity;
47 101
    }
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
     * @param int $type
56
     */
57
    protected function writeln($message, $verbosity = OutputInterface::VERBOSITY_NORMAL, $type = OutputInterface::OUTPUT_NORMAL)
58
    {
59
        if ($this->verbosity >= $verbosity) {
60
            $this->output->writeln($message, $type);
61
        }
62
    }
63
64
    /**
65
     * @param string|array $message The message as an array of lines or a single string
66
     * @param int $verbosity
67
     * @param int $type
68
     */
69
    protected function writeErrorln($message, $verbosity = OutputInterface::VERBOSITY_QUIET, $type = OutputInterface::OUTPUT_NORMAL)
70
    {
71
        if ($this->verbosity >= $verbosity) {
72
73
            // When verbosity is set to quiet, SF swallows the error message in the writeln call
74
            // (unlike for other verbosity levels, which are left for us to handle...)
75
            // We resort to a hackish workaround to _always_ print errors to stdout, even in quiet mode.
76
            // If the end user does not want any error echoed, he can just 2>/dev/null
77
            if ($this->errOutput->getVerbosity() == OutputInterface::VERBOSITY_QUIET) {
78
                $this->errOutput->setVerbosity(OutputInterface::VERBOSITY_NORMAL);
79
                $this->errOutput->writeln($message, $type);
80
                $this->errOutput->setVerbosity(OutputInterface::VERBOSITY_QUIET);
81
            }
82
            else
83
            {
84
                $this->errOutput->writeln($message, $type);
85
            }
86
        }
87
    }
88
}
89