Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
Day
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
5 / 5
14
100.00% covered (success)
100.00%
1 / 1
 writeTemporaryScopeList
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
5
 rewriteTemporaryScopeList
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 readByCalendar
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 readListFromPreparedTemporaryScopeList
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
4
 __destruct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace BO\Zmsbackend\Day\Service;
4
5use BO\Zmsentities\Day as Entity;
6
7/**
8 *
9 */
10class Day extends \BO\Zmsbackend\Base
11{
12    protected $tempScopeListExists = false;
13
14    public function writeTemporaryScopeList(\BO\Zmsentities\Calendar $calendar, $slotsRequiredForce = null)
15    {
16        $this->getReader()->exec(\BO\Zmsbackend\Day\Repository\Day::QUERY_CREATE_TEMPORARY_SCOPELIST);
17        $monthList = $calendar->getMonthList();
18        $slotsRequired = $slotsRequiredForce;
19        foreach ($monthList as $month) {
20            $dateTime = $month->getFirstDay();
21            foreach ($calendar->scopes as $scope) {
22                if (!$slotsRequiredForce) {
23                    $slotsRequired = $calendar->scopes->getRequiredSlotsByScope($scope);
24                }
25                $this->getReader()->perform(\BO\Zmsbackend\Day\Repository\Day::QUERY_INSERT_TEMPORARY_SCOPELIST, [
26                    'scopeID' => $scope->id,
27                    'year' => $dateTime->format('Y'),
28                    'month' => $dateTime->format('m'),
29                    'slotsRequired' => $slotsRequired > 1 ? round($slotsRequired, 0) : 1,
30                ]);
31            }
32        }
33        $this->tempScopeListExists = true;
34    }
35
36    /**
37     * Drop and rebuild calendarscope for the calendar's current firstDay/lastDay months.
38     * Used to shrink the daylist window (painted month) or scan one neighbor month at a time.
39     */
40    public function rewriteTemporaryScopeList(\BO\Zmsentities\Calendar $calendar, $slotsRequiredForce = null)
41    {
42        if ($this->tempScopeListExists) {
43            $this->getReader()->exec(\BO\Zmsbackend\Day\Repository\Day::QUERY_DROP_TEMPORARY_SCOPELIST);
44            $this->tempScopeListExists = false;
45        }
46        $this->writeTemporaryScopeList($calendar, $slotsRequiredForce);
47    }
48
49    public function readByCalendar(\BO\Zmsentities\Calendar $calendar, $slotsRequiredForce = null)
50    {
51        // We use a temporary table, so we can use create and insert on a readonly connection
52        $this->writeTemporaryScopeList($calendar, $slotsRequiredForce);
53
54        return $this->readListFromPreparedTemporaryScopeList($slotsRequiredForce);
55    }
56
57    public function readListFromPreparedTemporaryScopeList($slotsRequiredForce = null, ?string $daylistQuery = null)
58    {
59        $dayList = new \BO\Zmsentities\Collection\DayList();
60        $dayData = $this->getReader()->fetchAll(
61            $daylistQuery ?? \BO\Zmsbackend\Day\Repository\Day::QUERY_DAYLIST_JOIN,
62            [
63                'forceRequiredSlots' =>
64                    ($slotsRequiredForce === null || $slotsRequiredForce < 1) ? 1 : round($slotsRequiredForce),
65            ]
66        );
67        foreach ($dayData as $day) {
68            $day = new \BO\Zmsentities\Day($day);
69            $dayList[$day->getDayHash()] = $day;
70        }
71
72        return $dayList;
73    }
74
75    /**
76     * Remove temporary scope list at destruct to allow other functions to use it
77     */
78    public function __destruct()
79    {
80        if ($this->tempScopeListExists) {
81            $this->getReader()->exec(\BO\Zmsbackend\Day\Repository\Day::QUERY_DROP_TEMPORARY_SCOPELIST);
82        }
83    }
84}