Passed
Push — master ( cca6b1...f7b887 )
by Alexander
10:54
created

ContactController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 30
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A contact() 0 17 4
A __construct() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Contact;
6
7
use App\Form\ContactForm;
8
use Psr\Http\Message\ResponseFactoryInterface;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Message\ServerRequestInterface;
11
use Psr\Log\LoggerInterface;
12
use Yiisoft\Http\Method;
13
use Yiisoft\Yii\View\ViewRenderer;
14
15
class ContactController
16
{
17
    private ContactMailer $mailer;
18
    private ViewRenderer $viewRenderer;
19
20
    public function __construct(
21
        ViewRenderer $viewRenderer,
22
        ContactMailer $mailer
23
    ) {
24
        $this->mailer = $mailer;
25
        $this->viewRenderer = $viewRenderer->withControllerName('contact');
26
    }
27
28
    public function contact(ServerRequestInterface $request, ContactForm $form): ResponseInterface
29
    {
30
        $parameters = [
31
            'form' => $form
32
        ];
33
34
        if (($request->getMethod() === Method::POST)) {
35
            $sent = true;
36
37
            if ($form->load($request->getParsedBody()) && $form->validate()) {
0 ignored issues
show
Bug introduced by
It seems like $request->getParsedBody() 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 ($form->load(/** @scrutinizer ignore-type */ $request->getParsedBody()) && $form->validate()) {
Loading history...
38
                $this->mailer->send($form, $request);
39
            }
40
41
            $parameters['sent'] = $sent;
42
        }
43
44
        return $this->viewRenderer->withCsrf()->render('form', $parameters);
45
    }
46
}
47