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

ActionListMessageDialog   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 80
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 7

1 Method

Rating   Name   Duplication   Size   Complexity  
C listMessageDialog() 0 70 13
1
<?php
2
3
namespace Apps\Controller\Api\Profile;
4
5
use Apps\ActiveRecord\Blacklist;
6
use Apps\ActiveRecord\Message;
7
use Ffcms\Core\App;
8
use Ffcms\Core\Exception\ForbiddenException;
9
use Ffcms\Core\Helper\Type\Any;
10
use Ffcms\Core\Helper\Type\Arr;
11
use Ffcms\Core\Network\Request;
12
use Ffcms\Core\Network\Response;
13
use Illuminate\Database\Capsule\Manager as Capsule;
14
15
/**
16
 * Trait ActionListMessageDialog
17
 * @package Apps\Controller\Api\Profile
18
 * @property Request $request
19
 * @property Response $response
20
 * @method void setJsonHeader
21
 */
22
trait ActionListMessageDialog
23
{
24
    /**
25
     * Load user dialog list based on offset
26
     * @param int $offset
27
     * @param int $new
28
     * @return string
29
     * @throws ForbiddenException
30
     */
31
    public function listMessageDialog($offset = 0, $new = 0): ?string
32
    {
33
        // check is user auth
34
        if (!App::$User->isAuth()) {
35
            throw new ForbiddenException('Auth required');
36
        }
37
        $this->setJsonHeader();
0 ignored issues
show
Bug introduced by
It seems like setJsonHeader() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
38
39
        // check is offset is int
40
        if ($offset !== 0 && !Any::isInt($offset)) {
41
            $offset = 0;
42
        }
43
        ++$offset;
44
45
        // get user person
46
        $user = App::$User->identity();
47
48
        $records = Message::select('readed', 'target_id', 'sender_id', Capsule::raw('max(created_at) as cmax'))
49
            ->where('target_id', '=', $user->id)
50
            ->orWhere('sender_id', '=', $user->id)
51
            ->orderBy('readed', 'ASC') //- error happens, cuz readed is boolean in pgsql
52
            ->orderBy('cmax', 'DESC')
53
            ->groupBy(['sender_id', 'target_id', 'readed']) // multiple order's can throw exception on some kind of database engines
54
            ->take($offset * self::MSG_USER_LIST)
55
            ->get();
56
57
        $userList = [];
58
        $unreadList = [];
59
60
        if (Any::isInt($new) && $new > 0 && App::$User->isExist($new)) {
61
            $userList[] = $new;
62
        }
63
64
        // there is 2 way of messages: me->user; user->me, try to parse it
65
        foreach ($records as $row) {
66
            // target is not myself? then i'm - sender (remote user is target: my->to_user)
67
            if ($row->target_id !== $user->id) {
68
                $userList[] = $row->target_id;
69
            }
70
71
            // sender is not myself? then i'm - target (remote user is sender user->to_me)
72
            if ($row->sender_id !== $user->id) {
73
                $userList[] = $row->sender_id;
74
                if ((bool)$row->readed !== true) {
75
                    $unreadList[] = $row->sender_id;
76
                }
77
            }
78
        }
79
80
        // store only unique users in dialog
81
        $userList = array_unique($userList, SORT_NUMERIC);
82
        // generate json response based on userList and unreadList
83
        $response = [];
84
        foreach ($userList as $user_id) {
85
            $identity = App::$User->identity($user_id);
86
            if (null === $identity) {
87
                continue;
88
            }
89
90
            $response[] = [
91
                'user_id' => $user_id,
92
                'user_nick' => $identity->profile->getNickname(),
93
                'user_avatar' => $identity->profile->getAvatarUrl('small'),
94
                'message_new' => Arr::in($user_id, $unreadList),
95
                'user_block' => !Blacklist::check($user->id, $identity->id)
96
            ];
97
        }
98
99
        return json_encode(['status' => 1, 'data' => $response]);
100
    }
101
}
102