Completed
Push — master ( 4e528e...b698b3 )
by ARCANEDEV
04:17
created

Messagable   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 2
cbo 2
dl 0
loc 90
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A discussions() 0 9 1
A participants() 0 6 1
A messages() 0 6 1
A newMessagesCount() 0 4 1
A discussionsWithNewMessages() 0 19 2
1
<?php namespace Arcanedev\LaravelMessenger\Traits;
2
3
use Arcanedev\LaravelMessenger\Contracts\Discussion as DiscussionContract;
4
use Arcanedev\LaravelMessenger\Contracts\Participant as ParticipantContract;
5
use Arcanedev\LaravelMessenger\Models;
6
7
/**
8
 * Trait     Messagable
9
 *
10
 * @package  Arcanedev\LaravelMessenger\Traits
11
 * @author   ARCANEDEV <[email protected]>
12
 *
13
 * @property  int                                       id
14
 * @property  \Illuminate\Database\Eloquent\Collection  discussions
15
 * @property  \Illuminate\Database\Eloquent\Collection  participants
16
 * @property  \Illuminate\Database\Eloquent\Collection  messages
17
 */
18
trait Messagable
19
{
20
    /* ------------------------------------------------------------------------------------------------
21
     |  Traits
22
     | ------------------------------------------------------------------------------------------------
23
     */
24
    use ConfigHelper;
25
26
    /* ------------------------------------------------------------------------------------------------
27
     |  Relationships
28
     | ------------------------------------------------------------------------------------------------
29
     */
30
    /**
31
     * Thread relationship.
32
     *
33
     * @return \Illuminate\Database\Eloquent\Relations\belongsToMany
34
     */
35
    public function discussions()
36
    {
37
        return $this->belongsToMany(
0 ignored issues
show
Bug introduced by
It seems like belongsToMany() 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
            $this->getModelFromConfig('discussions', Models\Discussion::class),
39
            $this->getTableFromConfig('participants', 'participants'),
40
            'user_id',
41
            'discussion_id'
42
        );
43
    }
44
45
    /**
46
     * Participants relationship.
47
     *
48
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
49
     */
50
    public function participants()
51
    {
52
        return $this->hasMany(
0 ignored issues
show
Bug introduced by
It seems like hasMany() 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...
53
            $this->getModelFromConfig('participants', Models\Participant::class)
54
        );
55
    }
56
57
    /**
58
     * Message relationship.
59
     *
60
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
61
     */
62
    public function messages()
63
    {
64
        return $this->hasMany(
0 ignored issues
show
Bug introduced by
It seems like hasMany() 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...
65
            $this->getModelFromConfig('messages', Models\Message::class)
66
        );
67
    }
68
69
    /* ------------------------------------------------------------------------------------------------
70
     |  Main Functions
71
     | ------------------------------------------------------------------------------------------------
72
     */
73
    /**
74
     * Returns the new messages count for user.
75
     *
76
     * @return int
77
     */
78
    public function newMessagesCount()
79
    {
80
        return count($this->discussionsWithNewMessages());
81
    }
82
83
    /**
84
     * Returns all discussions IDs with new messages.
85
     *
86
     * @return array
87
     */
88
    public function discussionsWithNewMessages()
89
    {
90
        /** @var \Illuminate\Database\Eloquent\Collection  $participants */
91
        $participants = app(ParticipantContract::class)
92
            ->where('user_id', $this->id)
93
            ->get()
94
            ->pluck('last_read', 'discussion_id');
95
96
        if ($participants->isEmpty()) return [];
97
98
        /** @var \Illuminate\Database\Eloquent\Collection  $discussions */
99
        $discussions = app(DiscussionContract::class)
100
            ->whereIn('id', $participants->keys()->toArray())
101
            ->get();
102
103
        return $discussions->filter(function (Models\Discussion $discussion) use ($participants) {
104
            return $discussion->updated_at > $participants->get($discussion->id);
105
        })->pluck('id')->toArray();
106
    }
107
}
108