Completed
Push — master ( b79250...5c4b09 )
by Alexandre
04:43
created

Planning::getFirstAvailableTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace AppBundle\Service;
4
5
use AppBundle\Entity\Holiday;
6
use AppBundle\Service\Loader\HolidayLoaderInterface;
7
8
class Planning
9
{
10
    private $holidayLoader;
11
    private $workDayHours = array('18:30', '19:00', '19:30', '20:00', '20:30');
12
    private $saturdayHours = array('10:00', '10:30', '11:00', '11:30', '12:00', '17:00', '17:30', '18:00', '18:30', '19:00');
13
    private $sundayHours = array('10:00', '10:30', '11:00', '11:30', '12:00');
14
15
    public function __construct(HolidayLoaderInterface $holidayLoader)
16
    {
17
        $this->holidayLoader = $holidayLoader;
18
    }
19
20
    public function getFirstAvailableTime()
21
    {
22
        $times = $this->getAvailableTimes();
23
        $day = array_shift($times);
24
25
        return array_shift($day);
26
    }
27
28
    public function getDateFormatter()
29
    {
30
        return $formatter = \IntlDateFormatter::create('fr_FR', \IntlDateFormatter::FULL, \IntlDateFormatter::NONE);
0 ignored issues
show
Unused Code introduced by
$formatter is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
31
    }
32
33
    public function getAvailableTimes()
34
    {
35
        $holidays = $this->holidayLoader->getHolidays();
36
37
        $result = array();
38
        $formatter = $this->getDateFormatter();
39
40
        $current = new \DateTime('+2 days');
41
        $dayCount = 21;
42
43
        $iterations = 50000;
44
        while (count($result) < $dayCount) {
45
46
            // security
47
            $iterations--;
48
            if ($iterations == 0) {
49
                throw new \LogicException('Too much iterations looking for an available time.');
50
            }
51
52
            if ($this->isClosed($current, $holidays)) {
53
                $current->modify('+1 day');
54
55
                continue;
56
            }
57
58
            $day = $current->format('N');
59
            $date = $formatter->format($current);
60
61
            if ($day == 6) {
62
                $hours = $this->saturdayHours;
63
            } elseif ($day == 7) {
64
                $hours = $this->sundayHours;
65
            } else {
66
                $hours = $this->workDayHours;
67
            }
68
69
            $vals = array_map(function ($val) use ($date) {
70
                return $date.' // '.$val;
71
            }, $hours);
72
            $result[$date] = array_combine($vals, $vals);
73
74
            $current->modify('+1 day');
75
        }
76
77
        return $result;
78
    }
79
80
    private function isClosed(\DateTime $day, array $holidays)
81
    {
82
        foreach ($holidays as $holiday) {
83
            if ($holiday->match($day)) {
84
                return true;
85
            }
86
        }
87
88
        return false;
89
    }
90
}
91