Passed
Pull Request — master (#408)
by Wilmer
05:31
created

SignupController::redirectToMain()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 2
b 0
f 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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