-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #145 from SURFnet/feature/institution-configuratio…
…n-options-read Institution configuration options read-side
- Loading branch information
Showing
12 changed files
with
693 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...rfnet/StepupMiddleware/ApiBundle/Configuration/Entity/InstitutionConfigurationOptions.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
...Middleware/ApiBundle/Configuration/Projector/InstitutionConfigurationOptionsProjector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...ddleware/ApiBundle/Configuration/Repository/InstitutionConfigurationOptionsRepository.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
src/Surfnet/StepupMiddleware/ApiBundle/Doctrine/Type/ShowRaaContactInformationOptionType.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.