Passed
Push — master ( 17140c...26deac )
by Alexander
13:27
created

SignupController::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
4
namespace App\Controller;
5
6
7
use App\Controller;
8
use App\Entity\User;
9
use Cycle\ORM\ORMInterface;
10
use Cycle\ORM\Transaction;
11
use Psr\Http\Message\RequestInterface;
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 extends Controller
19
{
20
    protected function getId(): string
21
    {
22
        return 'signup';
23
    }
24
25
    public function signup(RequestInterface $request, IdentityRepositoryInterface $identityRepository, ORMInterface $orm, UrlGeneratorInterface $urlGenerator, LoggerInterface $logger): ResponseInterface
26
    {
27
        $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

27
        /** @scrutinizer ignore-call */ 
28
        $body = $request->getParsedBody();
Loading history...
28
        $error = null;
29
30
        if ($request->getMethod() === Method::POST) {
31
            try {
32
                foreach (['login', 'password'] as $name) {
33
                    if (empty($body[$name])) {
34
                        throw new \InvalidArgumentException(ucfirst($name) . ' is required.');
35
                    }
36
                }
37
38
                /** @var \App\Entity\User $identity */
39
                $identity = $identityRepository->findByLogin($body['login']);
40
                if ($identity !== null) {
41
                    throw new \InvalidArgumentException('Unable to register user with such username.');
42
                }
43
44
                $user = new User($body['login'], $body['password']);
45
46
                $transaction = new Transaction($orm);
47
                $transaction->persist($user);
48
49
                $transaction->run();
50
                return $this->responseFactory
51
                    ->createResponse(302)
52
                    ->withHeader(
53
                        'Location',
54
                        $urlGenerator->generate('site/index')
55
                    );
56
            } catch (\Throwable $e) {
57
                $logger->error($e);
58
                $error = $e->getMessage();
59
            }
60
        }
61
62
        return $this->render(
63
            'signup',
64
            [
65
                'body' => $body,
66
                'error' => $error,
67
                '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

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