Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
6 / 6 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| UserContextMiddleware | |
100.00% |
6 / 6 |
|
100.00% |
2 / 2 |
3 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| process | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | // ╔════════════════════════════════════════════════════════════╗ |
| 4 | // ║ MIT Licence (#Expat) - https://opensource.org/licenses/MIT ║ |
| 5 | // ║ Copyright 2026 Frederic Poeydomenge <dyno@phexium.com> ║ |
| 6 | // ╚════════════════════════════════════════════════════════════╝ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | namespace AppDemo\Shared\Application\Middleware; |
| 11 | |
| 12 | use AppDemo\Shared\Domain\Interface\SessionServiceInterface; |
| 13 | use AppDemo\Shared\Domain\Interface\UserContextProviderInterface; |
| 14 | use Override; |
| 15 | use Psr\Http\Message\ResponseInterface; |
| 16 | use Psr\Http\Message\ServerRequestInterface; |
| 17 | use Psr\Http\Server\MiddlewareInterface; |
| 18 | use Psr\Http\Server\RequestHandlerInterface; |
| 19 | |
| 20 | final readonly class UserContextMiddleware implements MiddlewareInterface |
| 21 | { |
| 22 | public function __construct( |
| 23 | private SessionServiceInterface $sessionService, |
| 24 | private UserContextProviderInterface $userContextProvider |
| 25 | ) {} |
| 26 | |
| 27 | #[Override] |
| 28 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
| 29 | { |
| 30 | // Force session start to ensure data is loaded |
| 31 | if (!$this->sessionService->getSession()->isStarted()) { |
| 32 | $this->sessionService->getSession()->start(); |
| 33 | } |
| 34 | |
| 35 | // Delegate user loading to provider |
| 36 | $userContext = $this->userContextProvider->getCurrentUserContext(); |
| 37 | |
| 38 | // Inject UserContext into request |
| 39 | $request = $request->withAttribute('user_context', $userContext); |
| 40 | |
| 41 | return $handler->handle($request); |
| 42 | } |
| 43 | } |