DependencyGuardCommand   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 31
c 2
b 0
f 0
dl 0
loc 79
ccs 30
cts 30
cp 1
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A execute() 0 14 2
A configure() 0 22 1
1
<?php
2
/**
3
 * Copyright MediaCT. All rights reserved.
4
 * https://www.mediact.nl
5
 */
6
7
namespace Mediact\DependencyGuard\Composer\Command;
8
9
use Composer\Command\BaseCommand;
10
use Mediact\DependencyGuard\Composer\Command\Exporter\ViolationExporterFactory;
11
use Mediact\DependencyGuard\Composer\Command\Exporter\ViolationExporterFactoryInterface;
12
use Mediact\DependencyGuard\DependencyGuardFactory;
13
use Mediact\DependencyGuard\DependencyGuardFactoryInterface;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Input\InputOption;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
class DependencyGuardCommand extends BaseCommand
19
{
20
    public const EXIT_NO_VIOLATIONS = 0;
21
    public const EXIT_VIOLATIONS    = 1;
22
23
    /** @var DependencyGuardFactoryInterface */
24
    private $guardFactory;
25
26
    /** @var ViolationExporterFactoryInterface */
27
    private $exporterFactory;
28
29
    /**
30
     * Constructor.
31
     *
32
     * @param DependencyGuardFactoryInterface|null   $guardFactory
33
     * @param ViolationExporterFactoryInterface|null $exporterFactory
34
     */
35 1
    public function __construct(
36
        DependencyGuardFactoryInterface $guardFactory = null,
37
        ViolationExporterFactoryInterface $exporterFactory = null
38
    ) {
39 1
        $this->guardFactory    = $guardFactory ?? new DependencyGuardFactory();
40 1
        $this->exporterFactory = $exporterFactory ?? new ViolationExporterFactory();
41
42 1
        parent::__construct();
43 1
    }
44
45
    /**
46
     * Configure the command.
47
     *
48
     * @return void
49
     */
50 1
    protected function configure(): void
51
    {
52 1
        $this->setName('dependency-guard');
53 1
        $this->setDescription(
54 1
            'Check Composer dependencies for a --no-dev install.'
55
        );
56
57 1
        $this->addOption(
58 1
            'format',
59 1
            'f',
60 1
            InputOption::VALUE_REQUIRED,
61
            'The output format. '
62 1
                . implode(
63 1
                    ', ',
64 1
                    array_map(
65
                        function (string $format) : string {
66 1
                            return sprintf('<comment>%s</comment>', $format);
67 1
                        },
68 1
                        $this->exporterFactory->getOutputFormats()
69
                    )
70
                ),
71 1
            ViolationExporterFactoryInterface::DEFAULT_FORMAT
72
        );
73 1
    }
74
75
    /**
76
     * Execute the command.
77
     *
78
     * @param InputInterface  $input
79
     * @param OutputInterface $output
80
     *
81
     * @return int
82
     */
83 3
    protected function execute(
84
        InputInterface $input,
85
        OutputInterface $output
86
    ): int {
87 3
        $composer   = $this->getComposer(true);
88 3
        $guard      = $this->guardFactory->create();
89 3
        $violations = $guard->determineViolations($composer);
90 3
        $exporter   = $this->exporterFactory->create($input, $output);
91
92 3
        $exporter->export($violations);
93
94 3
        return count($violations) > 0
95 2
            ? static::EXIT_VIOLATIONS
96 3
            : static::EXIT_NO_VIOLATIONS;
97
    }
98
}
99