Completed
Push — master ( 4544d3...e2a407 )
by Dominik
01:29
created

DependencyLoader::exec()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types = 1);
2
3
4
namespace Dominikb\ComposerLicenseChecker;
5
6
7
use Dominikb\ComposerLicenseChecker\Contracts\DependencyLoader as DependencyLoaderContract;
8
9
class DependencyLoader implements DependencyLoaderContract
10
{
11
    const LINES_BEFORE_DEPENDENCY_VERSIONS = 6;
12
13
    public function loadDependencies(string $composer, string $project): array
14
    {
15
        $commandOutput = $this->runComposerLicenseCommand($composer, $project);
16
17
        $cleanOutput = $this->stripHeadersFromOutput($commandOutput);
18
19
        return $this->splitColumnsIntoDependencies($cleanOutput);
20
    }
21
22
    private function runComposerLicenseCommand(string $composer, string $project): array
23
    {
24
        $command = sprintf('%s -d %s licenses', $composer, $project);
25
26
        return $this->exec($command);
27
    }
28
29
    private function stripHeadersFromOutput(array $output): array
30
    {
31
        return array_slice($output, self::LINES_BEFORE_DEPENDENCY_VERSIONS - 1);
32
    }
33
34
    private function splitColumnsIntoDependencies(array $output): array
35
    {
36
        $parser = new DependencyParser;
37
38
        $mappedToObjects = [];
39
        foreach ($output as $dependency) {
40
            $mappedToObjects[] = $parser->parse($dependency);
41
        }
42
43
        return $mappedToObjects;
44
    }
45
46
    protected function exec(string $command)
47
    {
48
        exec($command, $output);
49
50
        return $output;
51
    }
52
}
53