Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add frontend config #32

Merged
merged 3 commits into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions devops/ci/config/phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
parameters:
ignoreErrors:
-
message: "#^Method Cbase\\\\App\\\\Application\\\\Controller\\\\IndexController\\:\\:getAuth\\(\\) return type has no value type specified in iterable type array\\.$#"
message: "#^Property Cbase\\\\App\\\\Application\\\\Listener\\\\AddResourcesToFrontendConfig\\:\\:\\$config is never read, only written\\.$#"
count: 1
path: ../../../src/App/Application/Controller/IndexController.php
path: ../../../src/App/Application/Listener/AddResourcesToFrontendConfig.php

-
message: "#^Class Cbase\\\\ArtefactGuide\\\\Application\\\\Action\\\\ListArtefacts\\\\ListArtefactsQuery does not have a constructor and must be instantiated without any parameters\\.$#"
Expand Down Expand Up @@ -135,6 +135,11 @@ parameters:
count: 1
path: ../../../src/ArtefactGuide/Application/Console/CbagRestoreCommand.php

-
message: "#^Property Cbase\\\\ArtefactGuide\\\\Application\\\\Listener\\\\AddContentToFrontendConfig\\:\\:\\$config is never read, only written\\.$#"
count: 1
path: ../../../src/ArtefactGuide/Application/Listener/AddContentToFrontendConfig.php

-
message: "#^Method Cbase\\\\ArtefactGuide\\\\Domain\\\\Artefact\\:\\:jsonSerialize\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
Expand All @@ -161,7 +166,7 @@ parameters:
path: ../../../src/ArtefactGuide/Infrastructure/Persistence/Doctrine/Repository/DoctrineArtefactRepository.php

-
message: "#^Method Cbase\\\\ArtefactGuide\\\\Infrastructure\\\\Persistence\\\\Doctrine\\\\Repository\\\\DoctrineImageRepository\\:\\:all\\(\\) should return array\\<Cbase\\\\ArtefactGuide\\\\Domain\\\\Image\\> but returns array\\<int, object\\>\\.$#"
message: "#^Method Cbase\\\\ArtefactGuide\\\\Infrastructure\\\\Persistence\\\\Doctrine\\\\Repository\\\\DoctrineImageRepository\\:\\:all\\(\\) should return array\\<Cbase\\\\ArtefactGuide\\\\Domain\\\\Image\\> but returns mixed\\.$#"
count: 1
path: ../../../src/ArtefactGuide/Infrastructure/Persistence/Doctrine/Repository/DoctrineImageRepository.php

Expand All @@ -170,6 +175,11 @@ parameters:
count: 1
path: ../../../src/ArtefactGuide/Infrastructure/Persistence/Doctrine/Repository/DoctrineImageRepository.php

-
message: "#^Property Cbase\\\\Authentication\\\\Application\\\\Listener\\\\AddAuthToFrontendConfig\\:\\:\\$config is never read, only written\\.$#"
count: 1
path: ../../../src/Authentication/Application/Listener/AddAuthToFrontendConfig.php

-
message: "#^Method Cbase\\\\Authentication\\\\Domain\\\\User\\:\\:toArray\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
Expand Down Expand Up @@ -345,6 +355,16 @@ parameters:
count: 2
path: ../../../tests/ArtefactGuide/Application/Action/UpdateArtefact/UpdateArtefactHandlerTest.php

-
message: "#^Binary operation \"\\.\" between literal\\-string&non\\-falsy\\-string and array\\<string\\>\\|string results in an error\\.$#"
count: 1
path: ../../../tests/ArtefactGuide/Application/Action/UploadImage/UploadImageHandlerTest.php

-
message: "#^Parameter \\#1 \\$imageIds of method Cbase\\\\ArtefactGuide\\\\Domain\\\\ImageRepository\\:\\:findByImageIds\\(\\) expects array\\<string\\>, array\\<int, array\\<string\\>\\|string\\> given\\.$#"
count: 1
path: ../../../tests/ArtefactGuide/Application/Action/UploadImage/UploadImageHandlerTest.php

-
message: "#^Parameter \\#1 \\$item of method Cbase\\\\Shared\\\\Domain\\\\Collection\\<Cbase\\\\ArtefactGuide\\\\Domain\\\\Artefact\\>\\:\\:append\\(\\) expects Cbase\\\\ArtefactGuide\\\\Domain\\\\Artefact, Cbase\\\\ArtefactGuide\\\\Domain\\\\Image given\\.$#"
count: 1
Expand Down
53 changes: 6 additions & 47 deletions src/App/Application/Controller/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@

namespace Cbase\App\Application\Controller;

use Cbase\ArtefactGuide\Domain\Licence;
use Cbase\Authentication\Application\Controller\Authenticate;
use Cbase\Authentication\Domain\User;
use Cbase\Shared\Domain\FrontendConfig;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\RouterInterface;

#[AsController]
class IndexController extends AbstractController
{
public function __construct(private FrontendConfig $config)
{
}

public const APP_INDEX = 'app_index';

#[Route(
Expand All @@ -29,50 +30,8 @@ class IndexController extends AbstractController
)]
public function __invoke(): Response
{
$config = [
'auth' => $this->getAuth(),
'resources' => $this->getApiResources(),
'content' => ['licences' => Licence::VALID_LICENCES],
];
return $this->render('app/index.html.twig', [
'config' => json_encode($config),
'config' => json_encode($this->config),
]);
}

private function getAuth(): array
{
if (!$this->getUser()) {
return [
'authenticated' => false,
];
}
return [
'authenticated' => true,
'username' => $this->getUser()->getUserIdentifier(),
];
}

