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

BroadcastPage::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 20
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 27
rs 9.6
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\Contracts\UserInterface;
23
use Fisharebest\Webtrees\Http\ViewResponseTrait;
24
use Fisharebest\Webtrees\Services\MessageService;
25
use Fisharebest\Webtrees\Services\UserService;
26
use Psr\Http\Message\ResponseInterface;
27
use Psr\Http\Message\ServerRequestInterface;
28
use Psr\Http\Server\RequestHandlerInterface;
29
30
/**
31
 * Compose messages from an administrator.
32
 */
33
class BroadcastPage implements RequestHandlerInterface
34
{
35
    use ViewResponseTrait;
36
37
    /** @var MessageService */
38
    private $message_service;
39
40
    /** @var UserService */
41
    private $user_service;
42
43
    /**
44
     * MessagePage constructor.
45
     *
46
     * @param MessageService $message_service
47
     * @param UserService    $user_service
48
     */
49
    public function __construct(MessageService $message_service, UserService $user_service)
50
    {
51
        $this->user_service    = $user_service;
52
        $this->message_service = $message_service;
53
    }
54
55
    /**
56
     * @param ServerRequestInterface $request
57
     *
58
     * @return ResponseInterface
59
     */
60
    public function handle(ServerRequestInterface $request): ResponseInterface
61
    {
62
        $user    = $request->getAttribute('user');
63
        $referer = $request->getHeaderLine('referer');
64
        $params  = $request->getQueryParams();
65
        $body    = $params['body'] ?? '';
66
        $subject = $params['subject'] ?? '';
67
        $to      = $params['to'];
68
        $url     = $params['url'] ?? $referer;
69
70
        $to_names = $this->message_service->recipientUsers($to)
71
            ->map(static function (UserInterface $user): string {
72
                return $user->realName();
73
            });
74
75
        $title = $this->message_service->recipientDescription($to);
76
77
        $this->layout = 'layouts/administration';
78
79
        return $this->viewResponse('admin/broadcast', [
80
            'body'     => $body,
81
            'from'     => $user,
82
            'subject'  => $subject,
83
            'title'    => $title,
84
            'to'       => $to,
85
            'to_names' => $to_names,
86
            'url'      => $url,
87
        ]);
88
    }
89
}
90