Passed
Pull Request — master (#115)
by Dmitriy
23:50 queued 08:56
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\ResponseInterface;
12
use Psr\Log\LoggerInterface;
13
use Yiisoft\Auth\IdentityRepositoryInterface;
14
use Yiisoft\Http\Method;
15
use Yiisoft\Router\UrlGeneratorInterface;
16
17
final class SignupController
18
{
19
    private ViewRenderer $viewRenderer;
20
21
    public function __construct(ViewRenderer $viewRenderer)
22
    {
23
        $this->viewRenderer = $viewRenderer->withControllerName('signup');
24
    }
25
26
    public function signup(RequestInterface $request, IdentityRepositoryInterface $identityRepository, ORMInterface $orm, UrlGeneratorInterface $urlGenerator, LoggerInterface $logger): ResponseInterface
27
    {
28
        $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

28
        /** @scrutinizer ignore-call */ 
29
        $body = $request->getParsedBody();
Loading history...
29
        $error = null;
30
31
        if ($request->getMethod() === Method::POST) {
32
            try {
33
                foreach (['login', 'password'] as $name) {
34
                    if (empty($body[$name])) {
35
                        throw new \InvalidArgumentException(ucfirst($name) . ' is required.');
36
                    }
37
                }
38
39
                /** @var \App\Entity\User $identity */
40
                $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

40
                /** @scrutinizer ignore-call */ 
41
                $identity = $identityRepository->findByLogin($body['login']);
Loading history...
41
                if ($identity !== null) {
42
                    throw new \InvalidArgumentException('Unable to register user with such username.');
43
                }
44
45
                $user = new User($body['login'], $body['password']);
46
47
                $transaction = new Transaction($orm);
48
                $transaction->persist($user);
49
50
                $transaction->run();
51
                return $this->responseFactory
0 ignored issues
show
Bug Best Practice introduced by
The property responseFactory does not exist on App\Controller\SignupController. Did you maybe forget to declare it?
Loading history...
52
                    ->createResponse(302)
53
                    ->withHeader(
54
                        'Location',
55
                        $urlGenerator->generate('site/index')
56
                    );
57
            } catch (\Throwable $e) {
58
                $logger->error($e);
59
                $error = $e->getMessage();
60
            }
61
        }
62
63
        return $this->viewRenderer->render(
64
            'signup',
65
            [
66
                'body' => $body,
67
                'error' => $error,
68
                '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

68
                'csrf' => $request->/** @scrutinizer ignore-call */ getAttribute('csrf_token'),
Loading history...
69
            ]
70
        );
71
    }
72
}
73