Passed
Push — master ( d13433...bd569e )
by Théo
02:47
created

Extract::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 15
rs 9.9
c 0
b 0
f 0
cc 1
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 KevinGH\Box\Box;
18
use function KevinGH\Box\bump_open_file_descriptor_limit;
19
use KevinGH\Box\Console\IO\IO;
20
use function KevinGH\Box\create_temporary_phar;
21
use function KevinGH\Box\FileSystem\dump_file;
22
use function KevinGH\Box\FileSystem\remove;
23
use PharFileInfo;
24
use function realpath;
25
use RuntimeException;
26
use function sprintf;
27
use Symfony\Component\Console\Exception\RuntimeException as ConsoleRuntimeException;
28
use Symfony\Component\Console\Input\InputArgument;
29
use Throwable;
30
31
/**
32
 * @private
33
 */
34
final class Extract extends BaseCommand
35
{
36
    private const PHAR_ARG = 'phar';
37
    private const OUTPUT_ARG = 'output';
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    protected function configure(): void
43
    {
44
        $this->setName('extract');
45
        $this->setDescription(
46
            '🚚  Extracts a given PHAR into a directory'
47
        );
48
        $this->addArgument(
49
            self::PHAR_ARG,
50
            InputArgument::REQUIRED,
51
            'The PHAR file.'
52
        );
53
        $this->addArgument(
54
            self::OUTPUT_ARG,
55
            InputArgument::REQUIRED,
56
            'The output directory'
57
        );
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    protected function executeCommand(IO $io): int
64
    {
65
        $input = $io->getInput();
66
67
        $file = realpath($input->getArgument(self::PHAR_ARG));
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument(self::PHAR_ARG) can also be of type string[]; however, parameter $path of realpath() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

67
        $file = realpath(/** @scrutinizer ignore-type */ $input->getArgument(self::PHAR_ARG));
Loading history...
68
69
        if (false === $file) {
70
            $io->error(
71
                sprintf(
72
                    'The file "%s" could not be found.',
73
                    $input->getArgument(self::PHAR_ARG)
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument(self::PHAR_ARG) can also be of type string[]; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
                    /** @scrutinizer ignore-type */ $input->getArgument(self::PHAR_ARG)
Loading history...
74
                )
75
            );
76
77
            return 1;
78
        }
79
80
        $tmpFile = create_temporary_phar($file);
81
82
        try {
83
            $box = Box::create($tmpFile);
84
        } catch (Throwable $throwable) {
85
            if ($io->isDebug()) {
86
                throw new ConsoleRuntimeException(
87
                    'The given file is not a valid PHAR',
88
                    0,
89
                    $throwable
90
                );
91
            }
92
93
            $io->error('The given file is not a valid PHAR');
94
95
            return 1;
96
        }
97
98
        $restoreLimit = bump_open_file_descriptor_limit($box, $io);
99
100
        $outputDir = $input->getArgument(self::OUTPUT_ARG);
101
102
        try {
103
            remove($outputDir);
104
105
            foreach ($box->getPhar() as $pharFile) {
106
                /* @var PharFileInfo $pharFile */
107
                dump_file(
108
                    $outputDir.'/'.$pharFile->getFilename(),
0 ignored issues
show
Bug introduced by
Are you sure $outputDir of type null|string|string[] can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

108
                    /** @scrutinizer ignore-type */ $outputDir.'/'.$pharFile->getFilename(),
Loading history...
109
                    (string) $pharFile->getContent()
110
                );
111
            }
112
        } catch (RuntimeException $exception) {
113
            $io->error($exception->getMessage());
114
115
            return 1;
116
        } finally {
117
            $restoreLimit();
118
119
            remove($tmpFile);
120
        }
121
122
        $io->success('');
123
124
        return 0;
125
    }
126
}
127