Use hotel uid in groups.swiss export command, code cleanup

This commit is contained in:
Björn Fromme
2021-06-26 15:20:13 +02:00
parent 9ae808e305
commit 45365a4872
3 changed files with 28 additions and 36 deletions
@@ -58,12 +58,12 @@ class GroupsSwissCommand extends Command
$this->addArgument( $this->addArgument(
'hotel', 'hotel',
InputArgument::REQUIRED, InputArgument::REQUIRED,
'E&P hotel code' 'E&P hotel uid'
); );
$this->addArgument( $this->addArgument(
'house', 'house',
InputArgument::REQUIRED, InputArgument::REQUIRED,
'House number' 'Groups.swiss house number'
); );
$this->addArgument( $this->addArgument(
'key', 'key',
@@ -74,11 +74,11 @@ class GroupsSwissCommand extends Command
public function execute(InputInterface $input, OutputInterface $output) public function execute(InputInterface $input, OutputInterface $output)
{ {
$hotelCode = $input->getArgument('hotel'); $hotelUid = $input->getArgument('hotel');
$houseNumber = $input->getArgument('house'); $houseNumber = $input->getArgument('house');
$apiKey = $input->getArgument('key'); $apiKey = $input->getArgument('key');
$success = $this->groupsSwissService->publishContingents($hotelCode, $houseNumber, $apiKey); $success = $this->groupsSwissService->publishContingents($hotelUid, $houseNumber, $apiKey);
return $success ? 1 : 0; return $success ? 1 : 0;
} }
@@ -27,7 +27,9 @@ namespace EP\EpProducts\Domain\Repository;
* This copyright notice MUST APPEAR in all copies of the script! * This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/ ***************************************************************/
use CalendR\Event\EventInterface;
use CalendR\Event\Provider\ProviderInterface; use CalendR\Event\Provider\ProviderInterface;
use Doctrine\DBAL\DBALException;
use EP\EpProducts\Domain\Model\Contingent; use EP\EpProducts\Domain\Model\Contingent;
use EP\EpProducts\Domain\Model\Hotel; use EP\EpProducts\Domain\Model\Hotel;
use EP\EpProducts\Domain\Model\Product; use EP\EpProducts\Domain\Model\Product;
@@ -37,23 +39,22 @@ class ContingentRepository extends AbstractRepository implements ProviderInterfa
{ {
/** /**
* @param \DateTime $dateFrom * @param \DateTime $begin
* @param \DateTime $dateTo * @param \DateTime|null $end
* @param array $options * @param array $options
* @return \CalendR\Event\EventInterface[] * @return EventInterface[]
* @throws \Doctrine\DBAL\DBALException * @throws DBALException
*/ */
public function getEvents(\DateTime $dateFrom, \DateTime $dateTo = null, array $options = []) public function getEvents(\DateTime $begin, \DateTime $end = null, array $options = []): array
{ {
$optionsResolver = new OptionsResolver(); $optionsResolver = new OptionsResolver();
$optionsResolver->setRequired(['hotelCode']); $optionsResolver->setRequired(['hotel']);
$optionsResolver->setDefined(['hotel']);
$optionsResolver->setDefaults(['hotel' => null]);
$optionsResolver->setAllowedTypes('hotel', Hotel::class); $optionsResolver->setAllowedTypes('hotel', Hotel::class);
$resolvedOptions = $optionsResolver->resolve($options); $resolvedOptions = $optionsResolver->resolve($options);
/** @var Hotel $hotel */
$hotel = $resolvedOptions['hotel']; $hotel = $resolvedOptions['hotel'];
$hotelCode = $resolvedOptions['hotelCode']; $hotelCode = $hotel->getCode();
$qb = $this->getDbConnection()->createQueryBuilder(); $qb = $this->getDbConnection()->createQueryBuilder();
@@ -63,14 +64,14 @@ class ContingentRepository extends AbstractRepository implements ProviderInterfa
->where('hotel_code = :hotelCode') ->where('hotel_code = :hotelCode')
->andWhere('date >= :dateFrom') ->andWhere('date >= :dateFrom')
->setParameter('hotelCode', $hotelCode) ->setParameter('hotelCode', $hotelCode)
->setParameter('dateFrom', $dateFrom->format('Y-m-d')) ->setParameter('dateFrom', $begin->format('Y-m-d'))
->orderBy('date') ->orderBy('date')
; ;
if (null !== $dateTo) { if (null !== $end) {
$qb $qb
->andWhere('date <= :dateTo') ->andWhere('date <= :dateTo')
->setParameter('dateTo', $dateTo->format('Y-m-d')) ->setParameter('dateTo', $end->format('Y-m-d'))
; ;
} }
@@ -101,9 +102,7 @@ class ContingentRepository extends AbstractRepository implements ProviderInterfa
foreach ($dates as $row) foreach ($dates as $row)
{ {
if ($hotel) {
$row['hotel'] = $hotel; $row['hotel'] = $hotel;
}
$events[] = Contingent::fromArray($row); $events[] = Contingent::fromArray($row);
} }
@@ -114,7 +113,7 @@ class ContingentRepository extends AbstractRepository implements ProviderInterfa
* @param Hotel $hotel * @param Hotel $hotel
* @param Product $product * @param Product $product
* @return array * @return array
* @throws \Doctrine\DBAL\DBALException * @throws DBALException
*/ */
public function getAvailableContingents(Hotel $hotel, Product $product) public function getAvailableContingents(Hotel $hotel, Product $product)
{ {
@@ -147,7 +146,7 @@ class ContingentRepository extends AbstractRepository implements ProviderInterfa
* @param Hotel $hotel * @param Hotel $hotel
* @param Product $product * @param Product $product
* @return array * @return array
* @throws \Doctrine\DBAL\DBALException * @throws DBALException
*/ */
public function getAvailableRooms(\DateTime $dateFrom, \DateTime $dateTo, Hotel $hotel, Product $product) public function getAvailableRooms(\DateTime $dateFrom, \DateTime $dateTo, Hotel $hotel, Product $product)
{ {
@@ -37,8 +37,6 @@ use Psr\Log\LoggerAwareTrait;
use Symfony\Component\HttpClient\HttpClient; use Symfony\Component\HttpClient\HttpClient;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use TYPO3\CMS\Core\SingletonInterface; use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Configuration\Exception;
class GroupsSwissService implements SingletonInterface, LoggerAwareInterface class GroupsSwissService implements SingletonInterface, LoggerAwareInterface
{ {
@@ -62,26 +60,22 @@ class GroupsSwissService implements SingletonInterface, LoggerAwareInterface
/** /**
* @param HotelRepository $hotelRepository * @param HotelRepository $hotelRepository
* @param ContingentRepository $contingentRepository * @param ContingentRepository $contingentRepository
* @param ConfigurationManagerInterface $configurationManager
*/ */
public function __construct( public function __construct(HotelRepository $hotelRepository, ContingentRepository $contingentRepository)
HotelRepository $hotelRepository, {
ContingentRepository $contingentRepository,
ConfigurationManagerInterface $configurationManager
) {
$this->hotelRepository = $hotelRepository; $this->hotelRepository = $hotelRepository;
$this->contingentRepository = $contingentRepository; $this->contingentRepository = $contingentRepository;
$this->httpClient = HttpClient::create(); $this->httpClient = HttpClient::create();
} }
public function publishContingents(string $hotelCode, int $houseNumber, string $apiKey): bool public function publishContingents(int $hotelUid, int $houseNumber, string $apiKey): bool
{ {
$range = Period::after(new \DateTime('now'), '1 year'); $range = Period::after(new \DateTime('now'), '1 year');
$result = $this->hotelRepository->findByCode($hotelCode); $result = $this->hotelRepository->findByIdentifier($hotelUid);
if (null === $result) { if (null === $result) {
$this->logger->error(sprintf('Hotel with code %s not found', $hotelCode)); $this->logger->error(sprintf('Hotel with uid %d not found', $hotelUid));
return false; return false;
} }
@@ -92,11 +86,12 @@ class GroupsSwissService implements SingletonInterface, LoggerAwareInterface
$events = $this->contingentRepository->getEvents( $events = $this->contingentRepository->getEvents(
\DateTime::createFromImmutable($range->getStartDate()), \DateTime::createFromImmutable($range->getStartDate()),
\DateTime::createFromImmutable($range->getEndDate()), \DateTime::createFromImmutable($range->getEndDate()),
['hotel' => $hotel, 'hotelCode' => $hotelCode] ['hotel' => $hotel]
); );
} }
catch (DBALException $e) { catch (DBALException $e) {
$events = []; $this->logger->error(sprintf('No dates found for hotel with uid %d', $hotelUid));
return false;
} }
$this->resetChart($range, $apiKey, $houseNumber); $this->resetChart($range, $apiKey, $houseNumber);
@@ -134,7 +129,6 @@ class GroupsSwissService implements SingletonInterface, LoggerAwareInterface
*/ */
protected function generateChart(array $events, $apiKey, $houseNumber): void protected function generateChart(array $events, $apiKey, $houseNumber): void
{ {
$itemCount = 0;
$item = null; $item = null;
$status = null; $status = null;
@@ -151,7 +145,6 @@ class GroupsSwissService implements SingletonInterface, LoggerAwareInterface
$statusCode = Contingent::STATUS_ONREQUEST === $status ? 1 : 2; $statusCode = Contingent::STATUS_ONREQUEST === $status ? 1 : 2;
$this->addItem($item, $statusCode, $apiKey, $houseNumber); $this->addItem($item, $statusCode, $apiKey, $houseNumber);
$item = null; $item = null;
$itemCount++;
} }
} }
} }