Passed
Push — master ( a9f547...49cca7 )
by Raffael
04:18
created

Notifications::postSubscribtions()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 35
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 0
cts 30
cp 0
rs 8.439
c 0
b 0
f 0
cc 6
crap 42
eloc 22
nc 7
nop 5
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
31
class Notifications extends Controller
32
{
33
    /**
34
     * Notifier.
35
     *
36
     * @var Notifier
37
     */
38
    protected $notifier;
39
40
    /**
41
     * User.
42
     *
43
     * @var User
44
     */
45
    protected $user;
46
47
    /**
48
     * Filesystem.
49
     *
50
     * @var Filesystem
51
     */
52
    protected $fs;
53
54
    /**
55
     * Server.
56
     *
57
     * @var Server
58
     */
59
    protected $server;
60
61
    /**
62
     * Async.
63
     *
64
     * @var Async
65
     */
66
    protected $async;
67
68
    /**
69
     * Logger.
70
     *
71
     * @var LoggerInterface
72
     */
73
    protected $logger;
74
75
    /**
76
     * Role attribute decorator.
77
     *
78
     * @var RoleAttributeDecorator
79
     */
80
    protected $role_decorator;
81
82
    /**
83
     * Role attribute decorator.
84
     *
85
     * @var NodeAttributeDecorator
86
     */
87
    protected $node_decorator;
88
89
    /**
90
     * Notification attribute decorator.
91
     *
92
     * @var NotificationAttributeDecorator
93
     */
94
    protected $notification_decorator;
95
96
    /**
97
     * Constructor.
98
     */
99
    public function __construct(Notifier $notifier, Server $server, Async $async, LoggerInterface $logger, RoleAttributeDecorator $role_decorator, NodeAttributeDecorator $node_decorator, NotificationAttributeDecorator $notification_decorator)
100
    {
101
        $this->notifier = $notifier;
102
        $this->user = $server->getIdentity();
103
        $this->fs = $server->getFilesystem();
104
        $this->server = $server;
105
        $this->async = $async;
106
        $this->logger = $logger;
107
        $this->role_decorator = $role_decorator;
108
        $this->node_decorator = $node_decorator;
109
        $this->notification_decorator = $notification_decorator;
110
    }
111
112
    /**
113
     * @api {get} /api/v2/notifications Get notifications
114
     * @apiVersion 2.0.0
115
     * @apiName get
116
     * @apiGroup App\Notification
117
     * @apiPermission none
118
     * @apiDescription Fetch my nofitifications
119
     *
120
     * @apiExample (cURL) exmaple:
121
     * curl -XGET "https://SERVER/api/v2/notification"
122
     *
123
     * @apiSuccessExample {string} Success-Response:
124
     * HTTP/1.1 200 OK
125
     * [
126
     *  "id": "",
127
     *  "message": "Hi there, this is a notification",
128
     *  "subject": "Hi",
129
     *  "sender": {
130
     *      "id": "",
131
     *      "name": ""
132
     *  }
133
     * ]
134
     *
135
     * @param ObjectId $id
136
     */
137
    public function get(?ObjectId $id = null, array $attributes = [], int $offset = 0, int $limit = 20): Response
138
    {
139
        if ($id !== null) {
140
            $message = $this->notifier->getNotification($id);
141
            $result = $this->notification_decorator->decorate($message, $attributes);
142
143
            return (new Response())->setCode(200)->setBody($result);
144
        }
145
146
        $result = $this->notifier->getNotifications($this->user, $offset, $limit, $total);
147
        $uri = '/api/v2/notifications';
148
        $pager = new Pager($this->notification_decorator, $result, $attributes, $offset, $limit, $uri, $total);
149
        $result = $pager->paging();
150
151
        return (new Response())->setCode(200)->setBody($result);
152
    }
153
154
    /**
155
     * @api {delete} /api/v2/notifications Delete notification
156
     * @apiVersion 2.0.0
157
     * @apiName delete
158
     * @apiGroup App\Notification
159
     * @apiPermission none
160
     * @apiDescription Fetch my nofitifications
161
     *
162
     * @apiExample (cURL) exmaple:
163
     * curl -XGET "https://SERVER/api/v2/notification"
164
     *
165
     * @apiSuccessExample {string} Success-Response:
166
     * HTTP/1.1 204 No Content
167
     */
168
    public function delete(ObjectId $id): Response
169
    {
170
        $this->notifier->deleteNotification($id);
171
172
        return (new Response())->setCode(204);
173
    }
174
175
    /**
176
     * @api {post} /api/v2/notifications Post a notification to a group of users
177
     * @apiVersion 2.0.0
178
     * @apiName post
179
     * @apiGroup App\Notification
180
     * @apiPermission none
181
     * @apiDescription Send notification
182
     *
183
     * @apiExample (cURL) exmaple:
184
     * curl -XPOST "https://SERVER/api/v2/notification"
185
     *
186
     * @apiSuccessExample {string} Success-Response:
187
     * HTTP/1.1 202 Accepted
188
     */
189
    public function post(array $receiver, string $subject, string $body): Response
190
    {
191
        $users = $this->server->getUsersById($receiver);
192
        $message = $this->notifier->customMessage($subject, $body);
193
        $this->notifier->notify($users, $this->user, $message);
194
195
        return (new Response())->setCode(202);
196
    }
197
198
    /**
199
     * @api {post} /api/v2/notifications/broadcast Post a notification to all users
200
     * @apiVersion 2.0.0
201
     * @apiName postBroadcast
202
     * @apiGroup App\Notification
203
     * @apiPermission admin
204
     * @apiDescription Send notification
205
     *
206
     * @apiExample (cURL) exmaple:
207
     * curl -XPOST "https://SERVER/api/v2/notifications/broadcast"
208
     *
209
     * @apiSuccessExample {string} Success-Response:
210
     * HTTP/1.1 202 Accepted
211
     */
212
    public function postBroadcast(string $subject, string $body): Response
213
    {
214
        if (!$this->user->isAdmin()) {
215
            throw new ForbiddenException(
216
                'submitted parameters require to have admin privileges',
217
                    ForbiddenException::ADMIN_PRIV_REQUIRED
218
                );
219
        }
220
221
        $users = $this->server->getUsers();
222
        $message = $this->notifier->customMessage($subject, $body);
223
        $this->notifier->notify($users, $this->user, $message);
224
225
        return (new Response())->setCode(202);
226
    }
227
228
    /**
229
     * @api {post} /api/v2/notifications/mail Send a mail
230
     * @apiVersion 2.0.0
231
     * @apiName postMail
232
     * @apiGroup App\Notification
233
     * @apiPermission none
234
     * @apiDescription Send mail
235
     *
236
     * @apiExample (cURL) exmaple:
237
     * curl -XGET "https://SERVER/api/v2/notifications/mail"
238
     *
239
     * @apiSuccessExample {string} Success-Response:
240
     * HTTP/1.1 202 Accepted
241
     */
242
    public function postMail(array $receiver, string $subject, string $body)
243
    {
244
        $mail = new Message();
245
        $mail->setBody($body)
246
          ->setFrom($this->user->getAttributes()['mail'], $this->user->getAttributes()['username'])
247
          ->setSubject($subject)
248
          ->setTo($this->user->getAttributes()['mail'], 'Undisclosed Recipients')
249
          ->setBcc($receiver);
250
        $this->async->addJob(Mail::class, $mail->toString());
251
252
        return (new Response())->setCode(202);
253
    }
254
}
255