Completed
Push — master ( 741f1a...42bbb8 )
by Bernhard
03:50
created

SelfCheck   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 87
ccs 0
cts 52
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 16 1
B execute() 0 39 6
1
<?php
2
3
/**
4
 * \Wicked\Timely\Command\SelfCheck
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    wick-ed
15
 * @copyright 2020 Bernhard Wick
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/wick-ed/timely
18
 */
19
20
namespace Wicked\Timely\Command;
21
22
use GuzzleHttp\Exception\ConnectException;
23
use Symfony\Component\Console\Command\Command;
24
use Symfony\Component\Console\Input\InputInterface;
25
use Symfony\Component\Console\Output\OutputInterface;
26
use Packagist\Api\Client as PackagistClient;
27
28
/**
29
 * Class for the "selfcheck" command. Command is used to track time bookings
30
 *
31
 * @author    wick-ed
32
 * @copyright 2020 Bernhard Wick
33
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
34
 * @link      https://github.com/wick-ed/timely
35
 */
36
class SelfCheck extends Command
37
{
38
39
    /**
40
     * Constants used within this command
41
     *
42
     * @var string
43
     */
44
    const COMMAND_NAME = 'self-check';
45
    const PACKAGE_VENDOR = 'wick-ed';
46
47
    /**
48
     * Configures the "track" command
49
     *
50
     * @return void
51
     *
52
     * {@inheritDoc}
53
     * @see \Symfony\Component\Console\Command\Command::configure()
54
     */
55
    protected function configure()
56
    {
57
        $this
58
        ->setName(static::COMMAND_NAME)
59
        ->setDescription('Some self checks.')
60
        ->setHelp(<<<'EOF'
61
The <info>%command.name%</info> command does some self checks.
62
Including checking for the latest version.
63
64
An example call would be:
65
66
  <info>timely %command.name%</info>
67
EOF
68
            )
69
        ;
70
    }
71
72
    /**
73
     * Execute the command
74
     *
75
     * @param \Symfony\Component\Console\Input\InputInterface   $input  The command input
76
     * @param \Symfony\Component\Console\Output\OutputInterface $output The command output
77
     *
78
     * @return int
79
     *
80
     * {@inheritDoc}
81
     * @see \Symfony\Component\Console\Command\Command::execute()
82
     */
83
    protected function execute(InputInterface $input, OutputInterface $output)
84
    {
85
        try {
86
            $client = new PackagistClient();
87
            $package = $client->get(static::PACKAGE_VENDOR . '/' . strtolower($this->getApplication()->getName()));
88
        } catch (ConnectException $e) {
89
            $output->writeln('<comment>Could not check for available new versions. Are you offline?.</comment>');
90
            return 1;
91
        }
92
        $versions = array_keys($package->getVersions());
93
        $latestVersion = array_shift($versions);
94
        foreach ($versions as $version) {
95
            if (version_compare($latestVersion, $version, 'lt')) {
96
                $latestVersion = $version;
97
            }
98
        }
99
100
        if ($this->getApplication()->getVersion() === $latestVersion) {
101
            $output->writeln(sprintf('<comment>Your local installation is the latest available version (%s)</comment>', $latestVersion));
102
            return 0;
103
        }
104
        if (version_compare($latestVersion, $this->getApplication()->getVersion(), 'lt')) {
105
            $output->writeln(
106
                sprintf(
107
                    '<comment>Your local version %s exceeds available versions! You from the future? 0_o</comment>',
108
                    $this->getApplication()->getVersion()
109
                )
110
            );
111
            return 0;
112
        }
113
        $output->writeln(
114
            sprintf(
115
                '<error>Your local installation\'s version (%s) does differ from the latest available version %s. Please consider to update.</error>',
116
                $this->getApplication()->getVersion(),
117
                $latestVersion
118
            )
119
        );
120
        return 1;
121
    }
122
}
123