Passed
Pull Request — master (#408)
by Wilmer
09:16 queued 03:33
created

SignupController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 93.75%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 16
c 3
b 0
f 1
dl 0
loc 39
ccs 15
cts 16
cp 0.9375
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A signup() 0 23 6
A redirectToMain() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Auth\Controller;
6
7
use App\Auth\AuthService;
8
use App\Auth\Form\SignupForm;
9
use App\Service\WebControllerService;
10
use Psr\Http\Message\ResponseInterface;
11
use Psr\Http\Message\ServerRequestInterface;
12
use Yiisoft\Http\Method;
13
use Yiisoft\Translator\TranslatorInterface;
14
use Yiisoft\Validator\ValidatorInterface;
15
use Yiisoft\Yii\View\ViewRenderer;
16
17
final class SignupController
18
{
19
    private ViewRenderer $viewRenderer;
20
    private WebControllerService $webService;
21
22 5
    public function __construct(ViewRenderer $viewRenderer, WebControllerService $webService)
23
    {
24 5
        $this->viewRenderer = $viewRenderer->withControllerName('signup');
25 5
        $this->webService = $webService;
26 5
    }
27
28 5
    public function signup(
29
        AuthService $authService,
30
        ServerRequestInterface $request,
31
        TranslatorInterface $translator,
32
        ValidatorInterface $validator
33
    ): ResponseInterface {
34 5
        if (!$authService->isGuest()) {
35
            return $this->redirectToMain();
36
        }
37
38 5
        $body = $request->getParsedBody();
39
40 5
        $signupForm = new SignupForm($authService, $translator);
41
42
        if (
43 5
            $request->getMethod() === Method::POST
44 4
            && $signupForm->load(is_array($body) ? $body : [])
45 4
            && $validator->validate($signupForm)->isValid()
46
        ) {
47 1
            return $this->redirectToMain();
48
        }
49
50 5
        return $this->viewRenderer->render('signup', ['formModel' => $signupForm]);
51
    }
52
53 1
    private function redirectToMain(): ResponseInterface
54
    {
55 1
        return $this->webService->getRedirectResponse('site/index');
56
    }
57
}
58