Completed
Branch BUG-11108-ticket-reserved-coun... (144d27)
by
unknown
14:21 queued 17s
created

PhpCompatGreaterFiveSixHelper   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 90
Duplicated Lines 16.67 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 15
loc 90
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 15 15 2
A getTimezoneStringFromGmtOffset() 0 7 2
A convertWpGmtOffsetForDateTimeZone() 0 14 3
A convertToTimeFraction() 0 7 1
A getTimezoneOffset() 0 6 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace EventEspresso\core\services\helpers\datetime;
3
4
use DateTime;
5
use DateTimeZone;
6
use DomainException;
7
use EE_Datetime_Field;
8
9
class PhpCompatGreaterFiveSixHelper extends AbstractHelper
10
{
11
    /**
12
     * PhpCompatLessFiveSixHelper constructor.
13
     *
14
     * @throws DomainException
15
     */
16 View Code Duplication
    public function __construct()
17
    {
18
        if (PHP_VERSION_ID < 50600) {
19
            throw new DomainException(
20
                sprintf(
21
                    esc_html__(
22
                        'The %1$s is only usable on php versions greater than 5.6.  You\'ll want to use %2$s instead.',
23
                        'event_espresso'
24
                    ),
25
                    __CLASS__,
26
                    'EventEspresso\core\services\helpers\datetime\PhpCompatLessFiveSixHelper'
27
                )
28
            );
29
        }
30
    }
31
32
    /**
33
     * Returns a timezone string for the provided gmt_offset.
34
     * This is a valid timezone string that can be sent into DateTimeZone
35
     *
36
     * @param float|string $gmt_offset
37
     * @return string
38
     */
39
    public function getTimezoneStringFromGmtOffset($gmt_offset = '')
40
    {
41
        $gmt_offset_or_timezone_string = $this->sanitizeInitialIncomingGmtOffsetForGettingTimezoneString($gmt_offset);
42
        return is_float($gmt_offset_or_timezone_string)
43
            ? $this->convertWpGmtOffsetForDateTimeZone($gmt_offset_or_timezone_string)
44
            : $gmt_offset_or_timezone_string;
45
    }
46
47
48
49
    /**
50
     * Returns a formatted offset for use as an argument for constructing DateTimeZone
51
     * @param float $gmt_offset This should be a float representing the gmt_offset.
52
     * @return string
53
     */
54
    protected function convertWpGmtOffsetForDateTimeZone($gmt_offset)
55
    {
56
        $gmt_offset = (float) $gmt_offset;
57
        $is_negative = $gmt_offset < 0;
58
        $gmt_offset *= 100;
59
        $gmt_offset = absint($gmt_offset);
60
        //negative and need zero padding?
61
        if (strlen($gmt_offset) < 4) {
62
            $gmt_offset = str_pad($gmt_offset, 4, '0', STR_PAD_LEFT);
63
        }
64
        $gmt_offset = $this->convertToTimeFraction($gmt_offset);
65
        //return something like -1300, -0200 or +1300, +0200
66
        return $is_negative ? '-' . $gmt_offset : '+' . $gmt_offset;
67
    }
68
69
70
    /**
71
     * Converts something like `1550` to `1530` or `0275` to `0245`
72
     * Incoming offset should be a positive value, this will mutate negative values. Be aware!
73
     * @param int $offset
74
     * @return mixed
75
     */
76
    protected function convertToTimeFraction($offset)
77
    {
78
        $first_part = substr($offset, 0, 2);
79
        $second_part = substr($offset, 2, 2);
80
        $second_part = str_replace(array('25', '50', '75'), array('15', '30', '45'), $second_part);
81
        return $first_part . $second_part;
82
    }
83
84
85
    /**
86
     * Get Timezone offset for given timezone object
87
     *
88
     * @param DateTimeZone $date_time_zone
89
     * @param null|int     $time
90
     * @return int
91
     */
92
    public function getTimezoneOffset(DateTimezone $date_time_zone, $time = NULL)
93
    {
94
        $time = is_int($time) || $time === null ? $time : (int) strtotime($time);
95
        $time = preg_match(EE_Datetime_Field::unix_timestamp_regex, $time) ? $time : time();
96
        return $date_time_zone->getOffset(new DateTime('@' . $time));
97
    }
98
}
99