Completed
Push — master ( 49cca7...006e45 )
by Raffael
13:02 queued 09:25
created

Notifications::postMail()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 0
cts 23
cp 0
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 20
nc 2
nop 3
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * balloon
7
 *
8
 * @copyright   Copryright (c) 2012-2018 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Balloon\App\Notification\Api\v2;
13
14
use Balloon\App\Api\Controller;
15
use Balloon\App\Notification\AttributeDecorator as NotificationAttributeDecorator;
16
use Balloon\App\Notification\Notifier;
17
use Balloon\Async\Mail;
18
use Balloon\AttributeDecorator\Pager;
19
use Balloon\Filesystem;
20
use Balloon\Filesystem\Acl\Exception\Forbidden as ForbiddenException;
21
use Balloon\Filesystem\Node\AttributeDecorator as NodeAttributeDecorator;
22
use Balloon\Server;
23
use Balloon\Server\AttributeDecorator as RoleAttributeDecorator;
24
use Balloon\Server\User;
25
use Micro\Http\Response;
26
use MongoDB\BSON\ObjectId;
27
use Psr\Log\LoggerInterface;
28
use TaskScheduler\Async;
29
use Zend\Mail\Message;
30
use Zend\Mime\Message as MimeMessage;
31
use Zend\Mime\Part as MimePart;
32
33
class Notifications extends Controller
34
{
35
    /**
36
     * Notifier.
37
     *
38
     * @var Notifier
39
     */
40
    protected $notifier;
41
42
    /**
43
     * User.
44
     *
45
     * @var User
46
     */
47
    protected $user;
48
49
    /**
50
     * Filesystem.
51
     *
52
     * @var Filesystem
53
     */
54
    protected $fs;
55
56
    /**
57
     * Server.
58
     *
59
     * @var Server
60
     */
61
    protected $server;
62
63
    /**
64
     * Async.
65
     *
66
     * @var Async
67
     */
68
    protected $async;
69
70
    /**
71
     * Logger.
72
     *
73
     * @var LoggerInterface
74
     */
75
    protected $logger;
76
77
    /**
78
     * Role attribute decorator.
79
     *
80
     * @var RoleAttributeDecorator
81
     */
82
    protected $role_decorator;
83
84
    /**
85
     * Role attribute decorator.
86
     *
87
     * @var NodeAttributeDecorator
88
     */
89
    protected $node_decorator;
90
91
    /**
92
     * Notification attribute decorator.
93
     *
94
     * @var NotificationAttributeDecorator
95
     */
96
    protected $notification_decorator;
97
98
    /**
99
     * Constructor.
100
     */
101
    public function __construct(Notifier $notifier, Server $server, Async $async, LoggerInterface $logger, RoleAttributeDecorator $role_decorator, NodeAttributeDecorator $node_decorator, NotificationAttributeDecorator $notification_decorator)
102
    {
103
        $this->notifier = $notifier;
104
        $this->user = $server->getIdentity();
105
        $this->fs = $server->getFilesystem();
106
        $this->server = $server;
107
        $this->async = $async;
108
        $this->logger = $logger;
109
        $this->role_decorator = $role_decorator;
110
        $this->node_decorator = $node_decorator;
111
        $this->notification_decorator = $notification_decorator;
112
    }
113
114
    /**
115
     * @api {get} /api/v2/notifications Get notifications
116
     * @apiVersion 2.0.0
117
     * @apiName get
118
     * @apiGroup App\Notification
119
     * @apiPermission none
120
     * @apiDescription Fetch my nofitifications
121
     *
122
     * @apiExample (cURL) exmaple:
123
     * curl -XGET "https://SERVER/api/v2/notification"
124
     *
125
     * @apiSuccessExample {string} Success-Response:
126
     * HTTP/1.1 200 OK
127
     * [
128
     *  "id": "",
129
     *  "message": "Hi there, this is a notification",
130
     *  "subject": "Hi",
131
     *  "sender": {
132
     *      "id": "",
133
     *      "name": ""
134
     *  }
135
     * ]
136
     *
137
     * @param ObjectId $id
138
     */
139
    public function get(?ObjectId $id = null, array $attributes = [], int $offset = 0, int $limit = 20): Response
140
    {
141
        if ($id !== null) {
142
            $message = $this->notifier->getNotification($id);
143
            $result = $this->notification_decorator->decorate($message, $attributes);
144
145
            return (new Response())->setCode(200)->setBody($result);
146
        }
147
148
        $result = $this->notifier->getNotifications($this->user, $offset, $limit, $total);
149
        $uri = '/api/v2/notifications';
150
        $pager = new Pager($this->notification_decorator, $result, $attributes, $offset, $limit, $uri, $total);
151
        $result = $pager->paging();
152
153
        return (new Response())->setCode(200)->setBody($result);
154
    }
155
156
    /**
157
     * @api {delete} /api/v2/notifications Delete notification
158
     * @apiVersion 2.0.0
159
     * @apiName delete
160
     * @apiGroup App\Notification
161
     * @apiPermission none
162
     * @apiDescription Fetch my nofitifications
163
     *
164
     * @apiExample (cURL) exmaple:
165
     * curl -XGET "https://SERVER/api/v2/notification"
166
     *
167
     * @apiSuccessExample {string} Success-Response:
168
     * HTTP/1.1 204 No Content
169
     */
170
    public function delete(ObjectId $id): Response
171
    {
172
        $this->notifier->deleteNotification($id);
173
174
        return (new Response())->setCode(204);
175
    }
176
177
    /**
178
     * @api {post} /api/v2/notifications Post a notification to a group of users
179
     * @apiVersion 2.0.0
180
     * @apiName post
181
     * @apiGroup App\Notification
182
     * @apiPermission none
183
     * @apiDescription Send notification
184
     *
185
     * @apiExample (cURL) exmaple:
186
     * curl -XPOST "https://SERVER/api/v2/notification"
187
     *
188
     * @apiSuccessExample {string} Success-Response:
189
     * HTTP/1.1 202 Accepted
190
     */
191
    public function post(array $receiver, string $subject, string $body): Response
192
    {
193
        $users = $this->server->getUsersById($receiver);
194
        $message = $this->notifier->customMessage($subject, $body);
195
        $this->notifier->notify($users, $this->user, $message);
196
197
        return (new Response())->setCode(202);
198
    }
199
200
    /**
201
     * @api {post} /api/v2/notifications/broadcast Post a notification to all users
202
     * @apiVersion 2.0.0
203
     * @apiName postBroadcast
204
     * @apiGroup App\Notification
205
     * @apiPermission admin
206
     * @apiDescription Send notification
207
     *
208
     * @apiExample (cURL) exmaple:
209
     * curl -XPOST "https://SERVER/api/v2/notifications/broadcast"
210
     *
211
     * @apiSuccessExample {string} Success-Response:
212
     * HTTP/1.1 202 Accepted
213
     */
214
    public function postBroadcast(string $subject, string $body): Response
215
    {
216
        if (!$this->user->isAdmin()) {
217
            throw new ForbiddenException(
218
                'submitted parameters require to have admin privileges',
219
                    ForbiddenException::ADMIN_PRIV_REQUIRED
220
                );
221
        }
222
223
        $users = $this->server->getUsers();
224
        $message = $this->notifier->customMessage($subject, $body);
225
        $this->notifier->notify($users, $this->user, $message);
226
227
        return (new Response())->setCode(202);
228
    }
229
230
    /**
231
     * @api {post} /api/v2/notifications/mail Send a mail
232
     * @apiVersion 2.0.0
233
     * @apiName postMail
234
     * @apiGroup App\Notification
235
     * @apiPermission none
236
     * @apiDescription Send mail
237
     *
238
     * @apiExample (cURL) exmaple:
239
     * curl -XGET "https://SERVER/api/v2/notifications/mail"
240
     *
241
     * @apiSuccessExample {string} Success-Response:
242
     * HTTP/1.1 202 Accepted
243
     */
244
    public function postMail(array $receiver, string $subject, string $body)
245
    {
246
        $message = $this->notifier->customMessage($subject, $body);
247
248
        $html = new MimePart($message->renderTemplate('mail_html.phtml'));
249
        $html->type = 'text/html';
250
        $html->setCharset('utf-8');
251
252
        $plain = new MimePart($message->renderTemplate('mail_plain.phtml'));
253
        $plain->type = 'text/plain';
254
        $plain->setCharset('utf-8');
255
        $body = new MimeMessage();
256
        $body->setParts([$html, $plain]);
257
258
        $mail = (new Message())
259
          ->setSubject($message->getSubject())
260
          ->setBody($body)
261
          ->setEncoding('UTF-8');
262
263
        $type = $mail->getHeaders()->get('Content-Type');
264
        $type->setType('multipart/alternative');
265
266
        foreach ($receiver as $address) {
267
            $mail->setTo($address);
268
            $this->async->addJob(Mail::class, $mail->toString());
269
        }
270
271
        return (new Response())->setCode(202);
272
    }
273
}
274