Skip to content

Commit

Permalink
Remove transaction handling from ConfigurationCommandHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
DRvanR committed Mar 23, 2015
1 parent cbff982 commit e522446
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 196 deletions.
21 changes: 0 additions & 21 deletions src/Surfnet/Stepup/Configuration/Api/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,4 @@ public static function create();
* @return void
*/
public function update($newConfiguration);

/**
* Used to be able to update the gateway configuration within a single transaction.
*
* @return null|\Surfnet\Stepup\Configuration\Event\ServiceProvidersUpdatedEvent
*/
public function getLastUncommittedServiceProvidersUpdatedEvent();

/**
* Used to be able to update the raa configuration within a single transaction.
*
* @return null|\Surfnet\Stepup\Configuration\Event\RaaUpdatedEvent
*/
public function getLastUncommittedRaaUpdatedEvent();

/**
* Used to be able to update the raa configuration within a single transaction.
*
* @return null|\Surfnet\Stepup\Configuration\Event\SraaUpdatedEvent
*/
public function getLastUncommittedSraaUpdatedEvent();
}
62 changes: 4 additions & 58 deletions src/Surfnet/Stepup/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,6 @@ class Configuration extends EventSourcedAggregateRoot implements ConfigurationIn
*/
private $configuration;

/**
* @var null|\Surfnet\Stepup\Configuration\Event\ServiceProvidersUpdatedEvent
*/
private $lastServiceProvidersUpdatedEvent;

/**
* @var null|\Surfnet\Stepup\Configuration\Event\RaaUpdatedEvent
*/
private $lastRaaUpdatedEvent;

/**
* @var null|\Surfnet\Stepup\Configuration\Event\SraaUpdatedEvent
*/
private $lastSraaUpdatedEvent;

public static function create()
{
$configuration = new self();
Expand All @@ -72,24 +57,12 @@ public function update($configurationAsJson)
$this->configuration
));

$this->lastServiceProvidersUpdatedEvent = new ServiceProvidersUpdatedEvent(
$this->apply(new ServiceProvidersUpdatedEvent(
self::CONFIGURATION_ID,
$decodedConfiguration['gateway']['service_providers']
);

$this->lastRaaUpdatedEvent = new RaaUpdatedEvent(
self::CONFIGURATION_ID,
$decodedConfiguration['raa']
);

$this->lastSraaUpdatedEvent = new SraaUpdatedEvent(
self::CONFIGURATION_ID,
$decodedConfiguration['sraa']
);

$this->apply($this->lastServiceProvidersUpdatedEvent);
$this->apply($this->lastRaaUpdatedEvent);
$this->apply($this->lastSraaUpdatedEvent);
));
$this->apply(new RaaUpdatedEvent(self::CONFIGURATION_ID, $decodedConfiguration['raa']));
$this->apply(new SraaUpdatedEvent(self::CONFIGURATION_ID, $decodedConfiguration['sraa']));
}

public function getAggregateRootId()
Expand All @@ -101,31 +74,4 @@ public function applyConfigurationUpdatedEvent(ConfigurationUpdatedEvent $event)
{
$this->configuration = $event->newConfiguration;
}

public function getLastUncommittedServiceProvidersUpdatedEvent()
{
return $this->lastServiceProvidersUpdatedEvent;
}

public function getLastUncommittedRaaUpdatedEvent()
{
return $this->lastRaaUpdatedEvent;
}

public function getLastUncommittedSraaUpdatedEvent()
{
return $this->lastSraaUpdatedEvent;
}

