GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

CredentialRegistrationChallenge::process()   B
last analyzed

Complexity

Conditions 7
Paths 12

Size

Total Lines 80
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 54
nc 12
nop 2
dl 0
loc 80
rs 8.0703
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace LM\AuthAbstractor\Challenge;
6
7
use Psr\Http\Message\ServerRequestInterface;
8
use LM\AuthAbstractor\Configuration\IApplicationConfiguration;
9
use LM\AuthAbstractor\Implementation\Member;
10
use LM\AuthAbstractor\Model\AuthenticationProcess;
11
use LM\AuthAbstractor\Model\IAuthenticationProcess;
12
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
13
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
14
use LM\AuthAbstractor\Form\Constraint\ValidNewPassword;
15
use LM\AuthAbstractor\Implementation\ChallengeResponse;
16
use LM\AuthAbstractor\Model\IChallengeResponse;
17
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
18
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
19
use Symfony\Component\Form\FormError;
20
use Symfony\Component\Form\FormFactoryInterface;
21
use Symfony\Component\HttpFoundation\Response;
22
use Twig_Environment;
23
use Symfony\Component\HttpFoundation\Request;
24
use LM\AuthAbstractor\Enum\Persistence\Operation;
25
use LM\Common\Model\ArrayObject;
26
use LM\AuthAbstractor\Model\PersistOperation;
27
28
/**
29
 * A challenge for registering credentials (username + password) from the user.
30
 */
31
class CredentialRegistrationChallenge implements IChallenge
32
{
33
    /** @var IApplicationConfiguration */
34
    private $appConfig;
35
36
    /** @var ValidNewPassword */
37
    private $constraint;
38
39
    /** @var FormFactoryInterface */
40
    private $formFactory;
41
42
    /** @var HttpFoundationFactory */
43
    private $httpFoundationFactory;
44
45
    /** @var Twig_Environment */
46
    private $twig;
47
48
    /**
49
     * @internal
50
     */
51
    public function __construct(
52
        IApplicationConfiguration $appConfig,
53
        ValidNewPassword $constraint,
54
        FormFactoryInterface $formFactory,
55
        HttpFoundationFactory $httpFoundationFactory,
56
        Twig_Environment $twig
57
    ) {
58
        $this->appConfig = $appConfig;
59
        $this->constraint = $constraint;
60
        $this->formFactory = $formFactory;
61
        $this->httpFoundationFactory = $httpFoundationFactory;
62
        $this->twig = $twig;
63
    }
64
65
    /**
66
     * @internal
67
     */
68
    public function process(
69
        IAuthenticationProcess $process,
70
        ?ServerRequestInterface $httpRequest
71
    ): IChallengeResponse {
72
        $form = $this
73
            ->formFactory
74
            ->createBuilder()
75
            ->add('username')
76
            ->add('password', RepeatedType::class, [
77
                'constraints' => [
78
                    $this->constraint,
79
                ],
80
                'type' => PasswordType::class,
81
                'invalid_message' => 'The password fields must match.',
82
                'required' => true,
83
                'first_options'  => ['label' => 'Password'],
84
                'second_options' => ['label' => 'Repeat Password'],
85
            ])
86
            ->add('submit', SubmitType::class)
87
            ->getForm()
88
        ;
89
90
        if (null !== $httpRequest) {
91
            $form->handleRequest($this->httpFoundationFactory->createRequest($httpRequest));
92
        }
93
94
        if ($form->isSubmitted() && $this->appConfig->isExistingMember($form['username']->getData())) {
95
            $form
96
                ->get('username')
97
                ->addError(new FormError('The username is already taken.'))
98
            ;
99
        }
100
101
        if ($form->isSubmitted() && $form->isValid()) {
102
            $persistOperations = $process
0 ignored issues
show
Deprecated Code introduced by
The function LM\AuthAbstractor\Model\...nProcess::getTypedMap() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

102
            $persistOperations = /** @scrutinizer ignore-deprecated */ $process
Loading history...
103
                ->getTypedMap()
104
                ->has('persist_operations') ? $process
0 ignored issues
show
Deprecated Code introduced by
The function LM\AuthAbstractor\Model\...nProcess::getTypedMap() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

104
                ->has('persist_operations') ? /** @scrutinizer ignore-deprecated */ $process
Loading history...
105
                    ->getTypedMap()
106
                    ->get('persist_operations', ArrayObject::class)
107
            : new ArrayObject([], PersistOperation::class);
0 ignored issues
show
Deprecated Code introduced by
The class LM\Common\Model\ArrayObject has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

107
            : /** @scrutinizer ignore-deprecated */ new ArrayObject([], PersistOperation::class);
Loading history...
108
            // var_dump(Request::createFromGlobals());
109
            $member = new Member(
110
                password_hash($form->get('password')->getData(), PASSWORD_DEFAULT),
111
                $form->get('username')->getData()
112
            );
113
            $newDm = $process
0 ignored issues
show
Deprecated Code introduced by
The function LM\AuthAbstractor\Model\...nProcess::getTypedMap() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

113
            $newDm = /** @scrutinizer ignore-deprecated */ $process
Loading history...
114
                ->getTypedMap()
115
                ->add(
116
                    'member',
117
                    $member,
118
                    Member::class
119
                )
120
                ->set(
121
                    'persist_operations',
122
                    $persistOperations->add(
123
                        new PersistOperation($member, new Operation(Operation::CREATE)),
124
                        PersistOperation::class
125
                    ),
126
                    ArrayObject::class
127
                )
128
            ;
129
130
            return new ChallengeResponse(
131
                new AuthenticationProcess($newDm),
132
                null,
133
                false,
134
                true
135
            )
136
            ;
137
        }
138
139
        $response = new Response($this->twig->render('credential_registration.html.twig', [
140
            'form' => $form->createView(),
141
        ]));
142
143
        return new ChallengeResponse(
144
            $process,
145
            $response,
146
            false,
147
            false
148
        )
149
        ;
150
    }
151
}
152