Passed
Push — master ( d3fd5d...81f334 )
by Mihail
03:51
created

ActionNotifications::notifications()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 14
nc 2
nop 0
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Apps\Controller\Api\Profile;
4
5
use Apps\ActiveRecord\Message;
6
use Apps\ActiveRecord\UserNotification;
7
use Ffcms\Core\App;
8
use Ffcms\Core\Exception\ForbiddenException;
9
use Ffcms\Core\Network\Request;
10
use Ffcms\Core\Network\Response;
11
12
/**
13
 * Trait ActionNotifications
14
 * @package Apps\Controller\Api\Profile
15
 * @property Request $request
16
 * @property Response $response
17
 * @method void setJsonHeader()
18
 */
19
trait ActionNotifications
20
{
21
    /**
22
     * Get user p.m and notifications count
23
     * @return string
24
     * @throws ForbiddenException
25
     */
26
    public function notifications(): ?string
27
    {
28
        // check if authed
29
        if (!App::$User->isAuth()) {
30
            throw new ForbiddenException('Auth required');
31
        }
32
33
        $this->setJsonHeader();
34
35
        // get user object
36
        $user = App::$User->identity();
37
38
        // get messages count
39
        $messagesCount = Message::where('target_id', '=', $user->id)
40
            ->where('readed', '=', 0)->count();
41
42
        // get notifications count
43
        $notificationsCount = UserNotification::where('user_id', '=', $user->id)
44
            ->where('readed', '=', 0)->count();
45
46
        // render json output
47
        return json_encode([
48
            'status' => 1,
49
            'notify' => $notificationsCount,
50
            'messages' => $messagesCount,
51
            'summary' => $notificationsCount + $messagesCount
52
        ]);
53
    }
54
}
55