Test Setup Failed
Branch feature/job-builder/work-env-r... (305963)
by Grant
52:57 queued 34:52
created

ptDayEndToUtcTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 11
rs 10
cc 1
nc 1
nop 1
ccs 5
cts 5
cp 1
crap 1
1
<?php
2
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 View Code Duplication
if (!function_exists('humanizeDate')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 7
        $dateFormat = Config::get('app.date_format');
19 7
        $locale = App::getLocale();
20 7
        $timezone = Config::get('app.local_timezone');
21
22 7
        $datetime->setTimezone(new \DateTimeZone($timezone));
23
24 7
        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 3
        if (!isset($origin)) {
66 3
            $origin = new Date();
67
        }
68 3
        $interval = $datetime->diff($origin);
69
70 3
        $d = $interval->d;
71 3
        $h = $interval->h;
72 3
        $m = $interval->i;
73 3
        $s = $interval->s;
74
75 3 View Code Duplication
        if ($d > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76 3
            $unit = 'day';
77 3
            $count = $d;
78 2
        } elseif ($h > 0) {
79 1
            $unit = 'hour';
80 1
            $count = $h;
81 2
        } elseif ($m > 0) {
82 1
            $unit = 'minute';
83 1
            $count = $m;
84
        } else {
85 1
            $unit = 'second';
86 1
            $count = $s;
87
        }
88
89 3
        $key = "common/time.$unit";
90
91 3
        return Lang::choice($key, $count);
0 ignored issues
show
Bug introduced by
The method choice() does not exist on Illuminate\Support\Facades\Lang. Did you maybe mean transChoice()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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 transform to date of last day.
100
     *
101
     * @return string
102
     */
103
    function humanizeLastDay(Date $datetime) : string
104
    {
105 3
        $lastday = $datetime->sub("1 day");
106
107 3
        return humanizeDate($lastday);
0 ignored issues
show
Security Bug introduced by
It seems like $lastday defined by $datetime->sub('1 day') on line 105 can also be of type false; however, humanizeDate() does only seem to accept object<Jenssegers\Date\Date>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
108
    }
109
}
110
111 View Code Duplication
if (!function_exists('ptDayStartToUtcTime')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112
    /**
113
     * Given a date, creates a datetime object representing the start of day
114
     * in the most Western timezone in Canada (America/Vancouver).
115
     *
116
     * @param string $date ISO standard date YYYY-MM-DD.
117
     *
118
     * @return Date
119
     */
120
    function ptDayStartToUtcTime(string $date) : Date
121
    {
122 39
        $jobTimezone = Config::get('app.job_timezone');
123 39
        $dbTimezone = Config::get('app.timezone');
124
        // Create a new date that's the beginning of the day in
125
        // Pacific Daylight/Standard Time.
126 39
        $date = new Date("$date 00:00:00", new \DateTimeZone($jobTimezone));
127
        // Convert to UTC for correct offset.
128 39
        $date->setTimezone($dbTimezone);
129
130 39
        return $date;
131
    }
132
}
133
134 View Code Duplication
if (!function_exists('ptDayEndToUtcTime')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
135
    /**
136
     * Given a date, creates a datetime object representing the start of day
137
     * in the most Western timezone in Canada (America/Vancouver).
138
     *
139
     * @param string $date ISO standard date YYYY-MM-DD.
140
     *
141
     * @return Date
142
     */
143
    function ptDayEndToUtcTime(string $date) : Date
144
    {
145 39
        $jobTimezone = Config::get('app.job_timezone');
146 39
        $dbTimezone = Config::get('app.timezone');
147
        // Create a new date that's the beginning of the day in
148
        // Pacific Daylight/Standard Time.
149 39
        $date = new Date("$date 23:59:59", new \DateTimeZone($jobTimezone));
150
        // Convert to UTC for correct offset.
151 39
        $date->setTimezone($dbTimezone);
152
153 39
        return $date;
154
    }
155
}
156