Skip to content

Commit

Permalink
Move TypeOfServiceRepositoryFromConfig to domain
Browse files Browse the repository at this point in the history
It having a coupling with filesystem does not have to mean it is not
part of the domain. That was a fault in my reasoning. Also to more
easily reuse the repository. I decoupled the repo file path from DI and set
it in the Contstants.

That way we can also use it in the Coin value object factory method
  • Loading branch information
MKodde committed Jul 1, 2024
1 parent 3064cb5 commit 9d44f0f
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class Constants

final public const OIDC_SECRET_LENGTH = 20;

final public const TYPE_OF_SERVICE_LOCATION = __DIR__ . '/../../../../../assets/type_of_service.json';

public static function getValidNameIdFormats(): array
{
return [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types = 1);

/**
* Copyright 2024 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\ServiceProviderDashboard\Domain\Exception;

use Exception;

class TypeOfServiceException extends Exception
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,35 @@
* limitations under the License.
*/

namespace Surfnet\ServiceProviderDashboard\Infrastructure\DashboardBundle\Repository;
namespace Surfnet\ServiceProviderDashboard\Domain\Repository;

use Surfnet\ServiceProviderDashboard\Application\Exception\RuntimeException;
use Surfnet\ServiceProviderDashboard\Domain\Repository\TypeOfServiceRepository;
use Surfnet\ServiceProviderDashboard\Domain\Entity\Constants;
use Surfnet\ServiceProviderDashboard\Domain\Exception\TypeOfServiceException;
use Surfnet\ServiceProviderDashboard\Domain\ValueObject\TypeOfService;
use Surfnet\ServiceProviderDashboard\Domain\ValueObject\TypeOfServiceCollection;
use function file_exists;
use function is_array;

class TypeOfServiceRepositoryFromConfig implements TypeOfServiceRepository
{
private TypeOfServiceCollection $collection;

public function __construct(
private readonly string $typeOfServiceLocation,
) {
}

private function load(): void
{
if (!file_exists($this->typeOfServiceLocation)) {
throw new RuntimeException(
$typeOfServiceLocation = Constants::TYPE_OF_SERVICE_LOCATION;
if (!file_exists($typeOfServiceLocation)) {
throw new TypeOfServiceException(
sprintf(
'Please review the file location of the type of services json blob. %s',
$this->typeOfServiceLocation
$typeOfServiceLocation
)
);
}
$fileContents = file_get_contents($this->typeOfServiceLocation);
$fileContents = file_get_contents($typeOfServiceLocation);
if (!$fileContents) {
throw new RuntimeException('Unable to load the type of service json file.');
throw new TypeOfServiceException('Unable to load the type of service json file.');
}
$data = json_decode($fileContents);
if (!is_array($data)) {
throw new RuntimeException('The json can not be parsed into an array of service types');
throw new TypeOfServiceException('The json can not be parsed into an array of service types');
}
$this->collection = new TypeOfServiceCollection();
foreach ($data as $entry) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,10 @@ services:
Surfnet\ServiceProviderDashboard\Infrastructure\DashboardBundle\Repository\AttributeRepository:
arguments: [ '%kernel.project_dir%/assets/attributes.json' ]

Surfnet\ServiceProviderDashboard\Domain\Repository\TypeOfServiceRepository:
alias: Surfnet\ServiceProviderDashboard\Infrastructure\DashboardBundle\Repository\TypeOfServiceRepositoryFromConfig

Surfnet\ServiceProviderDashboard\Infrastructure\DashboardBundle\Repository\TypeOfServiceRepositoryFromConfig:
Surfnet\ServiceProviderDashboard\Domain\Repository\TypeOfServiceRepositoryFromConfig:

arguments: [ '%kernel.project_dir%/assets/type_of_service.json' ]
Surfnet\ServiceProviderDashboard\Domain\Repository\TypeOfServiceRepository:
alias: Surfnet\ServiceProviderDashboard\Domain\Repository\TypeOfServiceRepositoryFromConfig

Surfnet\ServiceProviderDashboard\Infrastructure\DashboardBundle\Repository\PrivacyQuestionsRepository:
public: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
* limitations under the License.
*/

namespace Surfnet\ServiceProviderDashboard\Tests\Unit\Infrastructure\DashboardBundle\Repository;
namespace Surfnet\ServiceProviderDashboard\Tests\Unit\Domain\DashboardBundle\Repository;

use PHPUnit\Framework\TestCase;
use Surfnet\ServiceProviderDashboard\Application\Exception\RuntimeException;
use Surfnet\ServiceProviderDashboard\Domain\Exception\TypeOfServiceException;
use Surfnet\ServiceProviderDashboard\Domain\Repository\TypeOfServiceRepositoryFromConfig;
use Surfnet\ServiceProviderDashboard\Domain\ValueObject\TypeOfService;
use Surfnet\ServiceProviderDashboard\Infrastructure\DashboardBundle\Repository\TypeOfServiceRepositoryFromConfig;

class TypeOfServiceRepositoryFromConfigTest extends TestCase
{
Expand All @@ -34,14 +35,14 @@ public function test_it_loads_types_of_service()
}
public function test_it_rejects_non_existing_file()
{
self::expectException(RuntimeException::class);
self::expectException(TypeOfServiceException::class);
self::expectExceptionMessageMatches('/Please review the file location of the type of services json blob/');
$typesRepo = new TypeOfServiceRepositoryFromConfig('/Fixtures/shes_not_there.json');
$typesRepo->getTypesOfServiceChoices();
}
public function test_it_rejects_faulty_json()
{
self::expectException(RuntimeException::class);
self::expectException(TypeOfServiceException::class);
self::expectExceptionMessage('The json can not be parsed into an array of service types');
$typesRepo = new TypeOfServiceRepositoryFromConfig(__DIR__ . '/Fixtures/type_of_service_corrupt_json.json');
$typesRepo->getTypesOfServiceChoices();
Expand Down

0 comments on commit 9d44f0f

Please sign in to comment.