Utils::getTimeDiff()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 13
c 0
b 0
f 0
nc 6
nop 1
dl 0
loc 26
ccs 14
cts 14
cp 1
crap 6
rs 9.2222
1
<?php
2
3
/*
4
 * This file is part of the Magallanes package.
5
 *
6
 * (c) Andrés Montañez <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Mage;
13
14
use Mage\Runtime\Runtime;
15
16
/**
17
 * Utility class for resolving trivial operations
18
 *
19
 * @author Andrés Montañez <[email protected]>
20
 */
21
class Utils
22
{
23
    /**
24
     * Given a stage code it will resolve a human friendly name
25
     */
26 41
    public function getStageName(string $stage): string
27
    {
28 41
        switch ($stage) {
29
            case Runtime::PRE_DEPLOY:
30 41
                return 'Pre Deploy';
31
32
            case Runtime::ON_DEPLOY:
33 36
                return 'On Deploy';
34
35
            case Runtime::POST_DEPLOY:
36 27
                return 'Post Deploy';
37
38
            case Runtime::ON_RELEASE:
39 29
                return 'On Release';
40
41
            case Runtime::POST_RELEASE:
42 29
                return 'Post Release';
43
        }
44
45 1
        return $stage;
46
    }
47
48
    /**
49
     * Given a Release ID, convert it to a DateTime instance
50
     */
51 2
    public function getReleaseDate(string $releaseId): \DateTime
52
    {
53 2
        $formatted = sprintf(
54
            '%d%d%d%d-%d%d-%d%d %d%d:%d%d:%d%d',
55 2
            $releaseId[0],
56 2
            $releaseId[1],
57 2
            $releaseId[2],
58 2
            $releaseId[3],
59 2
            $releaseId[4],
60 2
            $releaseId[5],
61 2
            $releaseId[6],
62 2
            $releaseId[7],
63 2
            $releaseId[8],
64 2
            $releaseId[9],
65 2
            $releaseId[10],
66 2
            $releaseId[11],
67 2
            $releaseId[12],
68 2
            $releaseId[13]
69
        );
70
71 2
        return new \DateTime($formatted);
72
    }
73
74
    /**
75
     * Given a Date, calculate friendly how much time has passed
76
     */
77 2
    public function getTimeDiff(\DateTime $releaseDate): string
78
    {
79 2
        $now = new \DateTime();
80 2
        $diff = $now->diff($releaseDate);
81
82 2
        if ($diff->days > 7) {
83 2
            return '';
84
        }
85
86 1
        if ($diff->days == 7) {
87 1
            return 'a week ago';
88
        }
89
90 1
        if ($diff->days >= 1) {
91 1
            return sprintf('%d day(s) ago', $diff->days);
92
        }
93
94 1
        if ($diff->h >= 1) {
95 1
            return sprintf('%d hour(s) ago', $diff->h);
96
        }
97
98 1
        if ($diff->i >= 1) {
99 1
            return sprintf('%d minute(s) ago', $diff->i);
100
        }
101
102 1
        return 'just now';
103
    }
104
}
105