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

BroadcastAction   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
B handle() 0 34 6
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\FlashMessages;
23
use Fisharebest\Webtrees\Http\ViewResponseTrait;
24
use Fisharebest\Webtrees\I18N;
25
use Fisharebest\Webtrees\Services\MessageService;
26
use Fisharebest\Webtrees\Services\UserService;
27
use Psr\Http\Message\ResponseInterface;
28
use Psr\Http\Message\ServerRequestInterface;
29
use Psr\Http\Server\RequestHandlerInterface;
30
31
use function e;
32
use function redirect;
33
use function route;
34
35
/**
36
 * Send messages from an administrator.
37
 */
38
class BroadcastAction 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
        $user    = $request->getAttribute('user');
68
        $params  = $request->getParsedBody();
69
        $body    = $params['body'];
70
        $subject = $params['subject'];
71
        $to      = $params['to'];
72
73
        $ip       = $request->getAttribute('client-ip');
74
        $to_users = $this->message_service->recipientUsers($to);
75
76
        if ($body === '' || $subject === '') {
77
            return redirect(route(BroadcastPage::class, [
78
                'body'    => $body,
79
                'subject' => $subject,
80
                'to'      => $to,
81
            ]));
82
        }
83
84
        $errors = false;
85
86
        foreach ($to_users as $to_user) {
87
            if ($this->message_service->deliverMessage($user, $to_user, $subject, $body, '', $ip)) {
88
                FlashMessages::addMessage(I18N::translate('The message was successfully sent to %s.', e($to_user->realName())), 'success');
89
            } else {
90
                $errors = true;
91
            }
92
        }
93
94
        if ($errors) {
95
            FlashMessages::addMessage(I18N::translate('The message was not sent.'), 'danger');
96
        }
97
98
        return redirect(route(ControlPanel::class));
99
    }
100
}
101