Passed
Push — master ( d95e49...7c87fb )
by Andrés
39s
created

SelfUpdateTask::getComposerOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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 38
    public function getName()
25
    {
26 38
        return 'composer/self-update';
27
    }
28
29 1
    public function getDescription()
30
    {
31 1
        return '[Composer] Self Update';
32
    }
33
34 5
    public function execute()
35
    {
36 5
        $options = $this->getOptions();
37 5
        $cmdVersion = sprintf('%s --version', $options['path']);
38
        /** @var Process $process */
39 5
        $process = $this->runtime->runCommand(trim($cmdVersion));
40 5
        if (!$process->isSuccessful()) {
41 1
            return false;
42
        }
43
44 4
        $buildDate = $this->getBuildDate($process->getOutput());
45 4
        if (!$buildDate instanceof DateTime) {
46 1
            return false;
47
        }
48
49 3
        $compareDate = $this->getCompareDate();
50 3
        if ($buildDate >= $compareDate) {
51 2
            throw new SkipException();
52
        }
53
54 1
        $cmdUpdate = sprintf('%s self-update', $options['path']);
55
        /** @var Process $process */
56 1
        $process = $this->runtime->runCommand(trim($cmdUpdate));
57
58 1
        return $process->isSuccessful();
59
    }
60
61 4
    protected function getBuildDate($output)
62
    {
63 4
        $buildDate = null;
64 4
        $output = explode(PHP_EOL, $output);
65 4
        foreach ($output as $row) {
66 4
            if (strpos($row, 'Composer version ') === 0) {
67 4
                $buildDate = DateTime::createFromFormat('Y-m-d H:i:s', substr(trim($row), -19));
68 4
            }
69 4
        }
70
71 4
        return $buildDate;
72
    }
73
74 3
    protected function getCompareDate()
75
    {
76 3
        $options = $this->getOptions();
77 3
        $compareDate = new DateTime();
78 3
        $compareDate->modify(sprintf('now -%d days', $options['days']));
79 3
        return $compareDate;
80
    }
81
82 5
    protected function getComposerOptions()
83
    {
84 5
        return ['days' => 60];
85
    }
86
}
87