Passed
Pull Request — master (#151)
by
unknown
26:08 queued 11:07
created

ContactController::contact()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 18
rs 9.9666
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 LoggerInterface $logger;
19
    private ViewRenderer $viewRenderer;
20
    private ResponseFactoryInterface $responseFactory;
21
22
    public function __construct(
23
        ViewRenderer $viewRenderer,
24
        ContactMailer $mailer,
25
        LoggerInterface $logger,
26
        ResponseFactoryInterface $responseFactory
27
    ) {
28
        $this->mailer = $mailer;
29
        $this->logger = $logger;
30
        $this->viewRenderer = $viewRenderer->withControllerName('contact');
31
        $this->responseFactory = $responseFactory;
32
    }
33
34
    public function contact(ServerRequestInterface $request, ContactForm $form): ResponseInterface
35
    {
36
        $parameters = [
37
            'form' => $form
38
        ];
39
        $sent = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $sent is dead and can be removed.
Loading history...
40
41
        if (($request->getMethod() === Method::POST)) {
42
            $sent = true;
43
44
            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

44
            if ($form->load(/** @scrutinizer ignore-type */ $request->getParsedBody()) && $form->validate()) {
Loading history...
45
                $this->mailer->send($form, $request);
46
            }
47
48
            $parameters['sent'] = $sent;
49
        }
50
51
        return $this->viewRenderer->withCsrf()->render('form', $parameters);
52
    }
53
}
54