Passed
Push — feature/display-last-full-day ( dc3e73...b485bc )
by Xander
16:46
created

humanizeLastDay()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 21
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 29
ccs 0
cts 0
cp 0
crap 30
rs 9.2728
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
use Jenssegers\Date\Date;
4
use Illuminate\Support\Facades\App;
5
use Illuminate\Support\Facades\Config;
6
use Illuminate\Support\Facades\Lang;
7
8
if (!function_exists('humanizeDate')) {
9
    /**
10
     * Computes a human readable localized date.
11
     *
12
     * @param Date $datetime DateTime object to translate.
13
     *
14
     * @return string
15
     */
16
    function humanizeDate(Date $datetime) : string
17
    {
18 6
        $dateFormat = Config::get('app.date_format');
19 6
        $locale = App::getLocale();
20 6
        $timezone = Config::get('app.local_timezone');
21
22 6
        $datetime->setTimezone(new \DateTimeZone($timezone));
23
24 6
        return $datetime->format($dateFormat[$locale]);
25
    }
26
}
27
28
if (!function_exists('humanizeTime')) {
29
    /**
30
     * Computes a human readable localized time.
31
     *
32
     * @param Date $datetime DateTime object to translate.
33
     *
34
     * @return string
35
     */
36
    function humanizeTime(Date $datetime) : string
37
    {
38 2
        $timeFormat = Config::get('app.time_format');
39 2
        $locale = App::getLocale();
40 2
        $timezone = Config::get('app.local_timezone');
41
42 2
        $datetime->setTimezone(new \DateTimeZone($timezone));
43
44 2
        $displayTime = $datetime->format($timeFormat[$locale]);
45
46 2
        if ($locale == 'fr') {
47
            $displayTime = str_replace(['EST', 'EDT'], ['HNE', 'HAE'], $displayTime);
48
        }
49
50 2
        return $displayTime;
51
    }
52
}
53
54
if (!function_exists('humanizeDateDiff')) {
55
    /**
56
     * Computes a human readable time difference between two dates.
57
     *
58
     * @param Date $datetime DateTime object to measure.
59
     * @param Date $origin   DateTime object to use as a reference.
60
     *
61
     * @return string
62
     */
63
    function humanizeDateDiff(Date $datetime, Date $origin = null) : string
64
    {
65 6
        if (!isset($origin)) {
66 6
            $origin = new Date();
67
        }
68 6
        $interval = $datetime->diff($origin);
69
70 6
        $d = $interval->d;
71 6
        $h = $interval->h;
72 6
        $m = $interval->i;
73 6
        $s = $interval->s;
74
75 6
        if ($d > 0) {
76 6
            $unit = 'day';
77 6
            $count = $d;
78 4
        } elseif ($h > 0) {
79 2
            $unit = 'hour';
80 2
            $count = $h;
81 4
        } elseif ($m > 0) {
82 2
            $unit = 'minute';
83 2
            $count = $m;
84
        } else {
85 2
            $unit = 'second';
86 2
            $count = $s;
87
        }
88
89 6
        $key = "common/time.$unit";
90
91 6
        return Lang::choice($key, $count);
92
    }
93
}
94
95
if (!function_exists('humanizeLastDay')) {
96
    /**
97
     * Returns the date of the last full day a person has before a deadline time.
98
     *
99
     * @param Date $datetime DateTime object to measure.
100
     * @param Date $origin   DateTime object to use as a reference.
0 ignored issues
show
Coding Style introduced by
Superfluous parameter comment
Loading history...
101
     *
102
     * @return string
103
     */
104
    function humanizeLastDay(Date $datetime) : string
105
    {
106
        if (!isset($origin)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $origin seems to never exist and therefore isset should always be false.
Loading history...
107
            $origin = new Date();
108
        }
109
        $interval = $datetime->diff($origin);
110
111
        $d = $interval->d;
112
        $h = $interval->h;
113
        $m = $interval->i;
114
        $s = $interval->s;
115
116
        if ($d > 0) {
117
            $unit = 'day';
118
            $count = $d;
119
        } elseif ($h > 0) {
120
            $unit = 'hour';
121
            $count = $h;
122
        } elseif ($m > 0) {
123
            $unit = 'minute';
124
            $count = $m;
125
        } else {
126
            $unit = 'second';
127
            $count = $s;
128
        }
129
130
        $key = "common/time.$unit";
131
132
        return Lang::choice($key, $count);
133
    }
134
}
135