Test Failed
Push — develop ( 28e0cd...307ddb )
by Daniel
05:05
created

UserHandler::success()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 22
rs 9.7666
c 0
b 0
f 0
cc 4
nc 3
nop 3
1
<?php
2
3
namespace Silverback\ApiComponentBundle\Form\Handler;
4
5
use Silverback\ApiComponentBundle\Entity\User\User;
6
use App\Security\PasswordManager;
0 ignored issues
show
Bug introduced by
The type App\Security\PasswordManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use App\Security\TokenAuthenticator;
0 ignored issues
show
Bug introduced by
The type App\Security\TokenAuthenticator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Lexik\Bundle\JWTAuthenticationBundle\Security\Http\Authentication\AuthenticationSuccessHandler;
9
use Silverback\ApiComponentBundle\Entity\Component\Form\Form;
10
use Silverback\ApiComponentBundle\Exception\UnsupportedFormEntityException;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\Response;
13
use Symfony\Component\Security\Core\Exception\AuthenticationException;
14
15
class UserHandler implements FormHandlerInterface
16
{
17
    private $authenticationSuccessHandler;
18
    private $passwordManager;
19
    private $tokenAuthenticator;
20
21
    public function __construct(
22
        AuthenticationSuccessHandler $authenticationSuccessHandler,
23
        PasswordManager $passwordManager,
24
        TokenAuthenticator $tokenAuthenticator
25
    ) {
26
        $this->authenticationSuccessHandler = $authenticationSuccessHandler;
27
        $this->passwordManager = $passwordManager;
28
        $this->tokenAuthenticator = $tokenAuthenticator;
29
    }
30
31
    /**
32
     * @param Form $form
33
     * @param User $user
34
     * @param Request $request
35
     * @return null|Response
36
     * @throws UnsupportedFormEntityException
37
     */
38
    public function success(Form $form, $user, Request $request): ?Response
39
    {
40
        $credentials = $this->tokenAuthenticator->getCredentials($request);
41
        $appServerTokenUser = $this->tokenAuthenticator->getUser($credentials);
42
        if (!$appServerTokenUser) {
43
            $exception = new AuthenticationException('This form can only be submitted from the app server.');
44
            return $this->tokenAuthenticator->onAuthenticationFailure($request, $exception);
45
        }
46
47
        if (!$user instanceof User) {
0 ignored issues
show
introduced by
$user is always a sub-type of Silverback\ApiComponentBundle\Entity\User\User.
Loading history...
48
            throw new UnsupportedFormEntityException(
49
                sprintf(
50
                    '`%s` only supports forms that submit the `%s` entity. Received `%s`',
51
                    self::class,
52
                    User::class,
53
                    \is_object($user) ? \get_class($user) : $user
54
                )
55
            );
56
        }
57
        $this->passwordManager->persistPlainPassword($user);
58
        $user->eraseCredentials();
59
        return $this->authenticationSuccessHandler->handleAuthenticationSuccess($user);
60
    }
61
}
62