Skip to content

Commit

Permalink
Add PSR-14 Event to modify event objects prior persistence (#35)
Browse files Browse the repository at this point in the history
A new PSR-14 event is added that allows to modify the event right before
it is persisted within Destination Data One import.

This for example allows to alter dates, prices, etc.

Improve handling of missing end time in dates.
  • Loading branch information
DanielSiepmann committed Aug 10, 2023
1 parent 05f3ec4 commit 3d0d5d8
Show file tree
Hide file tree
Showing 19 changed files with 491 additions and 18 deletions.
14 changes: 8 additions & 6 deletions Classes/Domain/Model/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Date extends AbstractEntity
protected $start;

/**
* @var \DateTime
* @var \DateTime|null
*/
protected $end;

Expand Down Expand Up @@ -73,31 +73,33 @@ public function getHasUsefulStartTime(): bool
}

/**
* @return \DateTime end
* @return \DateTime|null end
*/
public function getEnd()
{
return $this->end;
}

/**
* @param \DateTime $end
* @param \DateTime|null $end
*
* @return void
*/
public function setEnd(\DateTime $end)
public function setEnd($end)
{
$this->end = $end;
}

public function getHasUsefulEndTime(): bool
{
return $this->getEnd()->format('H:i') !== '23:59';
$end = $this->getEnd();
return $end && $end->format('H:i') !== '23:59';
}

public function getEndsOnSameDay(): bool
{
return $this->getStart()->format('Y-m-d') === $this->getEnd()->format('Y-m-d');
$end = $this->getEnd();
return $end && $this->getStart()->format('Y-m-d') === $end->format('Y-m-d');
}

/**
Expand Down
18 changes: 17 additions & 1 deletion Classes/Service/DestinationDataImportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Wrm\Events\Service;

use Exception;
use TYPO3\CMS\Core\EventDispatcher\EventDispatcher;
use TYPO3\CMS\Core\Log\Logger;
use TYPO3\CMS\Core\Log\LogManager;
use TYPO3\CMS\Core\Utility\GeneralUtility;
Expand All @@ -22,6 +23,7 @@
use Wrm\Events\Service\DestinationDataImportService\CategoriesAssignment\Import as CategoryImport;
use Wrm\Events\Service\DestinationDataImportService\DataFetcher;
use Wrm\Events\Service\DestinationDataImportService\DatesFactory;
use Wrm\Events\Service\DestinationDataImportService\Events\EventImportEvent;
use Wrm\Events\Service\DestinationDataImportService\FilesAssignment;
use Wrm\Events\Service\DestinationDataImportService\LocationAssignment;
use Wrm\Events\Service\DestinationDataImportService\Slugger;
Expand Down Expand Up @@ -108,6 +110,11 @@ class DestinationDataImportService
*/
private $cacheManager;

/**
* @var EventDispatcher
*/
private $eventDispatcher;

/**
* ImportService constructor.
*
Expand All @@ -123,6 +130,7 @@ class DestinationDataImportService
* @param LocationAssignment $locationAssignment
* @param Slugger $slugger
* @param CacheManager $cacheManager
* @param EventDispatcher $eventDispatcher
*/
public function __construct(
EventRepository $eventRepository,
Expand All @@ -137,7 +145,8 @@ public function __construct(
CategoriesAssignment $categoriesAssignment,
LocationAssignment $locationAssignment,
Slugger $slugger,
CacheManager $cacheManager
CacheManager $cacheManager,
EventDispatcher $eventDispatcher
) {
$this->eventRepository = $eventRepository;
$this->organizerRepository = $organizerRepository;
Expand All @@ -152,6 +161,7 @@ public function __construct(
$this->locationAssignment = $locationAssignment;
$this->slugger = $slugger;
$this->cacheManager = $cacheManager;
$this->eventDispatcher = $eventDispatcher;
}

public function import(
Expand Down Expand Up @@ -198,6 +208,7 @@ public function processData(array $data): int

// Event already exists? If not create one!
$this->tmpCurrentEvent = $this->getOrCreateEvent($event['global_id'], $event['title']);
$existingEvent = clone $this->tmpCurrentEvent;

// Set language UID
$this->tmpCurrentEvent->setLanguageUid(-1);
Expand Down Expand Up @@ -266,6 +277,11 @@ public function processData(array $data): int
$this->setAssets($event['media_objects']);
}

$this->eventDispatcher->dispatch(new EventImportEvent(
$existingEvent,
$this->tmpCurrentEvent
));

// Update and persist
$this->logger->info('Persist database');
$this->eventRepository->update($this->tmpCurrentEvent);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

/*
* Copyright (C) 2023 Daniel Siepmann <coding@daniel-siepmann.de>
*
* This program 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 2
* of the License, or (at your option) any later version.
*
* This program 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/

namespace Wrm\Events\Service\DestinationDataImportService\Events;

use Wrm\Events\Domain\Model\Event;

final class EventImportEvent
{
/**
* @var Event
*/
private $existingEvent;

/**
* @var Event
*/
private $eventToImport;

public function __construct(
Event $existingEvent,
Event $eventToImport
) {
$this->existingEvent = $existingEvent;
$this->eventToImport = $eventToImport;
}

public function getBaseEvent(): Event
{
return clone $this->existingEvent;
}

public function getEventToImport(): Event
{
return $this->eventToImport;
}
}
2 changes: 1 addition & 1 deletion Classes/Updates/MigrateDuplicateLocations.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private function getMatchingLocation(
return 0;
}

return (int) $uid;
return (int)$uid;
}

