-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathWrapper.php
243 lines (217 loc) · 6.85 KB
/
Wrapper.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
namespace Drupal\openy_daxko_gxp_syncer\syncer;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\openy_mappings\LocationMappingRepository;
/**
* Enrich and normilize data from daxko groupex api for create sessions.
*
* @package Drupal\openy_daxko_gxp_syncer/syncer.
*/
class Wrapper {
/**
* Config.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
public $config;
/**
* Location Mapping repository.
*
* @var \Drupal\openy_mappings\LocationMappingRepository
*/
protected $locationRepository;
/**
* Logger channel.
*
* @var \Drupal\Core\Logger\LoggerChannelInterface
*/
public $logger;
/**
* Array of schedules.
*
* @var array
*/
protected $schedules;
/**
* Array of schedules to create.
*
* @var array
*/
protected $schedulesToCreate;
/**
* Array of schedules to update.
*
* @var array
*/
protected $schedulesToUpdate;
/**
* Array of locations data.
*
* @var array
*/
protected $locationMapping;
/**
* Constructor.
*
* @param \Drupal\Core\Config\ImmutableConfig $config
* Config.
* @param \Drupal\ymca_mappings\LocationMappingRepository $locationRepository
* YN Location repositore.
* @param \Drupal\Core\Logger\LoggerChannelInterface $logger
* Logger.
*/
public function __construct(ImmutableConfig $config, LocationMappingRepository $locationRepository, LoggerChannelInterface $logger) {
$this->config = $config;
$this->locationRepository = $locationRepository;
$this->locationMapping = [];
$this->logger = $logger;
}
/**
* {@inheritdoc}
*/
public function getSchedules() {
return $this->schedules;
}
/**
* {@inheritdoc}
*/
public function getSchedulesToCreate() {
return $this->schedulesToCreate;
}
/**
* {@inheritdoc}
*/
public function setSchedulesToCreate($schedules) {
$this->schedulesToCreate = $schedules;
}
/**
* {@inheritdoc}
*/
public function getSchedulesToUpdate() {
return $this->schedulesToUpdate;
}
/**
* {@inheritdoc}
*/
public function setSchedulesToUpdate($schedules) {
$this->schedulesToUpdate = $schedules;
}
/**
* {@inheritdoc}
*/
public function addSchedules($rawData) {
$schedules = [];
$locationMapping = $this->getLocationMapping();
// Remove not needed brief key from api data.
foreach ($rawData as $schedule) {
if (isset($schedule['brief'])) {
$schedules[$schedule['brief']['id']] = $schedule['brief'];
}
}
// Normalize fields.
foreach ($schedules as $key => &$schedule) {
if (!$this->isScheduleValid($schedule)) {
unset($schedules[$key]);
continue;
}
$schedule['locationId'] = $locationMapping[$schedule['locationId']]['branchId'];
// In Groupex UI we can set only one instructor or activity.
$schedule['instructor'] = reset($schedule['instructor']);
$schedule['activity'] = reset($schedule['activity']);
// Remove  symbol.
$schedule['activity'] = str_replace('Â', '', $schedule['activity']);
$schedule['name'] = str_replace('Â', '', $schedule['name']);
if ($schedule['reservable'] && $this->config->get('enable_capacity_in_full_syncer')) {
$availabilityStatus = 'class full';
$availability = $schedule["capacity"] - $schedule["booked"];
$waitlist = $schedule["waitlistCapacity"] - $schedule["waitlistBooked"];
if ($waitlist > 0) {
// @todo disaplay waitlist availability.
$availabilityStatus = 'waitlist only';
}
if ($availability > 0) {
$availabilityStatus = $availability . ' spots left';
}
$schedule['availabilityStatus'] = $availabilityStatus;
}
if ($this->config->get('enable_capacity_in_full_syncer')) {
unset($schedule["capacity"]);
unset($schedule["booked"]);
unset($schedule["waitlistCapacity"]);
unset($schedule["waitlistBooked"]);
}
// Get reservation id for schedule. Last 6 symbols in id is a date.
if ($schedule['reservable']) {
$schedule['reservationId'] = substr($schedule['id'], 0, -6);
}
// Conver datetime.
$schedule['weekDay'] = new \DateTime($schedule['startDateTime'], new \DateTimeZone('utc'));
$schedule['weekDay']->setTimezone(new \DateTimeZone('America/Chicago'));
$schedule['weekDay'] = strtolower($schedule['weekDay']->format('l'));
$schedule['day'] = new \DateTime($schedule['startDateTime'], new \DateTimeZone('utc'));
$schedule['day']->setTimezone(new \DateTimeZone('America/Chicago'));
$schedule['day'] = $schedule['day']->format('Y-m-d');
$schedule['startDateTime'] = new \DateTime($schedule['startDateTime'], new \DateTimeZone('utc'));
$schedule['startDateTime'] = $schedule['startDateTime']->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
$schedule['endDateTime'] = new \DateTime($schedule['endDateTime'], new \DateTimeZone('utc'));
$schedule['endDateTime'] = $schedule['endDateTime']->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
// Add hash for check changes in cleaner.
$schedule['hash'] = hash('sha256', serialize($schedule));
}
$this->schedules = $schedules;
}
/**
* Get locations mapping.
*
* @return array
* Locations mapping with branch id and title.
*/
public function getLocationMapping() {
if (count($this->locationMapping) > 0) {
return $this->locationMapping;
}
$gxpLocations = $this->locationRepository->loadAllLocationsWithGroupExId();
$locationMapping = [];
foreach ($gxpLocations as $mapping) {
$branch = $mapping->field_location_ref->referencedEntities();
$branch = reset($branch);
$locationMapping[$mapping->get('field_groupex_id')->value] = [
'branchId' => $branch->id(),
'title' => $branch->title->value,
];
}
$this->locationMapping = $locationMapping;
return $locationMapping;
}
/**
* Check if schedule is valid.
*
* @param mixed $schedule
* Schedule.
*
* @return bool
* Is valid.
*/
private function isScheduleValid($schedule) {
$locationMapping = $this->getLocationMapping();
if (!isset($locationMapping[$schedule['locationId']])) {
$msg = 'We have not Groupex location id %id in locations mapping, continue. Data: %data';
$this->logger->warning($msg, [
'%id' => $schedule['locationId'],
'%data' => json_encode($schedule),
]);
return FALSE;
}
if (empty($schedule['name'])) {
$msg = 'No name in schedule data with id %id, continue. Data: %data';
$this->logger->warning($msg, [
'%id' => $schedule['id'],
'%data' => json_encode($schedule),
]);
return FALSE;
}
return TRUE;
}
}