Passed
Push — master ( 3de7ee...b534e3 )
by Mihail
14:25
created

ActionNotifications   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 50
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 6

1 Method

Rating   Name   Duplication   Size   Complexity  
B notifications() 0 39 3
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: zenn1
5
 * Date: 09.01.2018
6
 * Time: 21:05
7
 */
8
9
namespace Apps\Controller\Front\Profile;
10
11
12
use Apps\ActiveRecord\UserNotification;
13
use Apps\Model\Front\Profile\EntityNotificationsList;
14
use Ffcms\Core\App;
15
use Ffcms\Core\Arch\View;
16
use Ffcms\Core\Exception\ForbiddenException;
17
use Ffcms\Core\Helper\HTML\SimplePagination;
18
use Ffcms\Core\Network\Request;
19
use Ffcms\Core\Network\Response;
20
21
/**
22
 * Trait ActionNotifications
23
 * @package Apps\Controller\Front\Profile
24
 * @property View $view
25
 * @property Request $request
26
 * @property Response $response
27
 */
28
trait ActionNotifications
29
{
30
    private $_notifyPerPage = 25;
31
    /**
32
     * Show user notifications
33
     * @param string $type
34
     * @return string
35
     * @throws ForbiddenException
36
     * @throws \Ffcms\Core\Exception\SyntaxException
37
     */
38
    public function notifications($type = 'all')
39
    {
40
        if (!App::$User->isAuth()) {
41
            throw new ForbiddenException();
42
        }
43
44
        // get page index and current user object
45
        $page = (int)$this->request->query->get('page', 0);
46
        $offset = $page * $this->_notifyPerPage;
47
        $user = App::$User->identity();
48
49
        // try to find notifications in database as active record
50
        $query = UserNotification::where('user_id', '=', $user->id)
51
            ->orderBy('created_at', 'DESC');
52
        if ($type === 'unread') {
53
            $query = $query->where('readed', '=', 0);
54
        }
55
56
        $pagination = new SimplePagination([
57
            'url' => ['profile/notifications'],
58
            'page' => $page,
59
            'step' => $this->_notifyPerPage,
60
            'total' => $query->count()
61
        ]);
62
63
        // get current records as object and build response
64
        $records = $query->skip($offset)->take($this->_notifyPerPage);
65
        $data = $records->get();
66
        $model = new EntityNotificationsList($data);
67
        $model->make();
68
69
        // update reader records
70
        $records->update(['readed' => 1]);
71
72
        return $this->view->render('notifications', [
73
            'model' => $model,
74
            'pagination' => $pagination
75
        ]);
76
    }
77
}