Skip to content

Commit

Permalink
Improve service locator retrieval logic
Browse files Browse the repository at this point in the history
The get method on the ContainerController left some to the imagination
when it comes to logging. This commit adds logging and makes transparant
which service was being loaded. This is usefull in case when the service
could not be loaded.

The other part is that the SF controller when the ResponseContext is
loaded. The unavailability of the StateHandlers response context id is
now checked.
  • Loading branch information
MKodde committed Apr 25, 2024
1 parent 6c28707 commit 660bfa5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ public function setServiceContainer(ContainerInterface $container): void

public function get(string $serviceName): mixed
{
$logger = $this->container->get('logger');
$logger->notice(
sprintf(
'Reading the "%s" service from the container (temporary ContainerController solution)',
$serviceName
)
);
try {
$service = $this->container->get($serviceName);
} catch (Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Routing\Attribute\Route;
use function is_null;
use const FILTER_DEFAULT;
use const FILTER_FORCE_ARRAY;

Expand Down Expand Up @@ -646,11 +647,20 @@ private function getStepupService()
*/
private function getResponseContext($authenticationMode)
{
return match ($authenticationMode) {
self::MODE_SFO => $this->get($this->get('gateway.proxy.sfo.state_handler')->getResponseContextServiceId()),
self::MODE_SSO => $this->get($this->get('gateway.proxy.sso.state_handler')->getResponseContextServiceId()),
// Select the state handler that matches the current authentication mode
$stateHandlerServiceId = match ($authenticationMode) {
self::MODE_SFO => 'gateway.proxy.sfo.state_handler',
self::MODE_SSO => 'gateway.proxy.sso.state_handler',
default => throw new InvalidArgumentException('Invalid authentication mode requested'),
};

// We then load the correct state handler service. And retrieve the ResponseContext service id that was set on it
$responseContextServiceId = $this->get($stateHandlerServiceId)->getResponseContextServiceId();
if (is_null($responseContextServiceId)) {
throw new RuntimeException('The RequestContext service id is not set on the state handler %s');
}
// Finally return the ResponseContext
return $this->get($responseContextServiceId);
}

/**
Expand Down

0 comments on commit 660bfa5

Please sign in to comment.