/**
* @return array<string, \Symfony\Component\Routing\Route>
*/
private function getApiResources(): array
{
/** @var RouterInterface $router */
$router = $this->container->get('router');
$routes = $router->getRouteCollection()->all();

$excludedRoutes = [Authenticate::API_AUTH_CALLBACK];

$routes = array_filter($routes, function ($routeName) use ($excludedRoutes) {
return str_starts_with($routeName, 'api') && !in_array($routeName, $excludedRoutes, true);
}, ARRAY_FILTER_USE_KEY);

array_walk($routes, function (&$route, $routeName) {
$route = [
'path' => $route->getPath(),
'method' => $route->getMethods()[0],
];
});
return $routes;
}
}
39 changes: 39 additions & 0 deletions src/App/Application/Listener/AddResourcesToFrontendConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Cbase\App\Application\Listener;

use Cbase\Authentication\Application\Controller\Authenticate;
use Cbase\Shared\Domain\FrontendConfig;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\Routing\RouterInterface;

#[AsEventListener]
final class AddResourcesToFrontendConfig
{
public function __construct(private FrontendConfig $config, private RouterInterface $router)
{
}

public function __invoke(ControllerEvent $event): void
{
$routes = $this->router->getRouteCollection()->all();

$excludedRoutes = [Authenticate::API_AUTH_CALLBACK];

$routes = array_filter($routes, function ($routeName) use ($excludedRoutes) {
return str_starts_with($routeName, 'api') && !in_array($routeName, $excludedRoutes, true);
}, ARRAY_FILTER_USE_KEY);

$resources = [];
foreach ($routes as $routeName => $route) {
$resources[$routeName] = [
'path' => $route->getPath(),
'method' => $route->getMethods()[0],
];
}
$this->config['resources'] = $resources;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Cbase\ArtefactGuide\Application\Listener;

use Cbase\ArtefactGuide\Domain\Licence;
use Cbase\Shared\Domain\FrontendConfig;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpKernel\Event\ControllerEvent;

#[AsEventListener]
final class AddContentToFrontendConfig
{
public function __construct(private FrontendConfig $config)
{
}

public function __invoke(ControllerEvent $event): void
{
$this->config['content'] = ['licences' => Licence::VALID_LICENCES];
}
}
4 changes: 2 additions & 2 deletions src/ArtefactGuide/Domain/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function addArtefact(Artefact $artefact): void
}

/**
* @return array<string, string>
* @return array<string, string|array<string>>
*/
public function jsonSerialize(): array
{
Expand All @@ -87,7 +87,7 @@ public function jsonSerialize(): array
'author' => $this->author,
'createdAt' => $this->createdAt->format('Y-m-d'),
'licence' => $this->licence->value(),
'artefacts' => CollectionUtils::map($this->artefacts, fn(Artefact $artefact) => $artefact->getSlug()->value()),
'artefacts' => CollectionUtils::map($this->artefacts, fn (Artefact $artefact) => $artefact->getSlug()->value()),
];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Cbase\Authentication\Application\Listener;

use Cbase\Shared\Domain\FrontendConfig;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpKernel\Event\ControllerEvent;

#[AsEventListener]
final class AddAuthToFrontendConfig
{
public function __construct(private FrontendConfig $config, private Security $security)
{
}

public function __invoke(ControllerEvent $event): void
{
$auth = [
'authenticated' => false,
];

if ($this->security->getUser()) {
$auth = [
'authenticated' => true,
'username' => $this->security->getUser()->getUserIdentifier(),
];
}

$this->config['auth'] = $auth;
}
}
49 changes: 49 additions & 0 deletions src/Shared/Domain/FrontendConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Cbase\Shared\Domain;

use Symfony\Contracts\Service\ResetInterface;

/**
* @implements \ArrayAccess<string, array<string, mixed>>
*/
final class FrontendConfig implements \ArrayAccess, ResetInterface, \JsonSerializable
{
/**
* @var array<string, array<string, mixed>>
*/
protected array $config = [];

public function offsetExists(mixed $offset): bool
{
return isset($this->config[$offset]);
}

public function offsetGet(mixed $offset): mixed
{
return $this->config[$offset];
}

public function offsetSet(mixed $offset, mixed $value): void
{
$this->config[$offset] = $value;
}

public function offsetUnset(mixed $offset): void
{
unset($this->config[$offset]);
}


public function reset(): void
{
$this->config = [];
}

public function jsonSerialize(): mixed
{
return $this->config;
}
}
48 changes: 48 additions & 0 deletions tests/Shared/Domain/FrontendConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Tests\Shared\Domain;

use Cbase\Shared\Domain\FrontendConfig;
use Tests\Shared\Infrastructure\PhpUnit\UnitTestCase;

final class FrontendConfigTest extends UnitTestCase
{
public function test_values_can_be_added(): void
{
$config = new FrontendConfig();
$config['answer-to-everything'] = ['guess' => 42];

self::assertArrayHasKey('answer-to-everything', $config);
self::assertEquals(['guess' => 42], $config['answer-to-everything']);
}

public function test_is_config_serializable(): void
{
$config = new FrontendConfig();
$config['answer-to-everything'] = ['guess' => 42];

self::assertEquals('{"answer-to-everything":{"guess":42}}', json_encode($config));
}

public function test_is_config_resettable(): void
{
$config = new FrontendConfig();
$config['answer-to-everything'] = ['guess' => 42];

$config->reset();

self::assertEmpty($config->jsonSerialize());
}

public function test_is_config_can_be_unset(): void
{
$config = new FrontendConfig();
$config['answer-to-everything'] = ['guess' => 42];

unset($config['answer-to-everything']);

self::assertEmpty($config->jsonSerialize());
}
}