Completed
Push — master ( 7195f8...c1e355 )
by Bill
10s
created

ChurnCommand::getProcessResults()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 8.7624
c 0
b 0
f 0
cc 5
eloc 13
nc 6
nop 0
1
<?php declare(strict_types = 1);
2
3
namespace Churn\Commands;
4
5
use Churn\Factories\ResultsRendererFactory;
6
use Churn\Logic\ResultsLogic;
7
use Churn\Managers\ProcessManager;
8
use Churn\Factories\ProcessFactory;
9
use Churn\Managers\FileManager;
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\Input\InputOption;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Churn\Configuration\Config;
16
use Symfony\Component\Yaml\Yaml;
17
use InvalidArgumentException;
18
19
class ChurnCommand extends Command
20
{
21
    /**
22
     * The results logic.
23
     * @var ResultsLogic
24
     */
25
    private $resultsLogic;
26
27
    /**
28
     * The process manager.
29
     * @var ProcessManager
30
     */
31
    private $processManager;
32
33
    /**
34
     * The renderer factory.
35
     * @var ResultsRendererFactory
36
     */
37
    private $renderer;
38
39
    /**
40
     * ChurnCommand constructor.
41
     * @param ResultsLogic           $resultsLogic   The results logic.
42
     * @param ProcessManager         $processManager The process manager.
43
     * @param ResultsRendererFactory $renderer       The Results Renderer.
44
     */
45
    public function __construct(ResultsLogic $resultsLogic, ProcessManager $processManager, ResultsRendererFactory $renderer)
46
    {
47
        parent::__construct();
48
        $this->resultsLogic = $resultsLogic;
49
        $this->processManager = $processManager;
50
        $this->renderer = $renderer;
51
    }
52
53
    /**
54
     * Configure the command
55
     * @return void
56
     */
57
    protected function configure()
58
    {
59
        $this->setName('run')
60
            ->addArgument('paths', InputArgument::IS_ARRAY, 'Path to source to check.')
61
            ->addOption('configuration', 'c', InputOption::VALUE_OPTIONAL, 'Path to the configuration file', 'churn.yml')  // @codingStandardsIgnoreLine
62
            ->addOption('format', null, InputOption::VALUE_REQUIRED, 'The output format to use', 'text')
63
            ->setDescription('Check files')
64
            ->setHelp('Checks the churn on the provided path argument(s).');
65
    }
66
67
    /**
68
     * Execute the command
69
     * @param  InputInterface  $input  Input.
70
     * @param  OutputInterface $output Output.
71
     * @return void
72
     */
73
    protected function execute(InputInterface $input, OutputInterface $output)
74
    {
75
        $config = Config::create(Yaml::parse(@file_get_contents($input->getOption('configuration'))) ?? []);
76
77
        $filesCollection = (new FileManager($config->getFileExtensions(), $config->getFilesToIgnore()))->getPhpFiles($this->getDirectoriesToScan($input, $config->getDirectoriesToScan()));
78
79
        $completedProcesses = $this->processManager->process(
80
            $filesCollection,
81
            new ProcessFactory($config->getCommitsSince()),
82
            $config->getParallelJobs()
83
        );
84
85
        $resultCollection = $this->resultsLogic->process($completedProcesses, $config->getMinScoreToShow(), $config->getFilesToShow());
86
        $this->renderer->renderResults($input->getOption('format'), $output, $resultCollection);
87
    }
88
89
    /**
90
     * Get the directories to scan.
91
     * @param InputInterface $input          Input Interface.
92
     * @param array          $dirsConfigured The directories configured to scan.
93
     * @throws InvalidArgumentException If paths argument invalid.
94
     * @return array When no directories to scan found.
95
     */
96
    private function getDirectoriesToScan(InputInterface $input, array $dirsConfigured): array
97
    {
98
        $dirsProvidedAsArgs = $input->getArgument('paths');
99
        if (count($dirsProvidedAsArgs) > 0) {
100
            return $dirsProvidedAsArgs;
101
        }
102
103
        if (count($dirsConfigured) > 0) {
104
            return $dirsConfigured;
105
        }
106
107
        throw new InvalidArgumentException(
108
            'Provide the directories you want to scan as arguments, ' .
109
            'or configure them under "directoriesToScan" in your churn.yml file.'
110
        );
111
    }
112
}
113