/**
* {@inheritDoc} Cleaning up the possible event, as the uncommittedEvents will be removed as well
*/
public function getUncommittedEvents()
{
$this->lastServiceProvidersUpdatedEvent = null;
$this->lastRaaUpdatedEvent = null;
$this->lastSraaUpdatedEvent = null;

return parent::getUncommittedEvents();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(RaaRepository $raaRepository)
/**
* @param RaaUpdatedEvent $event
*/
public function updateRaaConfiguration(RaaUpdatedEvent $event)
public function applyRaaUpdatedEvent(RaaUpdatedEvent $event)
{
foreach ($event->raas as $institution => $raaList) {
$this->updateRaaListForInstitution($institution, $raaList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(SraaRepository $raaRepository)
/**
* @param SraaUpdatedEvent $event
*/
public function replaceSraaConfiguration(SraaUpdatedEvent $event)
public function applySraaUpdatedEvent(SraaUpdatedEvent $event)
{
$this->sraaRepository->removeAll();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,9 @@

use Broadway\CommandHandling\CommandHandler;
use Broadway\Repository\AggregateNotFoundException;
use Doctrine\DBAL\Driver\Connection;
use Surfnet\Stepup\Configuration\Configuration;
use Surfnet\Stepup\Configuration\EventSourcing\ConfigurationRepository;
use Surfnet\StepupMiddleware\ApiBundle\Identity\Projector\RaaProjector;
use Surfnet\StepupMiddleware\ApiBundle\Identity\Projector\SraaProjector;
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\UpdateConfigurationCommand;
use Surfnet\StepupMiddleware\GatewayBundle\Service\GatewayConfigurationService;

class ConfigurationCommandHandler extends CommandHandler
{
Expand All @@ -35,53 +31,12 @@ class ConfigurationCommandHandler extends CommandHandler
*/
private $repository;

/**
* @var \Surfnet\StepupMiddleware\GatewayBundle\Service\GatewayConfigurationService
*/
private $gatewayConfigurationService;

/**
* @var \Surfnet\StepupMiddleware\ApiBundle\Identity\Projector\RaaProjector
*/
private $raaProjector;

/**
* @var \Surfnet\StepupMiddleware\ApiBundle\Identity\Projector\SraaProjector
*/
private $sraaProjector;

/**
* @var \Doctrine\DBAL\Driver\Connection
*/
private $middlewareConnection;

/**
* @var \Doctrine\DBAL\Driver\Connection
*/
private $gatewayConnection;

/**
* @param ConfigurationRepository $repository
* @param GatewayConfigurationService $gatewayConfigurationService
* @param RaaProjector $raaProjector
* @param SraaProjector $sraaProjector
* @param Connection $middlewareConnection
* @param Connection $gatewayConnection
*/
public function __construct(
ConfigurationRepository $repository,
GatewayConfigurationService $gatewayConfigurationService,
RaaProjector $raaProjector,
SraaProjector $sraaProjector,
Connection $middlewareConnection,
Connection $gatewayConnection
) {
public function __construct(ConfigurationRepository $repository)
{
$this->repository = $repository;
$this->gatewayConfigurationService = $gatewayConfigurationService;
$this->raaProjector = $raaProjector;
$this->sraaProjector = $sraaProjector;
$this->middlewareConnection = $middlewareConnection;
$this->gatewayConnection = $gatewayConnection;
}

public function handleUpdateConfigurationCommand(UpdateConfigurationCommand $command)
Expand All @@ -91,31 +46,9 @@ public function handleUpdateConfigurationCommand(UpdateConfigurationCommand $com
$configuration = Configuration::create();
}

$this->middlewareConnection->beginTransaction();
$this->gatewayConnection->beginTransaction();

try {
$configuration->update($command->configuration);

$event = $configuration->getLastUncommittedServiceProvidersUpdatedEvent();
$this->gatewayConfigurationService->updateServiceProviders($event->serviceProviders);

$event = $configuration->getLastUncommittedRaaUpdatedEvent();
$this->raaProjector->updateRaaConfiguration($event);

$event = $configuration->getLastUncommittedSraaUpdatedEvent();
$this->sraaProjector->replaceSraaConfiguration($event);

$this->repository->add($configuration);
} catch (\Exception $e) {
$this->middlewareConnection->rollBack();
$this->gatewayConnection->rollBack();

throw $e;
}
$configuration->update($command->configuration);

$this->middlewareConnection->commit();
$this->gatewayConnection->commit();
$this->repository->add($configuration);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,5 @@ services:
class: Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\CommandHandler\ConfigurationCommandHandler
arguments:
- @surfnet_stepup.repository.configuration
- @surfnet_stepup_middleware_gateway.service.gateway_configuration
- @surfnet_stepup_middleware_api.projector.raa
- @surfnet_stepup_middleware_api.projector.sraa
- @doctrine.dbal.middleware_connection
- @doctrine.dbal.gateway_connection
tags: [{ name: command_bus.command_handler }]

Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ class SurfnetStepupMiddlewareGatewayExtension extends Extension
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
/*$config = */$this->processConfiguration($configuration, $configs);
$this->processConfiguration($configuration, $configs);

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
$loader->load('repositories.yml');
$loader->load('projection.yml');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,41 +37,28 @@ public function removeAll()
}

/**
* Replace all configured SamlEntiies with the new SamlEntities.
* Replace all configured SamlEntities with the new SamlEntities.
*
* Will be updated later, see https://www.pivotaltracker.com/story/show/83532704
*
* @param $newSamlEntities
* @throws Exception
* @throws \Doctrine\DBAL\ConnectionException
* @param array $newSamlEntities
*/
public function replaceAll($newSamlEntities)
public function replaceAll(array $newSamlEntities)
{
$connection = $this->getEntityManager()->getConnection();
$connection->beginTransaction();
$entityManager = $this->getEntityManager();
$counter = 0;

try {
$this->removeAll();
$entityManager->flush();
$this->removeAll();
$entityManager->flush();

foreach ($newSamlEntities as $samlEntity) {
$entityManager->persist($samlEntity);
foreach ($newSamlEntities as $samlEntity) {
$entityManager->persist($samlEntity);

if (++$counter % 25 === 0) {
$entityManager->flush();
$counter = 0;
}
if (++$counter % 25 === 0) {
$entityManager->flush();
}

$entityManager->flush();
} catch (Exception $e) {
$connection->rollBack();

throw $e;
}

$connection->commit();
$entityManager->flush();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,39 @@
* limitations under the License.
*/

namespace Surfnet\StepupMiddleware\GatewayBundle\Service;
namespace Surfnet\StepupMiddleware\GatewayBundle\Projector;

use Broadway\ReadModel\Projector;
use Doctrine\Common\Collections\ArrayCollection;
use Surfnet\Stepup\Configuration\Event\ServiceProvidersUpdatedEvent;
use Surfnet\StepupMiddleware\GatewayBundle\Entity\SamlEntity;
use Surfnet\StepupMiddleware\GatewayBundle\Entity\SamlEntityRepository;

class GatewayConfigurationService extends Projector
class SamlEntityProjector extends Projector
{
/**
* @var \Surfnet\StepupMiddleware\GatewayBundle\Entity\SamlEntityRepository
* @var SamlEntityRepository
*/
private $samlEntityRepository;

/**
* @param SamlEntityRepository $samlEntityRepository
*/
public function __construct(SamlEntityRepository $samlEntityRepository)
{
$this->samlEntityRepository = $samlEntityRepository;
}

/**
* @param array $serviceProviderConfigurations
* @param ServiceProvidersUpdatedEvent $event
*/
public function updateServiceProviders(array $serviceProviderConfigurations)
public function applyServiceProvidersUpdatedEvent(ServiceProvidersUpdatedEvent $event)
{
$spConfigurations = new ArrayCollection();
foreach ($serviceProviderConfigurations as $configuration) {
$spConfigurations = [];
foreach ($event->serviceProviders as $configuration) {
$newConfiguration = $configuration;
unset($newConfiguration['entity_id']);

$spConfigurations->add(SamlEntity::createServiceProvider($configuration['entity_id'], $newConfiguration));
$spConfigurations[] = SamlEntity::createServiceProvider($configuration['entity_id'], $newConfiguration);
}

$this->samlEntityRepository->replaceAll($spConfigurations);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
services:
surfnet_stepup_middleware_gateway.service.gateway_configuration:
class: Surfnet\StepupMiddleware\GatewayBundle\Service\GatewayConfigurationService
arguments:
- @middleware.gateway_repository.saml_entities

surfnet_stepup_middleware_gateway.projector.second_factor:
class: Surfnet\StepupMiddleware\GatewayBundle\Projector\SecondFactorProjector
arguments:
- @middleware.gateway_repository.second_factors
tags: [{ name: event_bus.event_listener }]

surfnet_stepup_middleware_gateway.projector.saml_entity:
class: Surfnet\StepupMiddleware\GatewayBundle\Projector\SamlEntityProjector
arguments:
- @middleware.gateway_repository.saml_entities
tags: [{ name: event_bus.event_listener }]

0 comments on commit e522446

Please sign in to comment.