1 | <?php declare(strict_types = 1); |
||
23 | class ChurnCommand extends Command |
||
24 | { |
||
25 | /** |
||
26 | * The results logic. |
||
27 | * @var ResultsLogic |
||
28 | */ |
||
29 | private $resultsLogic; |
||
30 | |||
31 | /** |
||
32 | * The process manager. |
||
33 | * @var ProcessManager |
||
34 | */ |
||
35 | private $processManager; |
||
36 | |||
37 | /** |
||
38 | * The renderer factory. |
||
39 | * @var ResultsRendererFactory |
||
40 | */ |
||
41 | private $renderFactory; |
||
42 | |||
43 | /** |
||
44 | * ChurnCommand constructor. |
||
45 | * @param ResultsLogic $resultsLogic The results logic. |
||
46 | * @param ProcessManager $processManager The process manager. |
||
47 | * @param ResultsRendererFactory $renderFactory The Results Renderer Factory. |
||
48 | */ |
||
49 | public function __construct( |
||
50 | ResultsLogic $resultsLogic, |
||
51 | ProcessManager $processManager, |
||
52 | ResultsRendererFactory $renderFactory |
||
53 | ) { |
||
54 | parent::__construct(); |
||
55 | $this->resultsLogic = $resultsLogic; |
||
56 | $this->processManager = $processManager; |
||
57 | $this->renderFactory = $renderFactory; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Configure the command |
||
62 | * @return void |
||
63 | */ |
||
64 | protected function configure(): void |
||
65 | { |
||
66 | $this->setName('run') |
||
67 | ->addArgument('paths', InputArgument::IS_ARRAY, 'Path to source to check.') |
||
68 | ->addOption('configuration', 'c', InputOption::VALUE_OPTIONAL, 'Path to the configuration file', 'churn.yml') // @codingStandardsIgnoreLine |
||
69 | ->addOption('format', null, InputOption::VALUE_REQUIRED, 'The output format to use', 'text') |
||
70 | ->setDescription('Check files') |
||
71 | ->setHelp('Checks the churn on the provided path argument(s).'); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Execute the command |
||
76 | * @param InputInterface $input Input. |
||
77 | * @param OutputInterface $output Output. |
||
78 | * @return void |
||
79 | */ |
||
80 | protected function execute(InputInterface $input, OutputInterface $output): void |
||
81 | { |
||
82 | $content = (string) @file_get_contents($input->getOption('configuration')); |
||
83 | $config = Config::create(Yaml::parse($content) ?? []); |
||
84 | |||
85 | $paths = $this->getDirectoriesToScan($input, $config->getDirectoriesToScan()); |
||
86 | |||
87 | $completedProcesses = $this->processManager->process( |
||
88 | $this->createFileCollection($config, $paths), |
||
89 | new ProcessFactory($config->getCommitsSince()), |
||
90 | $config->getParallelJobs() |
||
91 | ); |
||
92 | |||
93 | $resultCollection = $this->resultsLogic->process( |
||
94 | $completedProcesses, |
||
95 | $config->getMinScoreToShow(), |
||
96 | $config->getFilesToShow() |
||
97 | ); |
||
98 | |||
99 | $renderer = $this->renderFactory->getRenderer($input->getOption('format')); |
||
100 | $renderer->render($output, $resultCollection); |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Get the directories to scan. |
||
105 | * @param InputInterface $input Input Interface. |
||
106 | * @param array $dirsConfigured The directories configured to scan. |
||
107 | * @throws InvalidArgumentException If paths argument invalid. |
||
108 | * @return array When no directories to scan found. |
||
109 | */ |
||
110 | private function getDirectoriesToScan(InputInterface $input, array $dirsConfigured): array |
||
126 | |||
127 | /** |
||
128 | * Creates a file collection. |
||
129 | * @param Config $config The configuration. |
||
130 | * @param string[] $paths An array of paths. |
||
131 | * @return FileCollection |
||
132 | */ |
||
133 | protected function createFileCollection(Config $config, array $paths): FileCollection |
||
142 | } |
||
143 |