DependencyLoader::exec()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 4
c 2
b 0
f 0
dl 0
loc 9
ccs 0
cts 5
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Dominikb\ComposerLicenseChecker;
6
7
use Dominikb\ComposerLicenseChecker\Contracts\DependencyLoader as DependencyLoaderContract;
8
use Dominikb\ComposerLicenseChecker\Contracts\DependencyParser;
9
use Dominikb\ComposerLicenseChecker\Exceptions\CommandExecutionException;
10
use Symfony\Component\Console\Command\Command;
11
12
class DependencyLoader implements DependencyLoaderContract
13
{
14
    /**
15
     * @var DependencyParser
16
     */
17
    private $dependencyParser;
18
19 3
    public function __construct(DependencyParser $dependencyParser)
20
    {
21 3
        $this->dependencyParser = $dependencyParser;
22
    }
23
24
    /**
25
     * @throws CommandExecutionException
26
     */
27 3
    public function loadDependencies(string $composer, string $project, bool $withoutDev): array
28
    {
29 3
        $commandOutput = $this->runComposerLicenseCommand($composer, $project, $withoutDev);
30
31 2
        return $this->dependencyParser->parse(join(PHP_EOL, $commandOutput));
32
    }
33
34
    /**
35
     * @throws CommandExecutionException
36
     */
37 3
    private function runComposerLicenseCommand(string $composer, string $project, bool $withoutDev): array
38
    {
39 3
        $command = sprintf('%s licenses%s --format json --working-dir %s', escapeshellarg($composer), $withoutDev ? ' --no-dev' : '', escapeshellarg($project));
40
41 3
        return $this->exec($command);
42
    }
43
44
    /**
45
     * @throws CommandExecutionException
46
     */
47
    protected function exec(string $command): array
48
    {
49
        exec($command, $output, $exitCode);
50
51
        if ($exitCode !== 0) {
52
            throw new CommandExecutionException('Error when trying to fetch licenses from Composer', Command::INVALID);
53
        }
54
55
        return $output;
56
    }
57
}
58