Test Failed
Pull Request — master (#494)
by
unknown
03:32
created

SignupController::signup()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 12
nc 3
nop 4
dl 0
loc 25
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Http\Controllers\Auth;
6
7
use App\Http\Service\WebControllerService;
8
use App\Modules\Auth\AuthService;
9
use App\Modules\Auth\Form\SignupForm;
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
    public function __construct(private WebControllerService $webService, private ViewRenderer $viewRenderer)
20
    {
21
        $this->viewRenderer = $viewRenderer->withControllerName('signup');
22
    }
23
24
    public function signup(
25
        AuthService $authService,
26
        ServerRequestInterface $request,
27
        TranslatorInterface $translator,
28
        ValidatorInterface $validator
29
    ): ResponseInterface {
30
        if (!$authService->isGuest()) {
31
            return $this->redirectToMain();
32
        }
33
34
        $body = $request->getParsedBody();
35
36
        $signupForm = new SignupForm($authService, $translator);
37
38
        if (
39
            $request->getMethod() === Method::POST
40
            && $signupForm->load(is_array($body) ? $body : [])
41
            && $validator
42
                ->validate($signupForm)
43
                ->isValid()
44
        ) {
45
            return $this->redirectToMain();
46
        }
47
48
        return $this->viewRenderer->render('signup', ['formModel' => $signupForm]);
49
    }
50
51
    private function redirectToMain(): ResponseInterface
52
    {
53
        return $this->webService->getRedirectResponse('site/index');
54
    }
55
}
56