Passed
Pull Request — master (#46)
by Théo
02:11
created

Verify::execute()   C

Complexity

Conditions 8
Paths 54

Size

Total Lines 66
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 66
rs 6.7081
c 0
b 0
f 0
cc 8
eloc 38
nc 54
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Assert\Assertion;
18
use KevinGH\Box\Signature;
19
use Phar;
20
use Symfony\Component\Console\Command\Command;
21
use Symfony\Component\Console\Input\InputArgument;
22
use Symfony\Component\Console\Input\InputInterface;
23
use Symfony\Component\Console\Output\OutputInterface;
24
use Symfony\Component\Console\Style\SymfonyStyle;
25
use Throwable;
26
27
final class Verify extends Command
28
{
29
    private const PHAR_ARG = 'phar';
30
    private const VERBOSITY_LEVEL = OutputInterface::VERBOSITY_VERBOSE;
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    protected function configure(): void
36
    {
37
        $this->setName('verify');
38
        $this->setDescription('Verifies the PHAR signature');
39
        $this->setHelp(
40
            <<<'HELP'
41
The <info>%command.name%</info> command will verify the signature of the PHAR.
42
43
<question>Why would I require that box handle the verification process?</question>
44
45
If you meet all of the following conditions:
46
 - The <comment>openssl</comment> extension is not installed
47
 - You need to verify a PHAR signed using a private key
48
49
Box supports verifying private key signed PHARs without using
50
either extensions. <error>Note however, that the entire PHAR will need
51
to be read into memory before the verification can be performed.</error>
52
HELP
53
        );
54
        $this->addArgument(
55
            self::PHAR_ARG,
56
            InputArgument::REQUIRED,
57
            'The PHAR file'
58
        );
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    protected function execute(InputInterface $input, OutputInterface $output): int
65
    {
66
        $io = new SymfonyStyle($input, $output);
67
68
        $pharPath = $input->getArgument(self::PHAR_ARG);
69
70
        Assertion::file($pharPath);
71
72
        $pharPath = false !== realpath($pharPath) ? realpath($pharPath) : $pharPath;
73
74
        $io->writeln(
75
            sprintf(
76
                'Verifying the PHAR "<comment>%s</comment>"...',
77
                $pharPath
78
            ),
79
            self::VERBOSITY_LEVEL
80
        );
81
82
        try {
83
            $phar = new Phar($pharPath);
84
85
            $verified = true;
86
            $signature = $phar->getSignature();
87
        } catch (Throwable $throwable) {
88
            // Continue
89
90
            $verified = false;
91
            $signature = null;
92
        }
93
94
        if (false === $verified) {
95
            $message = isset($throwable) && '' !== $throwable->getMessage()
96
                ? $throwable->getMessage()
97
                : 'Unknown reason.'
98
            ;
99
100
            $io->writeln(
101
                sprintf(
102
                    '<error>The PHAR failed the verification: %s</error>',
103
                    $message
104
                )
105
            );
106
107
            if ($output->isVerbose() && isset($throwable)) {
108
                throw $throwable;
109
            }
110
111
            return 1;
112
        }
113
114
        $io->writeln('<info>The PHAR passed verification.</info>');
115
116
        $io->writeln(
117
            '',
118
            self::VERBOSITY_LEVEL
119
        );
120
        $io->writeln(
121
            sprintf(
122
                '%s signature: <info>%s</info>',
123
                $signature['hash_type'],
124
                $signature['hash']
125
            ),
126
            self::VERBOSITY_LEVEL
127
        );
128
129
        return 0;
130
    }
131
}
132