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.

EmailChallenge::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 4
dl 0
loc 10
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LM\AuthAbstractor\Challenge;
6
7
use LM\AuthAbstractor\Model\IAuthenticationProcess;
8
use LM\AuthAbstractor\Implementation\ChallengeResponse;
9
use LM\AuthAbstractor\Model\IChallengeResponse;
10
use LM\AuthAbstractor\Model\AuthenticationProcess;
11
use Psr\Http\Message\ServerRequestInterface;
12
use LM\Common\Enum\Scalar;
13
use Twig_Environment;
14
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
15
use Symfony\Component\Form\FormFactoryInterface;
16
use LM\AuthAbstractor\Model\IMailer;
17
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
18
use LM\AuthAbstractor\Configuration\IApplicationConfiguration;
19
use Symfony\Component\HttpFoundation\Response;
20
use Symfony\Component\Form\FormError;
21
22
/**
23
 * A challenge for asking the user to enter a code sent by email.
24
 */
25
class EmailChallenge implements IChallenge
26
{
27
    /** @var int */
28
    const CODE_MIN = 0;
29
30
    /** @var int */
31
    const CODE_MAX = 999999;
32
33
    /** @var IApplicationConfiguration */
34
    private $appConfig;
35
36
    /** @var FormFactoryInterface */
37
    private $formFactory;
38
39
    /** @var HttpFoundationFactory */
40
    private $httpFoundationFactory;
41
42
    /** @var Twig_Environment */
43
    private $twig;
44
45
    /**
46
     * @internal
47
     */
48
    public function __construct(
49
        IApplicationConfiguration $appConfig,
50
        FormFactoryInterface $formFactory,
51
        HttpFoundationFactory $httpFoundationFactory,
52
        Twig_Environment $twig
53
    ) {
54
        $this->appConfig = $appConfig;
55
        $this->formFactory = $formFactory;
56
        $this->httpFoundationFactory = $httpFoundationFactory;
57
        $this->twig = $twig;
58
    }
59
60
    /**
61
     * @internal
62
     */
63
    public function process(
64
        IAuthenticationProcess $authenticationProcess,
65
        ?ServerRequestInterface $httpRequest
66
    ): IChallengeResponse {
67
        $email = $authenticationProcess
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

67
        $email = /** @scrutinizer ignore-deprecated */ $authenticationProcess
Loading history...
68
            ->getTypedMap()
69
            ->get('email', Scalar::_STR)
70
        ;
71
72
        $form = $this
73
            ->formFactory
74
            ->createBuilder()
75
            ->add('emailCode')
76
            ->add('submit', SubmitType::class)
77
            ->getForm()
78
        ;
79
80
        if (null !== $httpRequest) {
81
            $form->handleRequest(
82
                $this->httpFoundationFactory->createRequest($httpRequest)
83
            );
84
        }
85
86
        if (
87
            $form->isSubmitted() &&
88
            $form->isValid() &&
89
            null!== $httpRequest
90
        ) {
91
            $code = $authenticationProcess
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

91
            $code = /** @scrutinizer ignore-deprecated */ $authenticationProcess
Loading history...
92
                ->getTypedMap()
93
                ->get('email_code_hash', Scalar::_STR)
94
            ;
95
            $isCodeCorrect = password_verify(
96
                $form['emailCode']->getData(),
97
                $code
0 ignored issues
show
Bug introduced by
$code of type object is incompatible with the type string expected by parameter $hash of password_verify(). ( Ignorable by Annotation )

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

97
                /** @scrutinizer ignore-type */ $code
Loading history...
98
            );
99
            if (true !== $isCodeCorrect) {
100
                $form->addError(new FormError('The code you entered is incorrect'));
101
            }
102
        }
103
104
        if ($form->isSubmitted() && $form->isValid()) {
105
            return new ChallengeResponse(
106
                $authenticationProcess,
107
                null,
108
                false,
109
                true
110
            )
111
            ;
112
        }
113
114
        $code = random_int(self::CODE_MIN, self::CODE_MAX);
115
        
116
        $email = $authenticationProcess
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

116
        $email = /** @scrutinizer ignore-deprecated */ $authenticationProcess
Loading history...
Bug introduced by
Are you sure the assignment to $email is correct as $authenticationProcess->...($email, (string)$code) targeting LM\AuthAbstractor\Model\IMailer::send() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Unused Code introduced by
The assignment to $email is dead and can be removed.
Loading history...
117
            ->getTypedMap()
118
            ->get('mailer', IMailer::class)
119
            ->send($email, (string) $code)
0 ignored issues
show
Bug introduced by
$email of type object is incompatible with the type string expected by parameter $to of LM\AuthAbstractor\Model\IMailer::send(). ( Ignorable by Annotation )

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

119
            ->send(/** @scrutinizer ignore-type */ $email, (string) $code)
Loading history...
120
        ;
121
122
        $response = new Response($this->twig->render('email.html.twig', [
123
            "form" => $form->createView(),
124
        ]));
125
126
        return new ChallengeResponse(
127
            new AuthenticationProcess(
128
                $authenticationProcess
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

128
                /** @scrutinizer ignore-deprecated */ $authenticationProcess
Loading history...
129
                ->getTypedMap()
130
                ->set(
131
                    'email_code_hash',
132
                    password_hash((string) $code, PASSWORD_DEFAULT),
133
                    Scalar::_STR
134
                )
135
            ),
136
            $response,
137
            $form->isSubmitted(),
138
            false
139
        )
140
        ;
141
    }
142
}
143