Passed
Push — master ( cd18e5...9103a9 )
by Alexander
01:59
created

ContactController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 22
c 1
b 0
f 0
dl 0
loc 45
ccs 21
cts 21
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A contact() 0 34 4
A __construct() 0 4 1
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
    private ResponseFactoryInterface $responseFactory;
20
21 6
    public function __construct(ViewRenderer $viewRenderer, ResponseFactoryInterface $responseFactory)
22
    {
23 6
        $this->viewRenderer = $viewRenderer->withControllerName('contact');
24 6
        $this->responseFactory = $responseFactory;
25 6
    }
26
27 6
    public function contact(
28
        ContactForm $form,
29
        Flash $flash,
30
        ContactMailer $mailer,
31
        UrlGeneratorInterface $url,
32
        ServerRequestInterface $request
33
    ): ResponseInterface {
34 6
        $body = $request->getParsedBody();
35 6
        $method = $request->getMethod();
36
37 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

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