Skip to content
This repository has been archived by the owner on Jun 23, 2021. It is now read-only.

Commit

Permalink
Merge fbf63b5 into 6309a7a
Browse files Browse the repository at this point in the history
  • Loading branch information
Luc Wollants committed Feb 3, 2017
2 parents 6309a7a + fbf63b5 commit e30e5fc
Show file tree
Hide file tree
Showing 6 changed files with 308 additions and 3 deletions.
18 changes: 17 additions & 1 deletion bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use CultuurNet\UDB3\UiTPASService\Broadway\Saga\Metadata\StaticallyConfiguredSagaMetadataFactory;
use CultuurNet\UDB3\UiTPASService\Broadway\Saga\MultipleSagaManager;
use CultuurNet\UDB3\UiTPASService\Broadway\Saga\State\MongoDBRepository;
use CultuurNet\UDB3\UiTPASService\Broadway\Saga\State\StateCopier;
use CultuurNet\UDB3\UiTPASService\Broadway\Saga\State\StateManager;
use CultuurNet\UDB3\UiTPASService\OrganizerLabelReadRepository\JSONLDOrganizerLabelReadRepository;
use CultuurNet\UDB3\UiTPASService\Permissions\DefaultEventPermission;
Expand Down Expand Up @@ -463,6 +464,12 @@ function (Application $app) {
}
);

$app['uuid_generator'] = $app->share(
function () {
return new Broadway\UuidGenerator\Rfc4122\Version4Generator();
}
);

