Skip to content

Commit

Permalink
Add PSR-14 Event to modify event objects prior persistence
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.

Relates: #10597
  • Loading branch information
DanielSiepmann committed Aug 10, 2023
1 parent 00946af commit 1782bee
Show file tree
Hide file tree
Showing 15 changed files with 414 additions and 8 deletions.
8 changes: 4 additions & 4 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,19 +73,19 @@ 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;
}
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
34 changes: 34 additions & 0 deletions Documentation/Changelog/3.5.0.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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
-----

Nothing

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
@@ -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',
],
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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 WerkraumMedia\CustomEvent\EventListener;

use Wrm\Events\Service\DestinationDataImportService\Events\EventImportEvent;

final class EventImportListener
{
public function __invoke(EventImportEvent $psr14Event): void
{
$eventToImport = $psr14Event->getEventToImport();
$dates = $psr14Event->getEventToImport()->getDates();
foreach ($dates as $date) {
$endToSkip = clone $date->getStart();
$endToSkip->modify('+15 minutes');
if ($date->getEnd() == $endToSkip) {
$date->setEnd(null);
}
}
$eventToImport->setDates($dates);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use WerkraumMedia\CustomEvent\EventListener\EventImportListener;
use Wrm\Events\Service\DestinationDataImportService\Events\EventImportEvent;

return static function (ContainerConfigurator $containerConfigurator) {
$services = $containerConfigurator->services()
->defaults()
->autowire()
->autoconfigure()
;

$services->load('WerkraumMedia\\CustomEvent\\', '../Classes/');
$services->set(EventImportListener::class)
->tag(
'event.listener',
[
'event' => EventImportEvent::class,
]
)
;
};
Loading

0 comments on commit 1782bee

Please sign in to comment.