From ca0a0adaaee8afd34146445c7972f57be9cacea1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bjo=CC=88rn=20Fromme?= Date: Wed, 4 Dec 2019 17:48:02 +0100 Subject: [PATCH] Code cleanup --- .../Classes/Controller/IcalController.php | 60 +------ .../Classes/Service/IcalService.php | 147 ++++++++++++++++++ 2 files changed, 155 insertions(+), 52 deletions(-) create mode 100644 public/typo3conf/ext/ep_products/Classes/Service/IcalService.php diff --git a/public/typo3conf/ext/ep_products/Classes/Controller/IcalController.php b/public/typo3conf/ext/ep_products/Classes/Controller/IcalController.php index 6ba834ca..5c64b04a 100644 --- a/public/typo3conf/ext/ep_products/Classes/Controller/IcalController.php +++ b/public/typo3conf/ext/ep_products/Classes/Controller/IcalController.php @@ -6,7 +6,7 @@ namespace EP\EpProducts\Controller; * * Copyright notice * - * (c) 2016 Björn Fromme , dreipunktnull + * (c) 2019 Björn Fromme , dreipunktnull * * All rights reserved * @@ -27,76 +27,32 @@ namespace EP\EpProducts\Controller; * This copyright notice MUST APPEAR in all copies of the script! ***************************************************************/ -use EP\EpProducts\Domain\Model\Contingent; use EP\EpProducts\Domain\Model\Hotel; -use EP\EpProducts\Domain\Repository\ContingentRepository; -use Kigkonsult\Icalcreator\Vcalendar; -use League\Period\Period; +use EP\EpProducts\Service\IcalService; use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; class IcalController extends ActionController { /** - * @var \EP\EpProducts\Domain\Repository\ContingentRepository + * @var IcalService */ - protected $contingentRepository; + protected $icalService; - /** - * @param ContingentRepository $contingentRepository - */ - public function __construct(ContingentRepository $contingentRepository) + public function __construct(IcalService $icalService) { parent::__construct(); - $this->contingentRepository = $contingentRepository; + $this->icalService = $icalService; } /** * @param Hotel $hotel - * @param string $year - * @param string $month - * @param int $months + * @return string */ public function contingentsAction(Hotel $hotel) { - $range = Period::after(new \DateTime('now'), '1 year'); - - $events = $this->contingentRepository->getEvents( - \DateTime::createFromImmutable($range->getStartDate()), - \DateTime::createFromImmutable($range->getEndDate()), - ['hotel' => $hotel] - ); - - $vcalendar = Vcalendar::factory([ Vcalendar::UNIQUE_ID => 'www.ep-reisen.de' ]); - - foreach ($events as $event) { - /** @var Contingent $event */ - $paxTotal = $event->getPax(); - $paxAvaiable = $event->getAvailable(); - - if ($paxTotal > 0) { - $percentageBooked = 100 - round($paxAvaiable / $paxTotal * 100); - } else { - $percentageBooked = 100; - } - - if ($percentageBooked >= 75) { - $dtStart = $event->getBegin(); - $dtEnd = clone $event->getBegin(); - $dtEnd->modify('+1 day'); - $vcalendar - ->newVevent() - ->setDtstart($dtStart) - ->setDtend($dtEnd) - ->setSummary('Unavailable') - ->setDescription(sprintf('Unavailable on %s', $dtStart->format('d M Y'))) - ; - } - } - header(sprintf('Content-disposition: attachment; filename=%s.ical', $hotel->getPathSegment())); - return $vcalendar->vtimezonePopulate()->createCalendar(); + return $this->icalService->createContingentsFeed($hotel); } - } diff --git a/public/typo3conf/ext/ep_products/Classes/Service/IcalService.php b/public/typo3conf/ext/ep_products/Classes/Service/IcalService.php new file mode 100644 index 00000000..89da44c6 --- /dev/null +++ b/public/typo3conf/ext/ep_products/Classes/Service/IcalService.php @@ -0,0 +1,147 @@ +, dreipunktnull + * + * All rights reserved + * + * This script is part of the TYPO3 project. The TYPO3 project is + * free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * The GNU General Public License can be found at + * http://www.gnu.org/copyleft/gpl.html. + * + * This script is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * This copyright notice MUST APPEAR in all copies of the script! + ***************************************************************/ + +use Doctrine\DBAL\DBALException; +use EP\EpProducts\Domain\Model\Contingent; +use EP\EpProducts\Domain\Model\Hotel; +use EP\EpProducts\Domain\Repository\ContingentRepository; +use Kigkonsult\Icalcreator\Vcalendar; +use League\Period\Period; +use TYPO3\CMS\Core\SingletonInterface; + +class IcalService implements SingletonInterface +{ + const THRESHOLD = 75; + + /** + * @var ContingentRepository + */ + protected $contingentRepository; + + /** + * @param ContingentRepository $contingentRepository + */ + public function __construct(ContingentRepository $contingentRepository) + { + $this->contingentRepository = $contingentRepository; + } + + /** + * @param Hotel $hotel + * @return string + */ + public function createContingentsFeed(Hotel $hotel): string + { + $range = Period::after(new \DateTime('now'), '1 year'); + try { + $events = $this->contingentRepository->getEvents( + \DateTime::createFromImmutable($range->getStartDate()), + \DateTime::createFromImmutable($range->getEndDate()), + ['hotel' => $hotel] + ); + } + catch (DBALException $e) { + $events = []; + } + + $vcalendar = $this->createCalendarInstance(); + + $dateStart = $dateEnd = null; + + foreach ($events as $event) { + /** @var Contingent $event */ + $paxTotal = $event->getPax(); + $paxAvaiable = $event->getAvailable(); + + if ($paxTotal > 0) { + $percentageBooked = 100 - round($paxAvaiable / $paxTotal * 100); + } else { + $percentageBooked = 100; + } + + if ($percentageBooked >= self::THRESHOLD) { + if (null !== $dateStart) { + continue; + } + $dateStart = $event->getBegin(); + } elseif (null !== $dateStart) { + $dateEnd = clone $event->getBegin(); + $dateEnd->modify('-1 day'); + $this->addCalendarItem($vcalendar, $dateStart, $dateEnd); + $dateStart = $dateEnd = null; + } + } + + try { + $feed = $vcalendar->vtimezonePopulate()->createCalendar(); + } + catch (\Exception $e) { + $feed = ''; + } + + return $feed; + } + + /** + * @param Vcalendar $vcalendar + * @param \DateTime $dateStart + * @param \DateTime $dateEnd + */ + protected function addCalendarItem(Vcalendar $vcalendar, \DateTime $dateStart, \DateTime $dateEnd): void + { + try { + $vcalendar + ->newVevent() + ->setDtstart($dateStart) + ->setDtend($dateEnd) + ->setSummary('Unavailable') + ->setDescription(sprintf( + 'Unavailable from %s to %s', + $dateStart->format('d M Y'), + $dateEnd->format('d.M Y') + )) + ; + } + catch (\Exception $e) { + } + } + + /** + * @return Vcalendar + */ + protected function createCalendarInstance(): Vcalendar + { + $vcalendar = Vcalendar::factory([ Vcalendar::UNIQUE_ID => 'www.ep-reisen.de' ]); + $vcalendar->setXprop( + Vcalendar::X_WR_TIMEZONE, + 'Europe/Berlin' + ); + + return $vcalendar; + } +} \ No newline at end of file