-
Notifications
You must be signed in to change notification settings - Fork 57
/
ScheduleManagement.php
161 lines (137 loc) · 4 KB
/
ScheduleManagement.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/**
* DISCLAIMER
* Do not edit or add to this file if you wish to upgrade this module to newer
* versions in the future.
*
* @category Smile
* @package Smile\StoreLocator
* @author Romain Ruaud <romain.ruaud@smile.fr>
* @copyright 2017 Smile
* @license Open Software License ("OSL") v. 3.0
*/
namespace Smile\StoreLocator\Model\Retailer;
use Smile\Retailer\Api\Data\RetailerInterface;
use Smile\StoreLocator\Api\Data\RetailerTimeSlotInterface;
use Magento\Framework\Locale\ListsInterface;
use Magento\Framework\Locale\Resolver;
/**
* Schedule Management class for Retailers
*
* @category Smile
* @package Smile\StoreLocator
* @author Romain Ruaud <romain.ruaud@smile.fr>
*/
class ScheduleManagement
{
/**
* Display calendar up to X days.
*/
const CALENDAR_MAX_DATE = 6;
/**
* @var \Magento\Framework\Locale\ListsInterface
*/
private $localeList;
/**
* ScheduleManagement constructor.
*
* @param \Magento\Framework\Locale\ListsInterface $localeList Locale Lists
*/
public function __construct(ListsInterface $localeList)
{
$this->localeList = $localeList;
}
/**
* Retrieve opening hours for a given date
*
* @SuppressWarnings(PHPMD.StaticAccess)
*
* @param RetailerInterface $retailer The retailer
* @param null $dateTime The date to retrieve opening hours for
*
* @return RetailerTimeSlotInterface[]
*/
public function getOpeningHours($retailer, $dateTime = null)
{
$dayOpening = [];
if ($dateTime == null) {
$dateTime = new \DateTime();
}
if (is_string($dateTime)) {
$dateTime = \DateTime::createFromFormat('Y-m-d', $dateTime);
}
$dayOfWeek = $dateTime->format('w');
$date = $dateTime->format('Y-m-d');
$openingHours = $retailer->getExtensionAttributes()->getOpeningHours();
$specialOpeningHours = $retailer->getExtensionAttributes()->getSpecialOpeningHours();
if (isset($openingHours[$dayOfWeek])) {
$dayOpening = $openingHours[$dayOfWeek];
}
if (isset($specialOpeningHours[$date])) {
$dayOpening = $specialOpeningHours[$date];
}
return $dayOpening;
}
/**
* Get shop calendar : opening hours for the next X days.
*
* @SuppressWarnings(PHPMD.StaticAccess)
*
* @param RetailerInterface $retailer The retailer
*
* @return array
*/
public function getCalendar($retailer)
{
$calendar = [];
$date = $this->getMinDate();
$calendar[$date->format('Y-m-d')] = $this->getOpeningHours($retailer, $date);
while ($date < $this->getMaxDate()) {
$date->add(\DateInterval::createFromDateString('+1 day'));
$calendar[$date->format('Y-m-d')] = $this->getOpeningHours($retailer, $date);
}
return $calendar;
}
/**
* Retrieve opening hours
*
* @param RetailerInterface $retailer The retailer
*
* @return array
*/
public function getWeekOpeningHours($retailer)
{
$openingHours = [];
$days = $this->localeList->getOptionWeekdays(true, true);
foreach (array_keys($days) as $day) {
$openingHours[$day] = [];
}
foreach ($retailer->getExtensionAttributes()->getOpeningHours() as $day => $hours) {
$openingHours[$day] = $hours;
}
return $openingHours;
}
/**
* Get min date to calculate calendar
*
* @return \DateTime
*/
private function getMinDate()
{
$date = new \DateTime();
return $date;
}
/**
* Get max date to calculate calendar
*
* @SuppressWarnings(PHPMD.StaticAccess)
*
* @return \DateTime
*/
private function getMaxDate()
{
$date = $this->getMinDate();
$date->add(\DateInterval::createFromDateString(sprintf('+%s day', self::CALENDAR_MAX_DATE)));
return $date;
}
}