Completed
Pull Request — master (#10)
by Tomáš
31:42
created

RunCommand::execute()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 10
cp 0
rs 9.3142
c 0
b 0
f 0
cc 2
eloc 12
nc 3
nop 2
crap 6
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\PHP7_CodeSniffer\Console\ExitCode;
19
use Throwable;
20
21
final class RunCommand extends Command
22
{
23
    /**
24
     * @var StyleInterface
25
     */
26
    private $style;
27
28
    /**
29
     * @var Application
30
     */
31
    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...
32
33
    /**
34
     * @var MultiCsFileLoader
35
     */
36
    private $multiCsFileLoader;
37
38
    public function __construct(Application $application, StyleInterface $style, MultiCsFileLoader $multiCsFileLoader)
39
    {
40
        parent::__construct();
41
42
        $this->application = $application;
43
        $this->style = $style;
44
        $this->multiCsFileLoader = $multiCsFileLoader;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    protected function configure()
51
    {
52
        $this->setName('run');
53
        $this->addArgument('source', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'The path(s) to be checked.');
54
        $this->addOption('fix', null, null, 'Fix found violations.');
55
        $this->setDescription('Check coding standard in one or more directories.');
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    protected function execute(InputInterface $input, OutputInterface $output)
62
    {
63
        try {
64
            $this->application->runCommand(
65
                $this->createRunApplicationCommandFromInput($input)
66
            );
67
68
            $this->style->success(
69
                sprintf(
70
                    'Sources "%s" were checked!',
71
                    implode(',', $input->getArgument('source'))
72
                )
73
            );
74
75
            return ExitCode::SUCCESS;
76
        } catch (Throwable $throwable) {
77
            $this->style->error($throwable->getMessage());
78
79
            return ExitCode::ERROR;
80
        }
81
    }
82
83
    private function createRunApplicationCommandFromInput(InputInterface $input) : RunApplicationCommand
84
    {
85
        return new RunApplicationCommand(
86
            $input->getArgument('source'),
87
            $input->getOption('fix'),
88
            $this->multiCsFileLoader->load()
89
        );
90
    }
91
}
92