Completed
Pull Request — master (#187)
by Beñat
03:22
created

SignUpAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 19
nc 1
nop 9

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/*
4
 * This file is part of the Kreta package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
declare(strict_types=1);
14
15
namespace Kreta\IdentityAccess\Infrastructure\Ui\Http\Action;
16
17
use BenGorUser\User\Application\DataTransformer\UserDataTransformer;
18
use BenGorUser\User\Application\Query\UserOfInvitationTokenHandler;
19
use BenGorUser\User\Application\Query\UserOfInvitationTokenQuery;
20
use BenGorUser\User\Domain\Model\Exception\UserDoesNotExistException;
21
use BenGorUser\User\Domain\Model\Exception\UserTokenExpiredException;
22
use BenGorUser\UserBundle\Form\Type\SignUpByInvitationType;
23
use BenGorUser\UserBundle\Security\FormLoginAuthenticator;
24
use Kreta\SharedKernel\Application\CommandBus;
25
use Symfony\Component\Form\FormFactoryInterface;
26
use Symfony\Component\HttpFoundation\Request;
27
use Symfony\Component\HttpFoundation\Response;
28
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
29
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
30
use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
31
use Symfony\Component\Templating\EngineInterface;
32
use Symfony\Component\Translation\TranslatorInterface;
33
34
class SignUpAction
35
{
36
    private $queryHandler;
37
    private $dataTransformer;
38
    private $formFactory;
39
    private $commandBus;
40
    private $flashBag;
41
    private $translator;
42
    private $guardAuthenticatorHandler;
43
    private $authenticator;
44
    private $twig;
45
46
    public function __construct(
47
        UserOfInvitationTokenHandler $queryHandler,
48
        UserDataTransformer $dataTransformer,
49
        FormFactoryInterface $formFactory,
50
        CommandBus $commandBus,
51
        FlashBagInterface $flashBag,
52
        TranslatorInterface $translator,
53
        GuardAuthenticatorHandler $guardAuthenticatorHandler,
54
        FormLoginAuthenticator $authenticator,
55
        EngineInterface $twig
56
    ) {
57
        $this->queryHandler = $queryHandler;
58
        $this->dataTransformer = $dataTransformer;
59
        $this->formFactory = $formFactory;
60
        $this->commandBus = $commandBus;
61
        $this->flashBag = $flashBag;
62
        $this->translator = $translator;
63
        $this->guardAuthenticatorHandler = $guardAuthenticatorHandler;
64
        $this->authenticator = $authenticator;
65
        $this->twig = $twig;
66
    }
67
68
    public function __invoke(Request $request)
69
    {
70
        $invitationToken = $request->query->get('invitation-token');
71
        try {
72
            // we need to know if the invitation token given exists in
73
            // database, in case that it isn't, it throws 404.
74
            $user = $this->queryHandler->__invoke(new UserOfInvitationTokenQuery($invitationToken));
75
76
            // Convert to an object implementing Symfony's UserInterface
77
            $this->dataTransformer->write($user);
78
            $user = $this->dataTransformer->read();
79
        } catch (UserDoesNotExistException $exception) {
80
            throw new NotFoundHttpException();
81
        } catch (UserTokenExpiredException $exception) {
82
            throw new NotFoundHttpException();
83
        } catch (\InvalidArgumentException $exception) {
84
            throw new NotFoundHttpException();
85
        }
86
87
        $form = $this->formFactory->create(SignUpByInvitationType::class, null, [
88
            'roles'            => ['ROLE_USER'],
89
            'invitation_token' => $invitationToken,
90
        ]);
91
        if ($request->isMethod('POST')) {
92
            $form->handleRequest($request);
93
            if ($form->isValid()) {
94
                $this->commandBus->handle($form->getData());
95
                $this->flashBag->add('notice', $this->translator->trans('sign_up.success_flash', [], 'BenGorUser'));
96
97
                return $this->guardAuthenticatorHandler->authenticateUserAndHandleSuccess(
98
                    $user,
99
                    $request,
100
                    $this->authenticator,
101
                    'main'
102
                );
103
            }
104
        }
105
106
        return new Response(
107
            $this->twig->render('@BenGorUser/sign_up/by_invitation.html.twig', [
108
                'email' => $user->getUsername(),
109
                'form'  => $form->createView(),
110
            ])
111
        );
112
    }
113
}
114