Messagable   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 80
c 0
b 0
f 0
wmc 5
lcom 1
cbo 2
ccs 22
cts 22
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A discussions() 0 8 1
A participations() 0 7 1
A messages() 0 7 1
A newMessagesCount() 0 4 1
A discussionsWithNewMessages() 0 12 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\LaravelMessenger\Traits;
6
7
use Arcanedev\LaravelMessenger\Models;
8
use Illuminate\Database\Eloquent\Builder;
9
10
/**
11
 * Trait     Messagable
12
 *
13
 * @package  Arcanedev\LaravelMessenger\Traits
14
 * @author   ARCANEDEV <[email protected]>
15
 *
16
 * @property  int                                       id
17
 * @property  \Illuminate\Database\Eloquent\Collection  discussions
18
 * @property  \Illuminate\Database\Eloquent\Collection  participations
19
 * @property  \Illuminate\Database\Eloquent\Collection  messages
20
 */
21
trait Messagable
22
{
23
    /* -----------------------------------------------------------------
24
     |  Relationships
25
     | -----------------------------------------------------------------
26
     */
27
28
    /**
29
     * Thread relationship.
30
     *
31
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
32
     */
33 12
    public function discussions()
34
    {
35 12
        return $this->morphToMany(
0 ignored issues
show
Bug introduced by
It seems like morphToMany() 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...
36 12
            config('messenger.discussions.model', Models\Discussion::class),
37 12
            config('messenger.users.morph', 'participable'),
38 12
            config('messenger.participations.table', 'participations')
39
        );
40
    }
41
42
    /**
43
     * Participations relationship.
44
     *
45
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
46
     */
47 6
    public function participations()
48
    {
49 6
        return $this->morphMany(
0 ignored issues
show
Bug introduced by
It seems like morphMany() 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...
50 6
            config('messenger.participations.model', Models\Participation::class),
51 6
            config('messenger.users.morph', 'participable')
52
        );
53
    }
54
55
    /**
56
     * Message relationship.
57
     *
58
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
59
     */
60 6
    public function messages()
61
    {
62 6
        return $this->morphMany(
0 ignored issues
show
Bug introduced by
It seems like morphMany() 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...
63 6
            config('messenger.messages.model', Models\Message::class),
64 6
            config('messenger.users.morph', 'participable')
65
        );
66
    }
67
68
    /* -----------------------------------------------------------------
69
     |  Main Methods
70
     | -----------------------------------------------------------------
71
     */
72
73
    /**
74
     * Returns the new messages count for user.
75
     *
76
     * @return int
77
     */
78 6
    public function newMessagesCount()
79
    {
80 6
        return $this->discussionsWithNewMessages()->count();
81
    }
82
83
    /**
84
     * Returns all discussions IDs with new messages.
85
     *
86
     * @return \Illuminate\Database\Eloquent\Collection
87
     */
88 6
    public function discussionsWithNewMessages()
89
    {
90 6
        $participationsTable = config('messenger.participations.table', 'participations');
91 6
        $discussionsTable    = config('messenger.discussions.table', 'discussions');
92
93
        return $this->discussions()->where(function (Builder $query) use ($participationsTable, $discussionsTable) {
94 6
            $query->whereNull("$participationsTable.last_read");
95 6
            $query->orWhere(
96 6
                "$discussionsTable.updated_at", '>', $this->getConnection()->raw("$participationsTable.last_read")
0 ignored issues
show
Bug introduced by
It seems like getConnection() 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...
97
            );
98 6
        })->get();
99
    }
100
}
101