2022-04-21 08:07:25 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Wrm\Events\Service\DestinationDataImportService;
|
|
|
|
|
2022-07-07 09:03:31 +02:00
|
|
|
use TYPO3\CMS\Core\Context\Context;
|
2022-04-21 08:07:25 +02:00
|
|
|
use Wrm\Events\Domain\Model\Date;
|
|
|
|
|
|
|
|
class DatesFactory
|
|
|
|
{
|
2022-07-07 09:03:31 +02:00
|
|
|
/**
|
|
|
|
* @var Context
|
|
|
|
*/
|
|
|
|
private $context;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
Context $context
|
|
|
|
) {
|
|
|
|
$this->context = $context;
|
|
|
|
}
|
|
|
|
|
2022-04-21 08:07:25 +02:00
|
|
|
/**
|
|
|
|
* @return \Generator<Date>
|
|
|
|
*/
|
|
|
|
public function createDates(
|
|
|
|
array $timeIntervals,
|
|
|
|
bool $canceled
|
|
|
|
): \Generator {
|
|
|
|
foreach ($timeIntervals as $date) {
|
|
|
|
$dates = $this->createDate($date, $canceled);
|
|
|
|
if (!$dates instanceof \Generator) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($dates as $createdDate) {
|
|
|
|
yield $createdDate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \Generator<Date>|null
|
|
|
|
*/
|
|
|
|
private function createDate(
|
|
|
|
array $date,
|
|
|
|
bool $canceled
|
|
|
|
): ?\Generator {
|
|
|
|
if ($this->isDateSingleDate($date)) {
|
|
|
|
return $this->createSingleDate($date, $canceled);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->isDateInterval($date)) {
|
|
|
|
return $this->createDateFromInterval($date, $canceled);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function isDateSingleDate(array $date): bool
|
|
|
|
{
|
2022-04-25 07:47:47 +02:00
|
|
|
$frequency = $date['freq'] ?? '';
|
|
|
|
$start = $date['start'] ?? '';
|
|
|
|
|
|
|
|
return $frequency === ''
|
|
|
|
&& $start !== '';
|
2022-04-21 08:07:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function isDateInterval(array $date): bool
|
|
|
|
{
|
|
|
|
if (empty($date['repeatUntil'])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$frequency = $date['freq'] ?? '';
|
|
|
|
|
|
|
|
if ($frequency == 'Daily' && empty($date['weekdays'])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($frequency == 'Weekly' && !empty($date['weekdays'])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \Generator<Date>
|
|
|
|
*/
|
|
|
|
private function createSingleDate(
|
|
|
|
array $date,
|
|
|
|
bool $canceled
|
|
|
|
): \Generator {
|
|
|
|
if (strtotime($date['start']) > $this->getToday()) {
|
|
|
|
yield Date::createFromDestinationDataDate($date, $canceled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \Generator<Date>|null
|
|
|
|
*/
|
|
|
|
private function createDateFromInterval(
|
|
|
|
array $date,
|
|
|
|
bool $canceled
|
|
|
|
): ?\Generator {
|
|
|
|
if ($date['freq'] == 'Daily') {
|
|
|
|
return $this->createDailyDates($date, $canceled);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($date['freq'] == 'Weekly') {
|
|
|
|
return $this->createWeeklyDates($date, $canceled);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \Generator<Date>
|
|
|
|
*/
|
|
|
|
private function createDailyDates(
|
|
|
|
array $date,
|
|
|
|
bool $canceled
|
|
|
|
): \Generator {
|
|
|
|
$today = $this->getToday();
|
2022-07-07 14:10:49 +02:00
|
|
|
$timeZone = new \DateTimeZone($date['tz']);
|
|
|
|
$start = new \DateTimeImmutable($date['start'], $timeZone);
|
|
|
|
$end = new \DateTimeImmutable($date['end'], $timeZone);
|
|
|
|
$until = new \DateTimeImmutable($date['repeatUntil'], $timeZone);
|
2022-04-21 08:07:25 +02:00
|
|
|
|
|
|
|
$i = (int) strtotime($start->format('l'), $start->getTimestamp());
|
|
|
|
while ($i !== 0 && $i <= $until->getTimestamp()) {
|
|
|
|
$i = (int) strtotime('+1 day', $i);
|
|
|
|
if ($i < $today) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-07-07 14:10:49 +02:00
|
|
|
yield $this->createDateFromStartAndEnd(
|
|
|
|
$i,
|
2022-04-21 08:07:25 +02:00
|
|
|
$start,
|
2022-07-07 14:10:49 +02:00
|
|
|
$end,
|
2022-04-21 08:07:25 +02:00
|
|
|
$canceled
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \Generator<Date>
|
|
|
|
*/
|
|
|
|
private function createWeeklyDates(
|
|
|
|
array $date,
|
|
|
|
bool $canceled
|
|
|
|
): \Generator {
|
|
|
|
$today = $this->getToday();
|
2022-07-07 14:10:49 +02:00
|
|
|
$timeZone = new \DateTimeZone($date['tz']);
|
|
|
|
$start = new \DateTimeImmutable($date['start'], $timeZone);
|
|
|
|
$end = new \DateTimeImmutable($date['end'], $timeZone);
|
|
|
|
$until = new \DateTimeImmutable($date['repeatUntil'], $timeZone);
|
2022-04-21 08:07:25 +02:00
|
|
|
|
|
|
|
foreach ($date['weekdays'] as $day) {
|
|
|
|
$i = strtotime($day, $start->getTimestamp());
|
2022-06-27 14:41:30 +02:00
|
|
|
while ($i !== 0 && $i <= $until->getTimestamp()) {
|
2022-07-07 14:10:49 +02:00
|
|
|
$timeStampToUse = $i;
|
2022-06-27 14:41:30 +02:00
|
|
|
$i = strtotime('+1 week', $i);
|
2022-04-21 08:07:25 +02:00
|
|
|
if ($i < $today) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-07-07 14:10:49 +02:00
|
|
|
yield $this->createDateFromStartAndEnd(
|
2022-06-27 14:41:30 +02:00
|
|
|
$timeStampToUse,
|
2022-04-21 08:07:25 +02:00
|
|
|
$start,
|
2022-07-07 14:10:49 +02:00
|
|
|
$end,
|
2022-04-21 08:07:25 +02:00
|
|
|
$canceled
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-07 14:10:49 +02:00
|
|
|
private function createDateFromStartAndEnd(
|
|
|
|
int $timestamp,
|
|
|
|
\DateTimeImmutable $start,
|
|
|
|
\DateTimeImmutable $end,
|
2022-04-21 08:07:25 +02:00
|
|
|
bool $canceled
|
|
|
|
): Date {
|
2022-07-07 14:10:49 +02:00
|
|
|
$eventStart = $start->setTimestamp($timestamp)->setTime(
|
|
|
|
(int) $start->format('H'),
|
|
|
|
(int) $start->format('i')
|
|
|
|
);
|
|
|
|
$eventEnd = $end->setTimestamp($timestamp)->setTime(
|
|
|
|
(int) $end->format('H'),
|
|
|
|
(int) $end->format('i')
|
|
|
|
);
|
2022-04-21 08:07:25 +02:00
|
|
|
|
|
|
|
return Date::createFromDestinationData(
|
|
|
|
$eventStart,
|
|
|
|
$eventEnd,
|
|
|
|
$canceled
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getToday(): int
|
|
|
|
{
|
2022-07-07 09:03:31 +02:00
|
|
|
$today = $this->context->getPropertyFromAspect('date', 'full', new \DateTimeImmutable());
|
|
|
|
if (!$today instanceof \DateTimeImmutable) {
|
|
|
|
$today = new \DateTimeImmutable();
|
|
|
|
}
|
|
|
|
|
|
|
|
$midnight = $today->modify('midnight');
|
|
|
|
return (int) $midnight->format('U');
|
2022-04-21 08:07:25 +02:00
|
|
|
}
|
|
|
|
}
|