SignupController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Controller;
6
7
use App\User\User;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\ResponseFactoryInterface;
10
use Psr\Http\Message\ResponseInterface;
11
use Psr\Log\LoggerInterface;
12
use Yiisoft\Auth\IdentityRepositoryInterface;
13
use Yiisoft\Http\Method;
14
use Yiisoft\Router\UrlGeneratorInterface;
15
use Yiisoft\Yii\Cycle\Data\Writer\EntityWriter;
16
use Yiisoft\Yii\View\ViewRenderer;
17
18
final class SignupController
19
{
20
    private ViewRenderer $viewRenderer;
21
22
    public function __construct(ViewRenderer $viewRenderer)
23
    {
24
        $this->viewRenderer = $viewRenderer->withControllerName('signup');
25
    }
26
27
    public function signup(
28
        RequestInterface $request,
29
        IdentityRepositoryInterface $identityRepository,
30
        EntityWriter $entityWriter,
31
        UrlGeneratorInterface $urlGenerator,
32
        LoggerInterface $logger,
33
        ResponseFactoryInterface $responseFactory
34
    ): ResponseInterface {
35
        $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

35
        /** @scrutinizer ignore-call */ 
36
        $body = $request->getParsedBody();
Loading history...
36
        $error = null;
37
38
        if ($request->getMethod() === Method::POST) {
39
            try {
40
                foreach (['login', 'password'] as $name) {
41
                    if (empty($body[$name])) {
42
                        throw new \InvalidArgumentException(ucfirst($name) . ' is required.');
43
                    }
44
                }
45
46
                /** @var \App\User\User $identity */
47
                $identity = $identityRepository->findByLogin($body['login']);
0 ignored issues
show
Bug introduced by
The method findByLogin() does not exist on Yiisoft\Auth\IdentityRepositoryInterface. It seems like you code against a sub-type of Yiisoft\Auth\IdentityRepositoryInterface such as App\User\UserRepository. ( Ignorable by Annotation )

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

47
                /** @scrutinizer ignore-call */ 
48
                $identity = $identityRepository->findByLogin($body['login']);
Loading history...
48
                if ($identity !== null) {
49
                    throw new \InvalidArgumentException('Unable to register user with such username.');
50
                }
51
52
                $user = new User($body['login'], $body['password']);
53
                $entityWriter->write([$user]);
54
55
                return $responseFactory
56
                    ->createResponse(302)
57
                    ->withHeader(
58
                        'Location',
59
                        $urlGenerator->generate('site/index')
60
                    );
61
            } catch (\Throwable $e) {
62
                $logger->error($e);
63
                $error = $e->getMessage();
64
            }
65
        }
66
67
        return $this->viewRenderer->render(
68
            'signup',
69
            [
70
                'body' => $body,
71
                'error' => $error,
72
            ]
73
        );
74
    }
75
}
76