private function buildObject(array $location): Location
Expand Down
38 changes: 38 additions & 0 deletions Documentation/Changelog/3.5.0.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
3.5.0
=====

Breaking
--------

Nothing

Features
--------

* Add PSR-14 Events to Destination Data One import.
They allow individual installation to alter the import.

See :ref:`psr14` for an overview of PSR-14 Events.

Added Events:

* Allow to modify an event object before importing.

Fixes
-----

* Improve handling of dates with no end.
This was always technically possible. Still support from extension was missing.
This now got improved, the date object will not throw exceptions due to ``format()`` calls on none.
Furthermore the PHPDoc now reflects that there might be null instead of ``\DateTime``.
Also the setter was adjusted to allow ``null`` values.

Tasks
-----

Nothing

Deprecation
-----------

Nothing
13 changes: 13 additions & 0 deletions Documentation/Features/PSR14Events.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.. index:: single: PSR-14 Events
.. _psr14:

PSR-14 Events
=============

.. index:: single: PSR-14 Events; Destination Data One Import: Event Import

Destination Data One Import: ``EventImportEvent``
-------------------------------------------------

Executed during Destination Data One Import.
Allows to alter the event prior persistence.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ tx_events_domain_model_event
tx_events_domain_model_date
,uid,pid,event,start,end
,1,2,1,1676419200,1676484000
,2,2,2,1676419200,0
,2,2,2,1676419200,"\NULL"
,3,2,3,1676678400,1676743200
,4,2,4,1676678400,0
,4,2,4,1676678400,"\NULL"
,5,2,5,1676419200,1676678400
,6,2,6,1676559600,1676570400
,7,2,7,1676559600,1676678400
,8,2,8,1676559600,0
,8,2,8,1676559600,"\NULL"
,9,2,9,1676419200,1676570400
4 changes: 1 addition & 3 deletions Tests/Functional/Import/DestinationDataTest/AbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ abstract class AbstractTest extends AbstractFunctionalTestCase
{
protected function setUp(): void
{
$this->coreExtensionsToLoad = [
'filemetadata',
];
$this->coreExtensionsToLoad[] = 'filemetadata';

parent::setUp();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

return [
'tx_events_domain_model_event' => [
[
'uid' => 1,
'pid' => 2,
'title' => 'Event for modifying event',
'global_id' => 'e_100350503',
'dates' => 2,
],
],
'tx_events_domain_model_date' => [
[
'uid' => 1,
'pid' => 2,
'event' => 1,
'start' => 4097728800,
'end' => null,
],
[
'uid' => 2,
'pid' => 2,
'event' => 1,
'start' => 4097815200,
'end' => null,
],
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

/*
* Copyright (C) 2023 Daniel Siepmann <coding@daniel-siepmann.de>
*
* This program 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 2
* of the License, or (at your option) any later version.
*
* This program 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/

namespace Wrm\Events\Tests\Functional\Psr14Events\DestinationDataImport;

use GuzzleHttp\Psr7\Response;
use Wrm\Events\Tests\Functional\Import\DestinationDataTest\AbstractTest;

final class EventImportEventTest extends AbstractTest
{
protected function setUp(): void
{
$this->testExtensionsToLoad[] = 'typo3conf/ext/events/Tests/Functional/Psr14Events/DestinationDataImport/Fixtures/Extensions/custom_event';

parent::setUp();

$this->setUpConfiguration([
'restUrl = https://example.com/some-path/',
'license = example-license',
'restType = Event',
'restLimit = 3',
'restMode = next_months,12',
'restTemplate = ET2014A.json',
]);
}

/**
* @test
*/
public function registeredEventHandlerCanModifyEvent(): void
{
$this->importPHPDataSet(__DIR__ . '/Fixtures/Database/RegisteredEventHandlerCanModifyEvent.php');
$this->setUpResponses([new Response(200, [], file_get_contents(__DIR__ . '/Fixtures/Responses/RegisteredEventHandlerCanModifyEvent.json') ?: '')]);

$this->executeCommand();

$this->assertPHPDataSet(__DIR__ . '/Assertions/RegisteredEventHandlerCanModifyEvent.php');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

return [
'tx_events_domain_model_import' => [
[
'uid' => '1',
'pid' => '2',
'title' => 'Example import configuration',
'storage_pid' => 2,
'files_folder' => '1:/staedte/beispielstadt/events/',
'region' => '1',
'rest_experience' => 'beispielstadt',
],
],
];
Loading

0 comments on commit 3d0d5d8

Please sign in to comment.