Passed
Push — master ( 23dae9...6563ef )
by Andrés
01:14
created

SelfUpdateTask::getComposerOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
/*
3
 * This file is part of the Magallanes package.
4
 *
5
 * (c) Andrés Montañez <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Mage\Task\BuiltIn\Composer;
12
13
use Mage\Task\Exception\SkipException;
14
use Symfony\Component\Process\Process;
15
use DateTime;
16
17
/**
18
 * Composer Task - Self update
19
 *
20
 * @author Yanick Witschi <https://github.com/Toflar>
21
 */
22
class SelfUpdateTask extends AbstractComposerTask
23
{
24
    public function getName()
25
    {
26
        return 'composer/self-update';
27
    }
28
29
    public function getDescription()
30
    {
31
        return '[Composer] Self Update';
32
    }
33
34
    public function execute()
35
    {
36
        $options = $this->getOptions();
37
        $cmdVersion = sprintf('%s --version', $options['path']);
38
        /** @var Process $process */
39
        $process = $this->runtime->runCommand(trim($cmdVersion));
40
        if (!$process->isSuccessful()) {
41
            return false;
42
        }
43
44
        $buildDate = $this->getBuildDate($process->getOutput());
45
        if (!$buildDate instanceof DateTime) {
46
            return false;
47
        }
48
49
        $compareDate = $this->getCompareDate();
50
        if ($buildDate >= $compareDate) {
51
            throw new SkipException();
52
        }
53
54
        $cmdUpdate = sprintf('%s self-update', $options['path']);
55
        /** @var Process $process */
56
        $process = $this->runtime->runCommand(trim($cmdUpdate));
57
58
        return $process->isSuccessful();
59
    }
60
61
    protected function getBuildDate($output)
62
    {
63
        $buildDate = null;
64
        $output = explode(PHP_EOL, $output);
65
        foreach ($output as $row) {
66
            if (strpos($row, 'Composer version ') === 0) {
67
                $buildDate = DateTime::createFromFormat('Y-m-d H:i:s', substr(trim($row), -19));
68
            }
69
        }
70
71
        return $buildDate;
72
    }
73
74
    protected function getCompareDate()
75
    {
76
        $options = $this->getOptions();
77
        $compareDate = new DateTime();
78
        $compareDate->modify(sprintf('now -%d days', $options['days']));
79
        return $compareDate;
80
    }
81
82
    protected function getComposerOptions()
83
    {
84
        return ['days' => 60];
85
    }
86
}
87