Skip to content

Commit

Permalink
Merge pull request #145 from SURFnet/feature/institution-configuratio…
Browse files Browse the repository at this point in the history
…n-options-read

Institution configuration options read-side
  • Loading branch information
Alex Rothuis authored Jul 28, 2016
2 parents 56903df + cf1d35c commit 56e3926
Show file tree
Hide file tree
Showing 12 changed files with 693 additions and 0 deletions.
34 changes: 34 additions & 0 deletions app/DoctrineMigrations/Version20160728074605.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Surfnet\StepupMiddleware\Migrations;

use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;

/**
* Auto-generated Migration: Please modify to your needs!
*/
class Version20160728074605 extends AbstractMigration
{
/**
* @param Schema $schema
*/
public function up(Schema $schema)
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.');

$this->addSql('CREATE TABLE institution_configuration_options (institution VARCHAR(255) NOT NULL, use_ra_locations_option TINYINT(1) NOT NULL, show_raa_contact_information_option TINYINT(1) NOT NULL, PRIMARY KEY(institution)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB');
}

/**
* @param Schema $schema
*/
public function down(Schema $schema)
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.');

$this->addSql('DROP TABLE institution_configuration_options');
}
}
2 changes: 2 additions & 0 deletions app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ doctrine:
stepup_configuration_institution: Surfnet\StepupMiddleware\ApiBundle\Doctrine\Type\ConfigurationInstitutionType
stepup_configuration_contact_information: Surfnet\StepupMiddleware\ApiBundle\Doctrine\Type\ConfigurationContactInformationType
stepup_configuration_location: Surfnet\StepupMiddleware\ApiBundle\Doctrine\Type\ConfigurationLocationType
stepup_use_ra_locations_option: Surfnet\StepupMiddleware\ApiBundle\Doctrine\Type\UseRaLocationsOptionType
stepup_show_raa_contact_information_option: Surfnet\StepupMiddleware\ApiBundle\Doctrine\Type\ShowRaaContactInformationOptionType
orm:
default_entity_manager: middleware
auto_generate_proxy_classes: "%kernel.debug%"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/**
* Copyright 2016 SURFnet B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Surfnet\StepupMiddleware\ApiBundle\Configuration\Entity;

use Doctrine\ORM\Mapping as ORM;
use JsonSerializable;
use Surfnet\Stepup\Configuration\Value\Institution;
use Surfnet\Stepup\Configuration\Value\ShowRaaContactInformationOption;
use Surfnet\Stepup\Configuration\Value\UseRaLocationsOption;

/**
* @ORM\Entity(
* repositoryClass="Surfnet\StepupMiddleware\ApiBundle\Configuration\Repository\InstitutionConfigurationOptionsRepository"
* )
*/
final class InstitutionConfigurationOptions implements JsonSerializable
{
/**
* @ORM\Id
* @ORM\Column(type="stepup_configuration_institution")
*
* @var Institution
*/
public $institution;

/**
* @ORM\Column(type="stepup_use_ra_locations_option")
*
* @var UseRaLocationsOption
*/
public $useRaLocationsOption;

/**
* @ORM\Column(type="stepup_show_raa_contact_information_option")
*
* @var ShowRaaContactInformationOption
*/
public $showRaaContactInformationOption;

public static function create(
Institution $institution,
UseRaLocationsOption $useRaLocationsOption,
ShowRaaContactInformationOption $showRaaContactInformationOption
) {
$options = new self;

$options->institution = $institution;
$options->useRaLocationsOption = $useRaLocationsOption;
$options->showRaaContactInformationOption = $showRaaContactInformationOption;

return $options;
}

public function jsonSerialize()
{
return [
'institution' => $this->institution,
'use_ra_locations' => $this->useRaLocationsOption,
'show_raa_contact_information' => $this->showRaaContactInformationOption,
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/**
* Copyright 2016 SURFnet B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Surfnet\StepupMiddleware\ApiBundle\Configuration\Projector;

use Broadway\ReadModel\Projector;
use Surfnet\Stepup\Configuration\Event\NewInstitutionConfigurationCreatedEvent;
use Surfnet\Stepup\Configuration\Event\ShowRaaContactInformationOptionChangedEvent;
use Surfnet\Stepup\Configuration\Event\UseRaLocationsOptionChangedEvent;
use Surfnet\Stepup\Configuration\Value\ShowRaaContactInformationOption;
use Surfnet\Stepup\Configuration\Value\UseRaLocationsOption;
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Entity\InstitutionConfigurationOptions;
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Repository\InstitutionConfigurationOptionsRepository;

final class InstitutionConfigurationOptionsProjector extends Projector
{
/**
* @var InstitutionConfigurationOptionsRepository
*/
private $repository;

public function __construct(InstitutionConfigurationOptionsRepository $repository)
{
$this->repository = $repository;
}

public function applyNewInstitutionConfigurationCreatedEvent(NewInstitutionConfigurationCreatedEvent $event)
{
$institutionConfigurationOptions = InstitutionConfigurationOptions::create(
$event->institution,
$event->useRaLocationsOption,
$event->showRaaContactInformationOption
);

$this->repository->save($institutionConfigurationOptions);
}

public function applyUseRaLocationOptionChangedEvent(UseRaLocationsOptionChangedEvent $event)
{
$currentOptions = $this->repository->findConfigurationOptionsFor($event->institution);

$institutionConfigurationOptions = InstitutionConfigurationOptions::create(
$event->institution,
$event->useRaLocationsOption,
new ShowRaaContactInformationOption($currentOptions->showRaaContactInformationOption)
);

$this->repository->save($institutionConfigurationOptions);
}

public function applyShowRaaContactInformationOptionChangedEvent(ShowRaaContactInformationOptionChangedEvent $event)
{
$currentOptions = $this->repository->findConfigurationOptionsFor($event->institution);

$institutionConfigurationOptions = InstitutionConfigurationOptions::create(
$event->institution,
new UseRaLocationsOption($currentOptions->useRaLocationsOption),
$event->showRaaContactInformationOption
);

$this->repository->save($institutionConfigurationOptions);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/**
* Copyright 2016 SURFnet B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Surfnet\StepupMiddleware\ApiBundle\Configuration\Repository;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\NoResultException;
use Surfnet\Stepup\Configuration\Value\Institution;
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Entity\InstitutionConfigurationOptions;

final class InstitutionConfigurationOptionsRepository extends EntityRepository
{
/**
* @param Institution $institution
* @return InstitutionConfigurationOptions
* @throws NoResultException
* @throws NonUniqueResultException
*/
public function findConfigurationOptionsFor(Institution $institution)
{
return $this->createQueryBuilder('ico')
->where('ico.institution = :institution')
->setParameter('institution', $institution->getInstitution())
->getQuery()
->getSingleResult();
}

/**
* @param InstitutionConfigurationOptions $institutionConfigurationOptions
*/
public function save(InstitutionConfigurationOptions $institutionConfigurationOptions)
{
$entityManager = $this->getEntityManager();
$entityManager->persist($institutionConfigurationOptions);
$entityManager->flush();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

/**
* Copyright 2016 SURFnet B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Surfnet\StepupMiddleware\ApiBundle\Doctrine\Type;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Type;
use Surfnet\Stepup\Configuration\Value\ShowRaaContactInformationOption;
use Surfnet\Stepup\Exception\InvalidArgumentException;

/**
* Custom Type for the ShowRaaContactInformationOption Value Object
*/
class ShowRaaContactInformationOptionType extends Type
{
const NAME = 'stepup_show_raa_contact_information_option';

public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
return $platform->getBooleanTypeDeclarationSQL($fieldDeclaration);
}

public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if (is_null($value)) {
return $value;
}

if (!$value instanceof ShowRaaContactInformationOption) {
throw new ConversionException(
sprintf(
"Encountered illegal location of type %s '%s', expected a ShowRaaContactInformationOption instance",
is_object($value) ? get_class($value) : gettype($value),
is_scalar($value) ? (string) $value : ''
)
);
}

return $value->isEnabled();
}

public function convertToPHPValue($value, AbstractPlatform $platform)
{
if (is_null($value)) {
return $value;
}

try {
$showRaaContactInformationOption = new ShowRaaContactInformationOption($value);
} catch (InvalidArgumentException $e) {
// get nice standard message, so we can throw it keeping the exception chain
$doctrineExceptionMessage = ConversionException::conversionFailedFormat(
$value,
$this->getName(),
$platform->getDateTimeFormatString()
)->getMessage();

throw new ConversionException($doctrineExceptionMessage, 0, $e);
}

return $showRaaContactInformationOption;
}

public function getName()
{
return self::NAME;
}
}
Loading

0 comments on commit 56e3926

Please sign in to comment.