GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 138d08...b4d67e )
by Dragos
14:16
created

BusinessHoursBuilder::shiftToTimezone()   B

Complexity

Conditions 8
Paths 11

Size

Total Lines 53
Code Lines 32

Duplication

Lines 24
Ratio 45.28 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 24
loc 53
rs 7.1199
cc 8
eloc 32
nc 11
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Speicher210\BusinessHours;
4
5
use Speicher210\BusinessHours\Day\Day;
6
use Speicher210\BusinessHours\Day\DayBuilder;
7
use Speicher210\BusinessHours\Day\DayInterface;
8
use Speicher210\BusinessHours\Day\Time\TimeBuilder;
9
use Speicher210\BusinessHours\Day\Time\TimeInterval;
10
11
/**
12
 * Build a BusinessHours concrete implementation.
13
 */
14
class BusinessHoursBuilder
15
{
16
    /**
17
     * Build a BusinessHours from an array.
18
     *
19
     * @param array $data The business hours data.
20
     * @return BusinessHours
21
     */
22
    public static function fromAssociativeArray(array $data)
23
    {
24 View Code Duplication
        if (!isset($data['days']) || !is_array($data['days']) || !isset($data['timezone'])) {
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...
25
            throw new \InvalidArgumentException('Array is not valid.');
26
        }
27
28
        $days = array();
29
        foreach ($data['days'] as $day) {
30
            $days[] = DayBuilder::fromAssociativeArray($day);
31
        }
32
33
        return new BusinessHours($days, new \DateTimeZone($data['timezone']));
34
    }
35
36
    /**
37
     * Create a new BusinessHours with a different timezone from an existing BusinessHours.
38
     *
39
     * @param BusinessHours $businessHours The original business hours.
40
     * @param \DateTimeZone $newTimezone The new timezone.
41
     * @return BusinessHours
42
     */
43
    public static function shiftToTimezone(BusinessHours $businessHours, \DateTimeZone $newTimezone)
44
    {
45
        $now = new \DateTime('now');
46
        $oldTimezone = $businessHours->getTimezone();
47
        $offset = $newTimezone->getOffset($now) - $oldTimezone->getOffset($now);
48
49
        if ($offset === 0) {
50
            return clone $businessHours;
51
        }
52
53
        $tmpDays = array_fill_keys(Day::getDaysOfWeek(), array());
54
        foreach ($businessHours->getDays() as $day) {
55
            foreach ($day->getOpeningHoursIntervals() as $interval) {
56
                $start = $interval->getStart()->toSeconds() + $offset;
57
                $end = $interval->getEnd()->toSeconds() + $offset;
58
59
                // Current day.
60 View Code Duplication
                if ($start < 86400 && $end > 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...
61
                    $startForCurrentDay = max($start, 0);
62
                    $endForCurrentDay = min($end, 86400);
63
64
                    $dayOfWeek = $day->getDayOfWeek();
65
                    $interval = new TimeInterval(TimeBuilder::fromSeconds($startForCurrentDay), TimeBuilder::fromSeconds($endForCurrentDay));
66
                    array_push($tmpDays[$dayOfWeek], $interval);
67
                }
68
69
                // Previous day.
70 View Code Duplication
                if ($start < 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...
71
                    $startForPreviousDay = 86400 + $start;
72
                    $endForPreviousDay = min(86400, 86400 + $end);
73
74
                    $dayOfWeek = self::getPreviousDayOfWeek($day->getDayOfWeek());
75
                    $interval = new TimeInterval(TimeBuilder::fromSeconds($startForPreviousDay), TimeBuilder::fromSeconds($endForPreviousDay));
76
                    array_push($tmpDays[$dayOfWeek], $interval);
77
                }
78
79
                // Next day.
80 View Code Duplication
                if ($end > 86400) {
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...
81
                    $startForNextDay = max(0, $start - 86400);
82
                    $endForNextDay = $end - 86400;
83
84
                    $dayOfWeek = self::getNextDayOfWeek($day->getDayOfWeek());
85
                    $interval = new TimeInterval(TimeBuilder::fromSeconds($startForNextDay), TimeBuilder::fromSeconds($endForNextDay));
86
                    array_push($tmpDays[$dayOfWeek], $interval);
87
                }
88
            };
89
        }
90
91
        $tmpDays = array_filter($tmpDays);
92
        $days = self::flattenDaysIntervals($tmpDays);
93
94
        return new BusinessHours($days, $newTimezone);
95
    }
96
97
    /**
98
     * Flatten days intervals.
99
     *
100
     * @param array $days The days to flatten.
101
     * @return DayInterface[]
102
     */
103
    private static function flattenDaysIntervals(array $days)
104
    {
105
        ksort($days);
106
107
        $flattenDays = array();
108
        foreach ($days as $dayOfWeek => $intervals) {
109
            $flattenDays[] = DayBuilder::fromArray($dayOfWeek, $intervals);;
110
        }
111
112
        return $flattenDays;
113
    }
114
115
    /**
116
     * Get previous day of week for a given day of week.
117
     *
118
     * @param integer $dayOfWeek The day of week.
119
     * @return integer
120
     */
121
    private static function getPreviousDayOfWeek($dayOfWeek)
122
    {
123
        return DayInterface::WEEK_DAY_MONDAY === $dayOfWeek ? DayInterface::WEEK_DAY_SUNDAY : --$dayOfWeek;
124
    }
125
126
    /**
127
     * Get next day of week for a given day of week.
128
     *
129
     * @param integer $dayOfWeek The day of week.
130
     * @return integer
131
     */
132
    private static function getNextDayOfWeek($dayOfWeek)
133
    {
134
        return DayInterface::WEEK_DAY_SUNDAY === $dayOfWeek ? DayInterface::WEEK_DAY_MONDAY : ++$dayOfWeek;
135
    }
136
}
137