Passed
Push — master ( 149810...e381f9 )
by Greg
06:20
created

ContactPage   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 34 2
A __construct() 0 4 1
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2019 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22
use Fisharebest\Webtrees\Http\ViewResponseTrait;
23
use Fisharebest\Webtrees\I18N;
24
use Fisharebest\Webtrees\Services\MessageService;
25
use Fisharebest\Webtrees\Services\UserService;
26
use Fisharebest\Webtrees\Tree;
27
use Psr\Http\Message\ResponseInterface;
28
use Psr\Http\Message\ServerRequestInterface;
29
use Psr\Http\Server\RequestHandlerInterface;
30
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
31
32
use function assert;
33
use function in_array;
34
35
/**
36
 * Compose a message from a visitor.
37
 */
38
class ContactPage implements RequestHandlerInterface
39
{
40
    use ViewResponseTrait;
41
42
    /** @var MessageService */
43
    private $message_service;
44
45
    /** @var UserService */
46
    private $user_service;
47
48
    /**
49
     * MessagePage constructor.
50
     *
51
     * @param MessageService $message_service
52
     * @param UserService    $user_service
53
     */
54
    public function __construct(MessageService $message_service, UserService $user_service)
55
    {
56
        $this->user_service    = $user_service;
57
        $this->message_service = $message_service;
58
    }
59
60
    /**
61
     * @param ServerRequestInterface $request
62
     *
63
     * @return ResponseInterface
64
     */
65
    public function handle(ServerRequestInterface $request): ResponseInterface
66
    {
67
        $tree = $request->getAttribute('tree');
68
        assert($tree instanceof Tree);
69
70
        $referer    = $request->getHeaderLine('referer');
71
        $params     = $request->getQueryParams();
72
        $body       = $params['body'] ?? '';
73
        $from_email = $params['from_email'] ?? '';
74
        $from_name  = $params['from_name'] ?? '';
75
        $subject    = $params['subject'] ?? '';
76
        $to         = $params['to'] ?? '';
77
        $url        = $params['url'] ?? $referer;
78
79
        $to_user = $this->user_service->findByUserName($to);
80
81
        if (!in_array($to_user, $this->message_service->validContacts($tree), false)) {
82
            throw new AccessDeniedHttpException('Invalid contact user id');
83
        }
84
85
        $to_name = $to_user->realName();
86
87
        $title = I18N::translate('Send a message');
88
89
        return $this->viewResponse('contact-page', [
90
            'body'       => $body,
91
            'from_email' => $from_email,
92
            'from_name'  => $from_name,
93
            'subject'    => $subject,
94
            'title'      => $title,
95
            'to'         => $to,
96
            'to_name'    => $to_name,
97
            'tree'       => $tree,
98
            'url'        => $url,
99
        ]);
100
    }
101
}
102