SelfUpdateCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Paysera\PhpStormHelper\Command;
6
7
use Humbug\SelfUpdate\Updater;
8
use Phar;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Style\SymfonyStyle;
13
14
class SelfUpdateCommand extends Command
15
{
16
    private $updater;
17
18 6
    public function __construct(Updater $updater)
19
    {
20 6
        parent::__construct('self-update');
21 6
        $this->updater = $updater;
22 6
    }
23
24 6
    protected function configure(): void
25
    {
26
        $this
27 6
            ->setDescription(sprintf(
28 6
                'Update %s to most recent stable build.',
29 6
                $this->getLocalPharName()
30
            ))
31 6
            ->setAliases(['selfupdate'])
32
        ;
33 6
    }
34
35
    protected function execute(InputInterface $input, OutputInterface $output): int
36
    {
37
        $styledOutput = new SymfonyStyle($input, $output);
38
39
        $result = $this->updater->update();
40
41
        if ($result) {
42
            $styledOutput->success(sprintf(
43
                '%s has been updated from "%s" to "%s".',
44
                $this->getLocalPharName(),
45
                $this->updater->getOldVersion(),
46
                $this->updater->getNewVersion()
47
            ));
48
        } else {
49
            $styledOutput->success(sprintf('%s is already up to date.', $this->getLocalPharName()));
50
        }
51
52
        return 0;
53
    }
54
55 6
    private function getLocalPharName(): string
56
    {
57 6
        return basename(Phar::running());
58
    }
59
}
60