Passed
Push — master ( 4b044e...0a503c )
by Alexander
13:27
created

ContactController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
4
namespace App\Contact;
5
6
use App\Form\ContactForm;
7
use App\ViewRenderer;
8
use Psr\Http\Message\ResponseFactoryInterface;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Message\ServerRequestInterface;
11
use Yiisoft\Http\Header;
12
use Yiisoft\Http\Method;
13
use Yiisoft\Router\UrlGeneratorInterface;
14
use Yiisoft\Yii\Web\Flash;
15
16
class ContactController
17
{
18
    private ViewRenderer $viewRenderer;
19 6
    private ResponseFactoryInterface $responseFactory;
20
21
    public function __construct(ViewRenderer $viewRenderer, ResponseFactoryInterface $responseFactory)
22
    {
23
        $this->viewRenderer = $viewRenderer->withControllerName('contact');
24
        $this->responseFactory = $responseFactory;
25
    }
26 6
27 6
    public function contact(
28
        ContactForm $form,
29 6
        Flash $flash,
30 2
        ContactMailer $mailer,
31
        UrlGeneratorInterface $url,
32 2
        ServerRequestInterface $request
33 2
    ): ResponseInterface {
34
        $body = $request->getParsedBody();
35 2
        $method = $request->getMethod();
36
37
        if (($method === Method::POST) && $form->load($body) && $form->validate()) {
0 ignored issues
show
Bug introduced by
It seems like $body can also be of type null and object; however, parameter $data of Yiisoft\Form\FormModel::load() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

37
        if (($method === Method::POST) && $form->load(/** @scrutinizer ignore-type */ $body) && $form->validate()) {
Loading history...
38 2
            $mailer->send($form, $request);
39
40
            $flash->add(
41 2
                'is-success',
42 2
                [
43 2
                    'header' => 'System mailer notification.',
44 2
                    'body' => 'Thanks to contact us, we\'ll get in touch with you as soon as possible.'
45 2
                ],
46
                true
47
            );
48
49 6
            return $this->responseFactory
50 6
                ->createResponse(302)
51
                ->withHeader(
52 6
                    Header::LOCATION,
53 6
                    $url->generate('contact/form')
54
                );
55
        }
56
57
        return $this->viewRenderer->withCsrf()->render(
58 6
            'form',
59
            [
60 6
                'form' => $form
61
            ]
62
        );
63
    }
64
}
65