Passed
Push — master ( 30fec9...0ef1e1 )
by Burak
05:55
created

Thread::messages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
3
namespace App\Models;
4
5
use App\Traits\HasUUID;
6
use Cmgmyr\Messenger\Models\Message;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, App\Models\Message. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
use Cmgmyr\Messenger\Models\Models;
8
use Cmgmyr\Messenger\Models\Thread as BaseThread;
9
use Illuminate\Database\Eloquent\Factories\HasFactory;
10
11
class Thread extends BaseThread
12
{
13
    use HasFactory, HasUUID;
14
15
    /**
16
     * Messages relationship.
17
     *
18
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
19
     *
20
     * @codeCoverageIgnore
21
     */
22
    public function messages()
23
    {
24
        return $this
25
            ->hasMany(Models::classname(Message::class), 'thread_id', 'id')
26
            ->orderBy('created_at', 'desc');
27
    }
28
29
    /**
30
     * Mark a thread as read for a user.
31
     *
32
     * @param int $userId
33
     *
34
     * @return Participant
35
     */
36 13
    public function markAsRead($userId): Participant
37
    {
38 13
        $participant = $this->getParticipantFromUser($userId);
39 13
        $participant->last_read = now();
40 13
        $participant->save();
41
42 13
        return $participant;
43
    }
44
45
    /**
46
     * Restores all participants within a thread that has a new message.
47
     *
48
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
49
     */
50 11
    public function activateAllParticipants()
51
    {
52 11
        $participants = $this->participants()->withTrashed()->get();
53 11
        foreach ($participants as $participant) {
54 7
            $participant->restore();
55
        }
56
57 11
        return $participants;
58
    }
59
}
60