Passed
Push — master ( 1b2b6b...36c408 )
by Dominik
10:28 queued 07:57
created

DependencyLoader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 61.53%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
eloc 11
c 2
b 0
f 0
dl 0
loc 44
ccs 8
cts 13
cp 0.6153
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A runComposerLicenseCommand() 0 5 1
A loadDependencies() 0 5 1
A __construct() 0 3 1
A exec() 0 9 2
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 2
    public function __construct(DependencyParser $dependencyParser)
20
    {
21 2
        $this->dependencyParser = $dependencyParser;
22
    }
23
24
    /**
25
     * @throws CommandExecutionException
26
     */
27 2
    public function loadDependencies(string $composer, string $project): array
28
    {
29 2
        $commandOutput = $this->runComposerLicenseCommand($composer, $project);
30
31 1
        return $this->dependencyParser->parse(join(PHP_EOL, $commandOutput));
32
    }
33
34
    /**
35
     * @throws CommandExecutionException
36
     */
37 2
    private function runComposerLicenseCommand(string $composer, string $project): array
38
    {
39 2
        $command = sprintf('%s licenses --format json --working-dir %s', escapeshellarg($composer), escapeshellarg($project));
40
41 2
        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