Test Failed
Pull Request — master (#448)
by
unknown
05:18
created

Utils   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 0
dl 0
loc 86
ccs 0
cts 49
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B getStageName() 0 21 6
A getReleaseDate() 0 13 1
B getTimeDiff() 0 29 6
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;
12
13
use Mage\Runtime\Runtime;
14
use DateTime;
15
use DateInterval;
16
17
/**
18
 * Utility class for resolving trivial operations
19
 *
20
 * @author Andrés Montañez <[email protected]>
21
 */
22
class Utils
23
{
24
    /**
25
     * Given a stage code it will resolve a human friendly name
26
     *
27
     * @param string $stage
28
     * @return string
29
     */
30
    public function getStageName($stage)
31
    {
32
        switch ($stage) {
33
            case Runtime::PRE_DEPLOY:
34
                return 'Pre Deploy';
35
36
            case Runtime::ON_DEPLOY:
37
                return 'On Deploy';
38
39
            case Runtime::POST_DEPLOY:
40
                return 'Post Deploy';
41
42
            case Runtime::ON_RELEASE:
43
                return 'On Release';
44
45
            case Runtime::POST_RELEASE:
46
                return 'Post Release';
47
        }
48
49
        return $stage;
50
    }
51
52
    /**
53
     * Given a Release ID, convert it to a DateTime instance
54
     *
55
     * @param string $releaseId The Release ID
56
     * @return DateTime
57
     */
58
    public function getReleaseDate($releaseId)
59
    {
60
        $formatted = sprintf('%d%d%d%d-%d%d-%d%d %d%d:%d%d:%d%d',
61
            $releaseId[0], $releaseId[1], $releaseId[2], $releaseId[3],
62
            $releaseId[4], $releaseId[5],
63
            $releaseId[6], $releaseId[7],
64
            $releaseId[8], $releaseId[9],
65
            $releaseId[10], $releaseId[11],
66
            $releaseId[12], $releaseId[13]
67
        );
68
69
        return new DateTime($formatted);
70
    }
71
72
    /**
73
     * Given a Date, calculate friendly how much time has passed
74
     *
75
     * @param DateTime $releaseDate
76
     * @return string
77
     */
78
    public function getTimeDiff(DateTime $releaseDate)
79
    {
80
        $now = new DateTime();
81
82
        /** @var DateInterval $diff */
83
        $diff = $now->diff($releaseDate);
84
85
        if ($diff->days > 7) {
86
            return '';
87
        }
88
89
        if ($diff->days == 7) {
90
            return 'a week ago';
91
        }
92
93
        if ($diff->days >= 1) {
94
            return sprintf('%d day(s) ago', $diff->days);
95
        }
96
97
        if ($diff->h >= 1) {
98
            return sprintf('%d hour(s) ago', $diff->h);
99
        }
100
101
        if ($diff->i >= 1) {
102
            return sprintf('%d minute(s) ago', $diff->i);
103
        }
104
105
        return 'just now';
106
    }
107
}
108