Passed
Pull Request — master (#238)
by Théo
02:38
created

Diff::configure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 22
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\Box\Console\Command;
16
17
use Exception;
18
use KevinGH\Box\Json\JsonValidationException;
19
use ParagonIE\Pharaoh\Pharaoh;
20
use ParagonIE\Pharaoh\PharDiff;
21
use Symfony\Component\Console\Command\Command;
22
use Symfony\Component\Console\Exception\RuntimeException;
23
use Symfony\Component\Console\Input\InputArgument;
24
use Symfony\Component\Console\Input\InputInterface;
25
use Symfony\Component\Console\Input\InputOption;
26
use Symfony\Component\Console\Output\OutputInterface;
27
28
/**
29
 * @private
30
 */
31
final class Diff extends Command
32
{
33
    private const FIRST_PHAR_ARG = 'pharA';
34
    private const SECOND_PHAR_ARG = 'pharB';
35
36
    private const GNU_DIFF_OPTION = 'gnu-diff';
37
    private const CHECK_OPTION = 'check';
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    protected function configure(): void
43
    {
44
        parent::configure();
45
46
        $this->setName('diff');
47
        $this->setDescription('Display the differences between all of the files in two PHARs');
48
49
        $this->addArgument(
50
            self::FIRST_PHAR_ARG,
51
            InputArgument::OPTIONAL,
52
            'The first PHAR'
53
        );
54
        $this->addArgument(
55
            self::SECOND_PHAR_ARG,
56
            InputArgument::OPTIONAL,
57
            'The second PHAR'
58
        );
59
60
        $this->addOption(
61
            self::GNU_DIFF_OPTION,
62
            'd',
63
            InputOption::VALUE_NONE,
64
            'Displays a GNU diff instead of the default git diff'
65
        );
66
        $this->addOption(
67
            self::CHECK_OPTION,
68
            'c',
69
            InputOption::VALUE_OPTIONAL,
70
            'Verify the authenticity of the contents between the two PHARs with the given hash function.',
71
            'sha384'
72
        );
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    protected function execute(InputInterface $input, OutputInterface $output): int
79
    {
80
        $phars = [
81
            new Pharaoh($input->getArgument(self::FIRST_PHAR_ARG)),
82
            new Pharaoh($input->getArgument(self::SECOND_PHAR_ARG)),
83
        ];
84
85
        $diff = new PharDiff(...$phars);
86
87
        if ($output->isVerbose()) {
88
            $diff->setVerbose(true);
89
        }
90
91
        if ($input->hasParameterOption(['-c', '--check'])) {
92
            return $diff->listChecksums($input->getOption(self::CHECK_OPTION) ?? 'sha384');
93
        }
94
95
        if ($input->getOption(self::GNU_DIFF_OPTION)) {
96
            return $diff->printGnuDiff();
97
        }
98
99
        return $diff->printGitDiff();
100
    }
101
}
102