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 ( cddece...946e53 )
by Luis Ramón
03:29
created

WorkdayRepository::getArrayCalendar()   C

Complexity

Conditions 12
Paths 31

Size

Total Lines 76
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 76
rs 5.2846
cc 12
eloc 50
nc 31
nop 1

How to fix   Long Method    Complexity   

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 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
        $count = 0;
105
        $locked = 0;
106
107
        /** @var \ArrayIterator $iterator */
108
        $iterator = $workdays->getIterator();
109
110
        /** @var \DateTime $targetDate */
111
        while ($iterator->valid() && ($targetDate = $iterator->current()->getDate()) >= $currentDate) {
112
            while ($targetDate >= $currentDate) {
113
                if ($currentWeek != $currentDate->format('W') || $currentMonth != $currentDate->format('m')) {
114
                    $month[(int) $currentWeek] = ['days' => $week, 'count' => $count, 'locked' => $locked];
115
                    $week = [];
116
                    $count = 0;
117
                    $locked = 0;
118
                    if ($currentMonth != $currentDate->format('m')) {
119
                        $calendar[((int) $currentYear * 12 + (int) $currentMonth - 1)] = $month;
120
                        $dayOfWeek = $currentDate->format('w');
121
                        $numFillDays = (7 + (int) $dayOfWeek - 1) % 7;
122
                        $month = [];
123
                        while ($numFillDays--) {
124
                            $week[] = ['day' => ''];
125
                        }
126
                        $currentMonth = $currentDate->format('m');
127
                        $currentYear = $currentDate->format('Y');
128
                    }
129
                    $currentWeek = $currentDate->format('W');
130
                }
131
132
                if ($targetDate != $currentDate) {
133
                    $week[] = ['day' => $currentDate->format('d')];
134
                }
135
                $currentDate->add(new \DateInterval('P1D'));
136
                $currentDate->setTime(0, 0);
137
            }
138
            $week[] = ['day' =>  $targetDate->format('d'), 'data' => $iterator->current()];
139
            $count++;
140
            if ($iterator->current()->isLocked()) {
141
                $locked++;
142
            }
143
            $iterator->next();
144
        }
145
        $month[(int) $currentWeek] = ['days' => $week, 'count' => $count, 'locked' => $locked];
146
147
        $calendar[((int) $currentYear * 12 + (int) $currentMonth - 1)] = $month;
148
149
        return $calendar;
150
    }
151
152 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...
153
    {
154
        $query = $this->createQueryBuilder('w')
155
            ->where('w.agreement = :agreement')
156
            ->andWhere('w.date > :date')
157
            ->orderBy('w.date', 'ASC')
158
            ->setParameter('date', $workday->getDate())
159
            ->setParameter('agreement', $workday->getAgreement())
160
            ->getQuery();
161
162
        $result = $query->setMaxResults(1)->getResult();
163
164
        return $result ? $result[0] : null;
165
    }
166
167 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...
168
    {
169
        $query = $this->createQueryBuilder('w')
170
            ->where('w.agreement = :agreement')
171
            ->andWhere('w.date < :date')
172
            ->orderBy('w.date', 'DESC')
173
            ->setParameter('date', $workday->getDate())
174
            ->setParameter('agreement', $workday->getAgreement())
175
            ->getQuery();
176
177
        $result = $query->setMaxResults(1)->getResult();
178
179
        return $result ? $result[0] : null;
180
    }
181
182
    public function getWorkdaysInRange(Agreement $agreement, \DateTime $start, \DateTime $end)
183
    {
184
        return $this->createQueryBuilder('w')
185
            ->where('w.agreement = :agreement')
186
            ->andWhere('w.date >= :start')
187
            ->andWhere('w.date <= :end')
188
            ->orderBy('w.date', 'DESC')
189
            ->setParameter('agreement', $agreement)
190
            ->setParameter('start', $start)
191
            ->setParameter('end', $end)
192
            ->getQuery()
193
            ->getResult();
194
    }
195
196
    public function getWorkdaysInWeek(Agreement $agreement, $week, $year)
197
    {
198
        $date = new \DateTime();
199
        $start = $date->setISODate($year, $week);
200
        $end = clone $date;
201
        $end = $end->modify('+6 days');
202
203
        return $this->getWorkdaysInRange($agreement, $start, $end);
204
    }
205
206
    public function isWeekLocked(Agreement $agreement, $week, $year)
207
    {
208
        $workdays = $this->getWorkdaysInWeek($agreement, $week, $year);
209
210
        $count = 0;
211
        $locked = 0;
212
213
        /** @var Workday $workday */
214
        foreach ($workdays as $workday) {
215
            $count++;
216
            if ($workday->isLocked()) {
217
                $locked++;
218
            }
219
        }
220
221
        // la semana está bloqueada si hay días de trabajo y todos están bloqueados
222
        return $count && ($count === $locked);
223
    }
224
}
225