+
+ /**
+ * {@inheritdoc}
+ */
+ public function preUpdate(PreUpdateEventArgs $eventArgs) {
+ //Check that we have a session instance
+ if (($user = $eventArgs->getEntity()) instanceof Session) {
+ //Set updated value
+ $user->setUpdated(new \DateTime('now'));
+ }
+ }
+
+ /**
+ * Wether if session is a premium day
+ *
+ * Consider as premium a day off for afternoon, the eve for evening and after
+ * Store computed result in premium member for afternoon and evening
+ *
+ * @return bool Whether the date is day off or not
+ */
+ public function isPremium(): bool {
+ //Without date
+ if (empty($date = $this->date)) {
+ throw new \LogicException('Property date is empty');
+ }
+
+ //Without slot
+ if (empty($slot = $this->slot) || empty($slotTitle = $slot->getTitle())) {
+ throw new \LogicException('Property slot is empty');
+ }
+
+ //With evening and after slot
+ if ($slotTitle == 'Evening' || $slotTitle == 'After') {
+ //Evening and after session is considered premium when the eve is a day off
+ $date = (clone $date)->add(new \DateInterval('P1D'));
+ }
+
+ //Get day number
+ $w = $date->format('w');
+
+ //Check if weekend day
+ if ($w == 0 || $w == 6) {
+ //With afternoon and evening slot
+ if ($slotTitle == 'Afternoon' || $slotTitle == 'Evening') {
+ //Save premium
+ $this->premium = true;
+ }
+
+ //Date is weekend day
+ return true;
+ }
+
+ //Get date day
+ $d = $date->format('d');
+
+ //Get date month
+ $m = $date->format('m');
+
+ //Check if fixed holiday
+ if (
+ //Check if 1st january
+ ($d == 1 && $m == 1) ||
+ //Check if 1st may
+ ($d == 1 && $m == 5) ||
+ //Check if 8st may
+ ($d == 8 && $m == 5) ||
+ //Check if 14st july
+ ($d == 14 && $m == 7) ||
+ //Check if 15st august
+ ($d == 15 && $m == 8) ||
+ //Check if 1st november
+ ($d == 1 && $m == 11) ||
+ //Check if 11st november
+ ($d == 11 && $m == 11) ||
+ //Check if 25st december
+ ($d == 25 && $m == 12)
+ ) {
+ //With afternoon and evening slot
+ if ($slotTitle == 'Afternoon' || $slotTitle == 'Evening') {
+ //Save premium
+ $this->premium = true;
+ }
+
+ //Date is a fixed holiday
+ return true;
+ }
+
+ //Get eastern
+ $eastern = $this->getEastern($date->format('Y'));
+
+ //Check dynamic holidays
+ if (
+ (clone $eastern)->add(new \DateInterval('P1D')) == $date ||
+ (clone $eastern)->add(new \DateInterval('P39D')) == $date ||
+ (clone $eastern)->add(new \DateInterval('P50D')) == $date
+ ) {
+ //With afternoon and evening slot
+ if ($slotTitle == 'Afternoon' || $slotTitle == 'Evening') {
+ //Save premium
+ $this->premium = true;
+ }
+
+ //Date is a dynamic holiday
+ return true;
+ }
+
+ //With afternoon and evening slot
+ if ($slotTitle == 'Afternoon' || $slotTitle == 'Evening') {
+ //Save premium
+ $this->premium = false;
+ }
+
+ //Date is not a holiday and week day
+ return false;
+ }
+
+ /**
+ * Compute eastern for selected year
+ *
+ * @param string $year The eastern year
+ *
+ * @return DateTime The eastern date
+ */
+ private function getEastern(string $year): \DateTime {
+ //Set static
+ static $data = null;
+
+ //Check if already computed
+ if (isset($data[$year])) {
+ //Return computed eastern
+ return $data[$year];
+ //Check if data is null
+ } elseif (is_null($data)) {
+ //Init data array
+ $data = [];
+ }
+
+ $d = (19 * ($year % 19) + 24) % 30;
+
+ $e = (2 * ($year % 4) + 4 * ($year % 7) + 6 * $d + 5) % 7;
+
+ $day = 22 + $d + $e;
+
+ $month = 3;
+
+ if ($day > 31) {
+ $day = $d + $e - 9;
+ $month = 4;
+ } elseif ($d == 29 && $e == 6) {
+ $day = 10;
+ $month = 4;
+ } elseif ($d == 28 && $e == 6) {
+ $day = 18;
+ $month = 4;
+ }
+
+ //Store eastern in data
+ return ($data[$year] = new \DateTime(sprintf('%04d-%02d-%02d', $year, $month, $day)));
+ }