Passed
Push — master ( ba613a...1f1f0a )
by Alexander
14:26
created

SignupController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace App\Controller;
5
6
use App\Entity\User;
7
use App\ViewRenderer;
8
use Cycle\ORM\ORMInterface;
9
use Cycle\ORM\Transaction;
10
use Psr\Http\Message\RequestInterface;
11
use Psr\Http\Message\ResponseFactoryInterface;
12
use Psr\Http\Message\ResponseInterface;
13
use Psr\Log\LoggerInterface;
14
use Yiisoft\Auth\IdentityRepositoryInterface;
15
use Yiisoft\Http\Method;
16
use Yiisoft\Router\UrlGeneratorInterface;
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
        ORMInterface $orm,
31
        UrlGeneratorInterface $urlGenerator,
32
        LoggerInterface $logger,
33
        ResponseFactoryInterface $responseFactory
34
    ): ResponseInterface
35
    {
36
        $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. ( Ignorable by Annotation )

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

36
        /** @scrutinizer ignore-call */ 
37
        $body = $request->getParsedBody();
Loading history...
37
        $error = null;
38
39
        if ($request->getMethod() === Method::POST) {
40
            try {
41
                foreach (['login', 'password'] as $name) {
42
                    if (empty($body[$name])) {
43
                        throw new \InvalidArgumentException(ucfirst($name) . ' is required.');
44
                    }
45
                }
46
47
                /** @var \App\Entity\User $identity */
48
                $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\Repository\UserRepository. ( Ignorable by Annotation )

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

48
                /** @scrutinizer ignore-call */ 
49
                $identity = $identityRepository->findByLogin($body['login']);
Loading history...
49
                if ($identity !== null) {
50
                    throw new \InvalidArgumentException('Unable to register user with such username.');
51
                }
52
53
                $user = new User($body['login'], $body['password']);
54
55
                $transaction = new Transaction($orm);
56
                $transaction->persist($user);
57
58
                $transaction->run();
59
                return $responseFactory
60
                    ->createResponse(302)
61
                    ->withHeader(
62
                        'Location',
63
                        $urlGenerator->generate('site/index')
64
                    );
65
            } catch (\Throwable $e) {
66
                $logger->error($e);
67
                $error = $e->getMessage();
68
            }
69
        }
70
71
        return $this->viewRenderer->render(
72
            'signup',
73
            [
74
                'body' => $body,
75
                'error' => $error,
76
                'csrf' => $request->getAttribute('csrf_token'),
0 ignored issues
show
Bug introduced by
The method getAttribute() 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. ( Ignorable by Annotation )

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

76
                'csrf' => $request->/** @scrutinizer ignore-call */ getAttribute('csrf_token'),
Loading history...
77
            ]
78
        );
79
    }
80
}
81