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.

CredentialChallenge::process()   B
last analyzed

Complexity

Conditions 7
Paths 16

Size

Total Lines 53
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 35
nc 16
nop 2
dl 0
loc 53
rs 8.4266
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 LM\AuthAbstractor\Configuration\IApplicationConfiguration;
8
use LM\AuthAbstractor\Model\AuthenticationProcess;
9
use LM\AuthAbstractor\Model\IAuthenticationProcess;
10
use LM\Common\Model\StringObject;
11
use Psr\Http\Message\ServerRequestInterface;
12
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
13
use LM\AuthAbstractor\Model\IChallengeResponse;
14
use LM\AuthAbstractor\Implementation\ChallengeResponse;
15
use Symfony\Component\HttpFoundation\Response;
16
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
17
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
18
use Symfony\Component\Form\FormError;
19
use Symfony\Component\Form\FormFactoryInterface;
20
use Twig_Environment;
21
22
/**
23
 * A challenge providing credential-based authentication (username + password).
24
 */
25
class CredentialChallenge implements IChallenge
26
{
27
    /** @var IApplicationConfiguration */
28
    private $appConfig;
29
30
    /** @var FormFactoryInterface */
31
    private $formFactory;
32
33
    /** @var HttpFoundationFactory */
34
    private $httpFoundationFactory;
35
36
    /** @var Twig_Environment */
37
    private $twig;
38
39
    /**
40
     * @internal
41
     */
42
    public function __construct(
43
        IApplicationConfiguration $appConfig,
44
        FormFactoryInterface $formFactory,
45
        HttpFoundationFactory $httpFoundationFactory,
46
        Twig_Environment $twig
47
    ) {
48
        $this->appConfig = $appConfig;
49
        $this->formFactory = $formFactory;
50
        $this->httpFoundationFactory = $httpFoundationFactory;
51
        $this->twig = $twig;
52
    }
53
54
    /**
55
     * @internal
56
     */
57
    public function process(
58
        IAuthenticationProcess $process,
59
        ?ServerRequestInterface $httpRequest
60
    ): IChallengeResponse {
61
        $form = $this
62
            ->formFactory
63
            ->createBuilder()
64
            ->add('username')
65
            ->add('password', PasswordType::class)
66
            ->add('submit', SubmitType::class)
67
            ->getForm()
68
        ;
69
70
        if (null !== $httpRequest) {
71
            $form->handleRequest($this->httpFoundationFactory->createRequest($httpRequest));
72
        }
73
        if ($form->isSubmitted()) {
74
            if (!$this->appConfig->isExistingMember($form['username']->getData())) {
75
                $form->addError(new FormError('Invalid credentials'));
76
            } else {
77
                $member = $this->appConfig->getMember($form['username']->getData());
78
                if (!password_verify($form['password']->getData(), $member->getHashedPassword())) {
79
                    $form->addError(new FormError('Invalid credentials'));
80
                }
81
            }
82
        }
83
        if ($form->isSubmitted() && $form->isValid()) {
84
            $authProcess = new AuthenticationProcess($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

84
            $authProcess = new AuthenticationProcess(/** @scrutinizer ignore-deprecated */ $process
Loading history...
85
                ->getTypedMap()
86
                ->add(
87
                    'username',
88
                    new StringObject($form['username']->getData()),
89
                    StringObject::class
90
                ))
91
            ;
92
93
            return new ChallengeResponse(
94
                $authProcess,
95
                null,
96
                false,
97
                true
98
            )
99
            ;
100
        }
101
        $httpResponse = new Response($this->twig->render("credential_authentication.html.twig", [
102
            "form" => $form->createView(),
103
        ]));
104
105
        return new ChallengeResponse(
106
            $process,
107
            $httpResponse,
108
            $form->isSubmitted(),
109
            false
110
        )
111
        ;
112
    }
113
}
114