Passed
Push — master ( 66fdc9...e2b180 )
by Alexander
08:00 queued 05:21
created

SiteController::contact()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 47
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 7.0957

Importance

Changes 0
Metric Value
cc 7
eloc 24
c 0
b 0
f 0
nc 5
nop 5
dl 0
loc 47
ccs 21
cts 24
cp 0.875
crap 7.0957
rs 8.6026
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Controller;
6
7
use App\Form\ContactForm;
8
use App\Service\Mailer;
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 SiteController extends AbstractController
18
{
19 3
    public function index(ServerRequestInterface $request): ResponseInterface
20
    {
21 3
        return $this->render(
22 3
            'site/index',
23
            [
24 3
                'csrf' => $request->getAttribute(Csrf::REQUEST_NAME)
25
            ]
26
        );
27
    }
28
29 1
    public function about(ServerRequestInterface $request): ResponseInterface
30
    {
31 1
        return $this->render(
32 1
            'site/about',
33
            [
34 1
                'csrf' => $request->getAttribute(Csrf::REQUEST_NAME)
35
            ]
36
        );
37
    }
38
39 6
    public function contact(
40
        ContactForm $form,
41
        Flash $flash,
42
        Mailer $mailer,
43
        UrlGeneratorInterface $url,
44
        ServerRequestInterface $request
45
    ): ResponseInterface {
46 6
        $body = $request->getParsedBody();
47 6
        $method = $request->getMethod();
48
49 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

49
        if (($method === Method::POST) && $form->load(/** @scrutinizer ignore-type */ $body) && $form->validate()) {
Loading history...
50 2
            $message = $mailer->message($form);
51
52 2
            $attachFiles = $request->getUploadedFiles();
53
54 2
            foreach ($attachFiles as $attachFile) {
55
                foreach ($attachFile as $file) {
56
                    if ($file->getError() === UPLOAD_ERR_OK) {
57
                        $message->addFile($file);
58
                    }
59
                }
60
            }
61
62 2
            $mailer->send($message);
63
64 2
            $flash->add(
65 2
                'is-success',
66
                [
67 2
                    'header' => 'System mailer notification.',
68
                    'body' =>  'Thanks to contact us, we\'ll get in touch with you as soon as possible.'
69
                ],
70 2
                true
71
            );
72
73 2
            return $this->responseFactory
74 2
                ->createResponse(302)
75 2
                ->withHeader(
76 2
                    Header::LOCATION,
77 2
                    $url->generate('site/index')
78
                );
79
        }
80
81 6
        return $this->render(
82 6
            'site/contact',
83
            [
84 6
                'csrf' => $request->getAttribute(Csrf::REQUEST_NAME),
85 6
                'form' => $form
86
            ]
87
        );
88
    }
89
90 8
    public function getViewPath(): string
91
    {
92 8
        return $this->aliases->get('@views');
93
    }
94
}
95