$app['saga_manager'] = $app->share(
function (Application $app) {
return new MultipleSagaManager(
Expand Down Expand Up @@ -498,6 +505,14 @@ function (Application $app) {
}
);

$app['state_copier'] = $app->share(
function (Application $app) {
return new StateCopier(
$app['uuid_generator']
);
}
);

$app['uitpas_event_saga'] = $app->share(
function (Application $app) {
$saga = new UiTPASEventSaga(
Expand All @@ -508,7 +523,8 @@ function (Application $app) {
array_values($app['config']['labels'])
),
$app['organizer_label_reader'],
$app['culturefeed_uitpas_client']
$app['culturefeed_uitpas_client'],
$app['state_copier']
);

$logger = new Logger('uitpas_event_saga');
Expand Down
55 changes: 55 additions & 0 deletions src/Broadway/Saga/State/StateCopier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace CultuurNet\UDB3\UiTPASService\Broadway\Saga\State;

use Broadway\Saga\State;
use Broadway\UuidGenerator\UuidGeneratorInterface;

class StateCopier implements StateCopierInterface
{
/**
* @var UuidGeneratorInterface
*/
private $generator;

/**
* StateCopier constructor.
* @param UuidGeneratorInterface $generator
*/
public function __construct(UuidGeneratorInterface $generator)
{
$this->generator = $generator;
}

/**
* Create a copy with new id.
*
* @param State $state
* @return State
*/
public function copy(State $state)
{
$stateAsArray = $state->serialize();
$stateAsArray['id'] = $this->generator->generate();

return State::deserialize($stateAsArray);
}

/**
* Create a copy with new id and applied values.
*
* @param State $state
* @param array $values
* @return State
*/
public function copyWithValues(State $state, array $values)
{
$copiedState = $this->copy($state);

foreach ($values as $key => $value) {
$copiedState->set($key, $value);
}

return $copiedState;
}
}
25 changes: 25 additions & 0 deletions src/Broadway/Saga/State/StateCopierInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace CultuurNet\UDB3\UiTPASService\Broadway\Saga\State;

use Broadway\Saga\State;

interface StateCopierInterface
{
/**
* Create a copy with new id.
*
* @param State $state
* @return State
*/
public function copy(State $state);

/**
* Create a copy with new id and applied values.
*
* @param State $state
* @param array $values
* @return State
*/
public function copyWithValues(State $state, array $values);
}
39 changes: 38 additions & 1 deletion src/UiTPASEventSaga.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use CultuurNet\UDB3\Cdb\EventItemFactory;
use CultuurNet\UDB3\Cdb\PriceDescriptionParser;
use CultuurNet\UDB3\Event\Events\Concluded;
use CultuurNet\UDB3\Event\Events\EventCopied;
use CultuurNet\UDB3\Event\Events\EventCreated;
use CultuurNet\UDB3\Event\Events\EventImportedFromUDB2;
use CultuurNet\UDB3\Event\Events\EventUpdatedFromUDB2;
Expand All @@ -25,6 +26,7 @@
use CultuurNet\UDB3\PriceInfo\Price;
use CultuurNet\UDB3\PriceInfo\PriceInfo;
use CultuurNet\UDB3\PriceInfo\Tariff;
use CultuurNet\UDB3\UiTPASService\Broadway\Saga\State\StateCopierInterface;
use CultuurNet\UDB3\UiTPASService\OrganizerLabelReadRepository\OrganizerLabelReadRepositoryInterface;
use CultuurNet\UDB3\UiTPASService\Sync\Command\RegisterUiTPASEvent;
use CultuurNet\UDB3\UiTPASService\Sync\Command\UpdateUiTPASEvent;
Expand Down Expand Up @@ -74,28 +76,36 @@ class UiTPASEventSaga extends Saga implements StaticallyConfiguredSagaInterface,
*/
private $cultureFeedUitpas;

/**
* @var StateCopierInterface
*/
private $stateCopier;

/**
* @param CommandBusInterface $commandBus
* @param EventCdbIdExtractorInterface $eventCdbIdExtractor
* @param PriceDescriptionParser $priceDescriptionParser
* @param LabelCollection $uitpasLabels
* @param OrganizerLabelReadRepositoryInterface $organizerLabelRepository
* @param \CultureFeed_Uitpas $cultureFeedUitpas
* @param StateCopierInterface $stateCopier
*/
public function __construct(
CommandBusInterface $commandBus,
EventCdbIdExtractorInterface $eventCdbIdExtractor,
PriceDescriptionParser $priceDescriptionParser,
LabelCollection $uitpasLabels,
OrganizerLabelReadRepositoryInterface $organizerLabelRepository,
\CultureFeed_Uitpas $cultureFeedUitpas
\CultureFeed_Uitpas $cultureFeedUitpas,
StateCopierInterface $stateCopier
) {
$this->commandBus = $commandBus;
$this->eventCdbIdExtractor = $eventCdbIdExtractor;
$this->priceDescriptionParser = $priceDescriptionParser;
$this->uitpasLabels = $uitpasLabels;
$this->organizerLabelRepository = $organizerLabelRepository;
$this->cultureFeedUitpas = $cultureFeedUitpas;
$this->stateCopier = $stateCopier;

$this->logger = new NullLogger();
}
Expand All @@ -109,6 +119,12 @@ public static function configuration()
return null;
};

$copiedEventCallback = function (EventCopied $eventCopied) {
return new Criteria(
['uitpasAggregateId' => $eventCopied->getOriginalEventId()]
);
};

$concludedEventCallback = function (Concluded $concluded) {
return new Criteria(
['uitpasAggregateId' => $concluded->getItemId()]
Expand Down Expand Up @@ -141,6 +157,7 @@ public static function configuration()

return [
EventCreated::class => $initialEventCallback,
EventCopied::class => $copiedEventCallback,
EventImportedFromUDB2::class => $initialEventCallback,
EventUpdatedFromUDB2::class => $updateFromUdb2Callback,
OrganizerUpdated::class => $offerEventCallback,
Expand All @@ -166,6 +183,26 @@ public function handleEventCreated(EventCreated $eventCreated, State $state)
return $state;
}

/**
* @param EventCopied $eventCopied
* @param State $state
* @return State
*/
public function handleEventCopied(EventCopied $eventCopied, State $state)
{
$copiedState = $this->stateCopier->copyWithValues(
$state,
[
'uitpasAggregateId' => $eventCopied->getItemId(),
'syncCount' => 0,
]
);

$this->triggerSyncWhenConditionsAreMet($copiedState);

return $copiedState;
}

/**
* @param EventImportedFromUDB2 $eventImportedFromUDB2
* @param State $state
Expand Down
103 changes: 103 additions & 0 deletions tests/Broadway/Saga/State/StateCopierTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace CultuurNet\UDB3\UiTPASService\Broadway\Saga\State;

use Broadway\Saga\State;
use Broadway\UuidGenerator\UuidGeneratorInterface;

class StateCopierTest extends \PHPUnit_Framework_TestCase
{
/**
* @var string
*/
private $id;

/**
* @var State
*/
private $state;

/**
* @var UuidGeneratorInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $generator;

/**
* @var StateCopier
*/
private $stateCopier;

protected function setUp()
{
$this->id = '6cd53be3-7512-4a64-98b9-b51b2ed6988d';

$this->state = $this->createStateWithValues(
'e9d11f11-9579-48c3-b531-33deaebc5947'
);

$this->generator = $this->createMock(UuidGeneratorInterface::class);

$this->generator->expects($this->once())
->method('generate')
->willReturn($this->id);

$this->stateCopier = new StateCopier($this->generator);
}

/**
* @test
*/
public function it_can_copy_a_state()
{
$this->state->setDone();

$copiedState = $this->stateCopier->copy($this->state);

$expectedState = $this->createStateWithValues($this->id);
$expectedState->setDone();

$this->assertEquals($expectedState, $copiedState);
}

/**
* @test
*/
public function it_can_copy_a_state_with_values()
{
$extraValues = [
'key3' => 'value3',
'key4' => 'value4',
];

$copiedState = $this->stateCopier->copyWithValues(
$this->state,
$extraValues
);

$expectedState = $this->createStateWithValues(
$this->id,
$extraValues
);

$this->assertEquals($expectedState, $copiedState);
}

/**
* @param string $id
* @param array $extraValues
* @return State
*/
private function createStateWithValues($id, array $extraValues = [])
{
$state = new State($id);

$state->set('key1', 'value1');
$state->set('key2', 'value2');

foreach ($extraValues as $key => $extraValue) {
$state->set($key, $extraValue);
}

return $state;
}
}
Loading

0 comments on commit e30e5fc

Please sign in to comment.