Passed
Pull Request — master (#396)
by Wilmer
06:08
created

SignupController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 25
c 2
b 1
f 0
dl 0
loc 50
ccs 23
cts 23
cp 1
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A signup() 0 41 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Controller;
6
7
use App\Form\LoginForm;
8
use App\User\User;
9
use Psr\Http\Message\RequestInterface;
10
use Psr\Http\Message\ResponseFactoryInterface;
11
use Psr\Http\Message\ResponseInterface;
12
use Yiisoft\Auth\IdentityRepositoryInterface;
13
use Yiisoft\Http\Method;
14
use Yiisoft\Router\UrlGeneratorInterface;
15
use Yiisoft\Validator\ValidatorInterface;
16
use Yiisoft\Yii\Cycle\Data\Writer\EntityWriter;
17
use Yiisoft\Yii\View\ViewRenderer;
18
19
final class SignupController
20
{
21
    private ViewRenderer $viewRenderer;
22
23 4
    public function __construct(ViewRenderer $viewRenderer)
24
    {
25 4
        $this->viewRenderer = $viewRenderer->withControllerName('signup');
26 4
    }
27
28 4
    public function signup(
29
        EntityWriter $entityWriter,
30
        IdentityRepositoryInterface $identityRepository,
31
        LoginForm $loginForm,
32
        RequestInterface $request,
33
        ResponseFactoryInterface $responseFactory,
34
        UrlGeneratorInterface $urlGenerator,
35
        ValidatorInterface $validator
36
    ): ResponseInterface {
37 4
        $body = $request->getParsedBody();
0 ignored issues
show
Bug introduced by
The method getParsedBody() does not exist on Psr\Http\Message\RequestInterface. It seems like you code against a sub-type of Psr\Http\Message\RequestInterface such as Psr\Http\Message\ServerRequestInterface or GuzzleHttp\Psr7\ServerRequest. ( Ignorable by Annotation )

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

37
        /** @scrutinizer ignore-call */ 
38
        $body = $request->getParsedBody();
Loading history...
38 4
        $error = null;
39
40
        if (
41 4
            $request->getMethod() === Method::POST
42 3
            && $loginForm->load($body)
43 3
            && $validator->validate($loginForm)->isValid()
44
        ) {
45
            /** @var User $identity */
46 2
            $identity = $identityRepository->findByLogin($loginForm->getAttributeValue('login'));
47
48 2
            if ($identity !== null) {
49 1
                $loginForm->getFormErrors()->addError('password', 'Unable to register user with such username.');
50
            } else {
51 1
                $user = new User($loginForm->getAttributeValue('login'), $loginForm->getAttributeValue('password'));
0 ignored issues
show
Bug introduced by
It seems like $loginForm->getAttributeValue('login') can also be of type boolean and iterable and null; however, parameter $login of App\User\User::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

51
                $user = new User(/** @scrutinizer ignore-type */ $loginForm->getAttributeValue('login'), $loginForm->getAttributeValue('password'));
Loading history...
Bug introduced by
It seems like $loginForm->getAttributeValue('password') can also be of type boolean and iterable and null; however, parameter $password of App\User\User::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

51
                $user = new User($loginForm->getAttributeValue('login'), /** @scrutinizer ignore-type */ $loginForm->getAttributeValue('password'));
Loading history...
52 1
                $entityWriter->write([$user]);
53
54
                return $responseFactory
55 1
                    ->createResponse(302)
56 1
                    ->withHeader(
57 1
                        'Location',
58 1
                        $urlGenerator->generate('site/index')
59
                    );
60
            }
61
        }
62
63 4
        return $this->viewRenderer->render(
64 4
            'signup',
65
            [
66 4
                'body' => $body,
67 4
                'formModel' => $loginForm,
68 4
                'error' => $error,
69
            ]
70
        );
71
    }
72
}
73