|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Contact; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
|
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
10
|
|
|
use Yiisoft\Http\Header; |
|
11
|
|
|
use Yiisoft\Http\Method; |
|
12
|
|
|
use Yiisoft\Http\Status; |
|
13
|
|
|
use Yiisoft\Router\UrlGeneratorInterface; |
|
14
|
|
|
use Yiisoft\Validator\ValidatorInterface; |
|
15
|
|
|
use Yiisoft\Yii\View\ViewRenderer; |
|
16
|
|
|
|
|
17
|
|
|
class ContactController |
|
18
|
|
|
{ |
|
19
|
|
|
private ContactMailer $mailer; |
|
20
|
|
|
private ResponseFactoryInterface $responseFactory; |
|
21
|
|
|
private UrlGeneratorInterface $url; |
|
22
|
|
|
private ViewRenderer $viewRenderer; |
|
23
|
|
|
|
|
24
|
6 |
|
public function __construct( |
|
25
|
|
|
ContactMailer $mailer, |
|
26
|
|
|
ResponseFactoryInterface $responseFactory, |
|
27
|
|
|
UrlGeneratorInterface $url, |
|
28
|
|
|
ViewRenderer $viewRenderer |
|
29
|
|
|
) { |
|
30
|
6 |
|
$this->mailer = $mailer; |
|
31
|
6 |
|
$this->responseFactory = $responseFactory; |
|
32
|
6 |
|
$this->url = $url; |
|
33
|
6 |
|
$this->viewRenderer = $viewRenderer |
|
34
|
6 |
|
->withControllerName('contact') |
|
35
|
6 |
|
->withViewBasePath(__DIR__ . '/views'); |
|
36
|
6 |
|
} |
|
37
|
|
|
|
|
38
|
6 |
|
public function contact( |
|
39
|
|
|
ValidatorInterface $validator, |
|
40
|
|
|
ServerRequestInterface $request |
|
41
|
|
|
): ResponseInterface { |
|
42
|
6 |
|
$body = $request->getParsedBody(); |
|
43
|
6 |
|
$form = new ContactForm(); |
|
44
|
6 |
|
if (($request->getMethod() === Method::POST) && $form->load((array)$body) && $validator->validate($form)->isValid()) { |
|
45
|
2 |
|
$this->mailer->send($form, $request); |
|
46
|
|
|
|
|
47
|
2 |
|
return $this->responseFactory |
|
48
|
2 |
|
->createResponse(Status::FOUND) |
|
49
|
2 |
|
->withHeader(Header::LOCATION, $this->url->generate('site/contact')); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
6 |
|
return $this->viewRenderer->render('form', ['form' => $form]); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|