Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.91% covered (success)
90.91%
10 / 11
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
UserAuthenticationContextBuilder
90.91% covered (success)
90.91%
10 / 11
50.00% covered (danger)
50.00%
1 / 2
5.02
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 process
90.00% covered (success)
90.00%
9 / 10
0.00% covered (danger)
0.00%
0 / 1
4.02
1<?php
2
3namespace Apie\ApieBundle\Security\ContextBuilders;
4
5use Apie\ApieBundle\Wrappers\SymfonyUserDecoratorFactory;
6use Apie\Common\Interfaces\UserDecorator;
7use Apie\Core\Context\ApieContext;
8use Apie\Core\ContextBuilders\ContextBuilderInterface;
9use Apie\Core\ContextConstants;
10use Symfony\Bundle\SecurityBundle\Security;
11
12/**
13 * Checks the authenticated user used in Symfony and add it to the context.
14 */
15class UserAuthenticationContextBuilder implements ContextBuilderInterface
16{
17    public function __construct(
18        private readonly Security $security,
19        private readonly SymfonyUserDecoratorFactory $factory
20    ) {
21    }
22
23    public function process(ApieContext $context): ApieContext
24    {
25        $user = $this->security->getUser();
26        if ($user) {
27            $context = $context->registerInstance($user);
28            if ($user instanceof UserDecorator) {
29                $context = $context->withContext(ContextConstants::AUTHENTICATED_USER, $user->getEntity());
30            } else {
31                $context = $context->withContext(ContextConstants::AUTHENTICATED_USER, $this->factory->create($user));
32            }
33        }
34        $token = $this->security->getToken();
35        if ($token) {
36            $context = $context->registerInstance($token);
37        }
38
39        return $context;
40    }
41}