Passed
Push — master ( e2b180...2e0dfd )
by Alexander
07:55 queued 05:11
created

ContactController::contact()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 35
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 18
c 1
b 0
f 0
nc 2
nop 5
dl 0
loc 35
ccs 18
cts 18
cp 1
crap 4
rs 9.6666
1
<?php
2
3
4
namespace App\Contact;
5
6
7
use App\Controller\AbstractController;
8
use App\Form\ContactForm;
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
use Yiisoft\Yii\Web\Middleware\Csrf;
16
17
class ContactController extends AbstractController
18
{
19 6
    public function contact(
20
        ContactForm $form,
21
        Flash $flash,
22
        ContactMailer $mailer,
23
        UrlGeneratorInterface $url,
24
        ServerRequestInterface $request
25
    ): ResponseInterface {
26 6
        $body = $request->getParsedBody();
27 6
        $method = $request->getMethod();
28
29 6
        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

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