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.

PasswordChallenge::process()   B
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 53
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 32
nc 8
nop 2
dl 0
loc 53
rs 8.7857
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\Model\IAuthenticationProcess;
10
use LM\Common\Model\StringObject;
11
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
12
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
13
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
14
use LM\AuthAbstractor\Model\IChallengeResponse;
15
use LM\AuthAbstractor\Implementation\ChallengeResponse;
16
use Symfony\Component\Form\FormError;
17
use Symfony\Component\Form\FormFactoryInterface;
18
use Symfony\Component\HttpFoundation\Response;
19
use Twig_Environment;
20
21
/**
22
 * This challenge asks the user for their password. The username needs to be
23
 * known first. (The username can be specified when creating the authentication
24
 * process or by placing an ExistingUsernameChallenge before.)
25
 */
26
class PasswordChallenge implements IChallenge
27
{
28
    /** @var IApplicationConfiguration */
29
    private $appConfig;
30
31
    /** @var FormFactoryInterface */
32
    private $formFactory;
33
34
    /** @var HttpFoundationFactory */
35
    private $httpFoundationFactory;
36
37
    /** @var Twig_Environment */
38
    private $twig;
39
40
    /**
41
     * @internal
42
     */
43
    public function __construct(
44
        IApplicationConfiguration $appConfig,
45
        FormFactoryInterface $formFactory,
46
        HttpFoundationFactory $httpFoundationFactory,
47
        Twig_Environment $twig
48
    ) {
49
        $this->appConfig = $appConfig;
50
        $this->formFactory = $formFactory;
51
        $this->httpFoundationFactory = $httpFoundationFactory;
52
        $this->twig = $twig;
53
    }
54
55
    /**
56
     * @internal
57
     */
58
    public function process(
59
        IAuthenticationProcess $process,
60
        ?ServerRequestInterface $httpRequest
61
    ): IChallengeResponse {
62
        $username = $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

62
        $username = /** @scrutinizer ignore-deprecated */ $process
Loading history...
63
            ->getTypedMap()
64
            ->get('username', StringObject::class)
65
            ->toString()
66
        ;
67
68
        $member = $this
69
            ->appConfig
70
            ->getMember($username)
71
        ;
72
73
        $form = $this
74
            ->formFactory
75
            ->createBuilder()
76
            ->add('password', PasswordType::class)
77
            ->add('submit', SubmitType::class)
78
            ->getForm()
79
        ;
80
81
        if (null !== $httpRequest) {
82
            $form->handleRequest($this->httpFoundationFactory->createRequest($httpRequest));
83
        }
84
85
        if ($form->isSubmitted() && !password_verify($form['password']->getData(), $member->getHashedPassword())) {
86
            $form
87
                ->get('password')
88
                ->addError(new FormError('The password is invalid.'))
89
            ;
90
        }
91
92
        if ($form->isSubmitted() && $form->isValid()) {
93
            return new ChallengeResponse(
94
                $process,
95
                null,
96
                false,
97
                true
98
            )
99
            ;
100
        }
101
102
        $response = new Response($this->twig->render("password_authentication.html.twig", [
103
            "form" => $form->createView(),
104
        ]));
105
106
        return new ChallengeResponse(
107
            $process,
108
            $response,
109
            $form->isSubmitted(),
110
            false
111
        )
112
        ;
113
    }
114
}
115