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 — user-entity ( c270d0...2b54ee )
by Luis Ramón
04:29 queued 01:03
created

WorkdayRepository::isWeekLocked()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.2
cc 4
eloc 9
nc 6
nop 3
1
<?php
2
3
namespace AppBundle\Entity;
4
5
use AppBundle\Form\Model\Calendar;
6
use Doctrine\Common\Collections\ArrayCollection;
7
use Doctrine\Common\Collections\Collection;
8
use Doctrine\ORM\EntityRepository;
9
10
class WorkdayRepository extends EntityRepository
11
{
12
    public function createCalendar(Calendar $calendar, Agreement $agreement)
13
    {
14
        $date = $calendar->getStartDate();
15
        $hours = $calendar->getTotalHours();
16
        $nonSchoolDays = $this->getEntityManager()->getRepository('AppBundle:NonSchoolDay')
17
            ->createQueryBuilder('n')
18
            ->select('n.date')
19
            ->getQuery()->getArrayResult();
20
21
        $nonSchoolDays = array_map('current', $nonSchoolDays);
22
23
        $collection = new ArrayCollection();
24
25
        while ($hours > 0) {
26
            if (false === in_array($date, $nonSchoolDays, false)) {
27
                $current = $this->getEntityManager()->getRepository('AppBundle:Workday')->findOneBy([
28
                    'date' => $date,
29
                    'agreement' => $agreement
30
                ]);
31
                if (null === $current) {
32
                    $current = new Workday();
33
                }
34
                $current->setDate(clone $date);
35
                $current->setAgreement($agreement);
36
                $assignedHours = 0;
37
                switch ($date->format('w')) {
38
                    case 0:
39
                        $assignedHours = min($hours, $calendar->getHoursSun());
40
                        break;
41
                    case 1:
42
                        $assignedHours = min($hours, $calendar->getHoursMon());
43
                        break;
44
                    case 2:
45
                        $assignedHours = min($hours, $calendar->getHoursTue());
46
                        break;
47
                    case 3:
48
                        $assignedHours = min($hours, $calendar->getHoursWed());
49
                        break;
50
                    case 4:
51
                        $assignedHours = min($hours, $calendar->getHoursThu());
52
                        break;
53
                    case 5:
54
                        $assignedHours = min($hours, $calendar->getHoursFri());
55
                        break;
56
                    case 6:
57
                        $assignedHours = min($hours, $calendar->getHoursSat());
58
                        break;
59
                }
60
                $current->setHours($current->getHours() + $assignedHours);
61
62
                $hours -= $assignedHours;
63
64
                if ($current->getHours()) {
65
                    $collection->add($current);
66
                    $this->getEntityManager()->persist($current);
67
                }
68
            }
69
70
            $date->add(new \DateInterval('P1D'));
71
        }
72
        return $collection;
73
    }
74
75
    public function getArrayCalendar(Collection $workdays)
76
    {
77
        if ($workdays->isEmpty()) {
78
            return [];
79
        }
80
81
        $calendar = [];
82
        
83
        /** @var Workday $current */
84
        $currentWorkday = $workdays->first();
85
86
        /** @var \DateTime $currentDate */
87
        $currentDate = $currentWorkday->getDate();
88
89
        $currentMonth = $currentDate->format('m');
90
        $currentYear = $currentDate->format('Y');
91
        $currentDate = new \DateTime();
92
        $currentDate->setDate($currentYear, $currentMonth, 1);
93
        $currentDate->setTime(0, 0);
94
        $dayOfWeek = $currentDate->format('w');
95
        $currentWeek = $currentDate->format('W');
96
        $numFillDays = (7 + (int) $dayOfWeek - 1) % 7;
97
98
        $month = [];
99
        $week = [];
100
        while ($numFillDays--) {
101
            $week[] = ['day' => ''];
102
        }
103
104
        /** @var \ArrayIterator $iterator */
105
        $iterator = $workdays->getIterator();
106
107
        /** @var \DateTime $targetDate */
108
        while ($iterator->valid() && ($targetDate = $iterator->current()->getDate()) >= $currentDate) {
109
            while ($targetDate >= $currentDate) {
110
                if ($currentWeek != $currentDate->format('W') || $currentMonth != $currentDate->format('m')) {
111
                    $month[(int) $currentWeek] = $week;
112
                    $week = [];
113
                    if ($currentMonth != $currentDate->format('m')) {
114
                        $calendar[((int) $currentYear * 12 + (int) $currentMonth - 1)] = $month;
115
                        $dayOfWeek = $currentDate->format('w');
116
                        $numFillDays = (7 + (int) $dayOfWeek - 1) % 7;
117
                        $month = [];
118
                        while ($numFillDays--) {
119
                            $week[] = ['day' => ''];
120
                        }
121
                        $currentMonth = $currentDate->format('m');
122
                        $currentYear = $currentDate->format('Y');
123
                    }
124
                    $currentWeek = $currentDate->format('W');
125
                }
126
127
                if ($targetDate != $currentDate) {
128
                    $week[] = ['day' => $currentDate->format('d')];
129
                }
130
                $currentDate->add(new \DateInterval('P1D'));
131
                $currentDate->setTime(0, 0);
132
            }
133
            $week[] = ['day' =>  $targetDate->format('d'), 'data' => $iterator->current()];
134
            $iterator->next();
135
        }
136
        $month[(int) $currentWeek] = $week;
137
138
        $calendar[((int) $currentYear*12 + (int) $currentMonth - 1)] = $month;
139
140
        return $calendar;
141
    }
142
143 View Code Duplication
    public function getNext(Workday $workday)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
144
    {
145
        $query = $this->createQueryBuilder('w')
146
            ->where('w.agreement = :agreement')
147
            ->andWhere('w.date > :date')
148
            ->orderBy('w.date', 'ASC')
149
            ->setParameter('date', $workday->getDate())
150
            ->setParameter('agreement', $workday->getAgreement())
151
            ->getQuery();
152
153
        $result = $query->setMaxResults(1)->getResult();
154
155
        return $result ? $result[0] : null;
156
    }
157
158 View Code Duplication
    public function getPrevious(Workday $workday)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
159
    {
160
        $query = $this->createQueryBuilder('w')
161
            ->where('w.agreement = :agreement')
162
            ->andWhere('w.date < :date')
163
            ->orderBy('w.date', 'DESC')
164
            ->setParameter('date', $workday->getDate())
165
            ->setParameter('agreement', $workday->getAgreement())
166
            ->getQuery();
167
168
        $result = $query->setMaxResults(1)->getResult();
169
170
        return $result ? $result[0] : null;
171
    }
172
173
    public function getWorkdaysInRange(Agreement $agreement, \DateTime $start, \DateTime $end)
174
    {
175
        return $this->createQueryBuilder('w')
176
            ->where('w.agreement = :agreement')
177
            ->andWhere('w.date >= :start')
178
            ->andWhere('w.date <= :end')
179
            ->orderBy('w.date', 'DESC')
180
            ->setParameter('agreement', $agreement)
181
            ->setParameter('start', $start)
182
            ->setParameter('end', $end)
183
            ->getQuery()
184
            ->getResult();
185
    }
186
187
    public function getWorkdaysInWeek(Agreement $agreement, $week, $year)
188
    {
189
        $date = new \DateTime();
190
        $start = $date->setISODate($year, $week);
191
        $end = clone $date;
192
        $end = $end->modify('+6 days');
193
194
        return $this->getWorkdaysInRange($agreement, $start, $end);
195
    }
196
197
    public function isWeekLocked(Agreement $agreement, $week, $year)
198
    {
199
        $workdays = $this->getWorkdaysInWeek($agreement, $week, $year);
200
201
        $count = 0;
202
        $locked = 0;
203
204
        /** @var Workday $workday */
205
        foreach ($workdays as $workday) {
206
            $count++;
207
            if ($workday->isLocked()) {
208
                $locked++;
209
            }
210
        }
211
212
        // la semana está bloqueada si hay días de trabajo y todos están bloqueados
213
        return $count && ($count === $locked);
214
    }
215
}
216