Completed
Pull Request — master (#15)
by Tomáš
03:22
created

RunCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 12
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 4
crap 2
1
<?php
2
3
/*
4
 * This file is part of Symplify
5
 * Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz).
6
 */
7
8
namespace Symplify\MultiCodingStandard\Console\Command;
9
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Input\InputArgument;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Symfony\Component\Console\Style\StyleInterface;
15
use Symplify\MultiCodingStandard\Application\Application;
16
use Symplify\MultiCodingStandard\Application\Command\RunApplicationCommand;
17
use Symplify\MultiCodingStandard\Configuration\MultiCsFileLoader;
18
use Symplify\MultiCodingStandard\Console\Output\InfoMessagePrinter;
19
use Symplify\PHP7_CodeSniffer\Console\ExitCode;
20
use Throwable;
21
22
final class RunCommand extends Command
23
{
24
    /**
25
     * @var StyleInterface
26
     */
27
    private $style;
28
29
    /**
30
     * @var Application
31
     */
32
    private $application;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
33
34
    /**
35
     * @var MultiCsFileLoader
36
     */
37
    private $multiCsFileLoader;
38
39
    /**
40
     * @var InfoMessagePrinter
41
     */
42
    private $infoMessagePrinter;
43
44
    public function __construct(
45
        Application $application,
46
        StyleInterface $style,
47
        MultiCsFileLoader $multiCsFileLoader,
48
        InfoMessagePrinter $infoMessagePrinter
49
    ) {
50
        parent::__construct();
51
52
        $this->application = $application;
53
        $this->style = $style;
54
        $this->multiCsFileLoader = $multiCsFileLoader;
55
        $this->infoMessagePrinter = $infoMessagePrinter;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    protected function configure()
62
    {
63
        $this->setName('run');
64
        $this->addArgument('source', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'The path(s) to be checked.');
65
        $this->addOption('fix', null, null, 'Fix found violations.');
66
        $this->setDescription('Check coding standard in one or more directories.');
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    protected function execute(InputInterface $input, OutputInterface $output)
73
    {
74
        try {
75
            $this->application->runCommand(
76
                $this->createRunApplicationCommandFromInput($input)
77
            );
78
79
            if ($this->infoMessagePrinter->hasSomeErrorMessages()) {
80
                $this->infoMessagePrinter->printFoundErrorsStatus($input->getOption('fix'));
81
82
                return ExitCode::ERROR;
83
            }
84
85
            $this->style->success(
86
                sprintf(
87
                    'Sources "%s" were checked!',
88
                    implode(',', $input->getArgument('source'))
89
                )
90
            );
91
92
            return ExitCode::SUCCESS;
93
        } catch (Throwable $throwable) {
94
            if ($throwable->getMessage()) {
95
                $this->style->error($throwable->getMessage());
96
            }
97
98
            return ExitCode::ERROR;
99
        }
100
    }
101
102
    private function createRunApplicationCommandFromInput(InputInterface $input) : RunApplicationCommand
103
    {
104
        return new RunApplicationCommand(
105
            $input->getArgument('source'),
106
            $input->getOption('fix'),
107
            $this->multiCsFileLoader->load()
108
        );
109
    }
110
}
111