Thread   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A messages() 0 4 1
A addMessage() 0 9 1
A subject() 0 4 2
A users() 0 7 1
A from() 0 4 1
A to() 0 4 1
A path() 0 4 1
A storeMessagePath() 0 9 1
1
<?php
2
3
namespace Aurawindsurfing\Messenger;
4
5
use App\User;
6
use Illuminate\Support\Str;
7
use Illuminate\Database\Eloquent\Model;
8
9
/**
10
 * Class Thread.
11
 */
12
class Thread extends Model
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $table = 'messenger_threads';
18
19
    protected $guarded = [];
20
21
    /**
22
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
23
     */
24
    public function messages()
25
    {
26
        return $this->hasMany(Message::class)->oldest();
27
    }
28
29
    public function addMessage($body, $userId = null)
30
    {
31
        $message = $this->messages()->create([
32
            'body'    => $body,
33
            'user_id' => $userId,
34
        ]);
35
36
        return $message;
37
    }
38
39
    public function subject()
40
    {
41
        return isset($this->hasMany(Message::class)->oldest()->first()->body) ? Str::limit($this->hasMany(Message::class)->oldest()->first()->body, 40) : 'Create a new message';
42
    }
43
44
    public function users()
45
    {
46
        return User::whereIn('id', [
47
            $this->from()->id,
48
            $this->to()->id,
49
        ]);
50
    }
51
52
    public function from()
53
    {
54
        return User::find($this->from_user_id);
0 ignored issues
show
Documentation introduced by
The property from_user_id does not exist on object<Aurawindsurfing\Messenger\Thread>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
55
    }
56
57
    public function to()
58
    {
59
        return User::find($this->to_user_id);
0 ignored issues
show
Documentation introduced by
The property to_user_id does not exist on object<Aurawindsurfing\Messenger\Thread>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
60
    }
61
62
    public function path()
63
    {
64
        return str_replace_last('{thread?}', $this->id, config('messenger.index'));
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Aurawindsurfing\Messenger\Thread>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
65
    }
66
67
    public function storeMessagePath()
68
    {
69
//
70
//        if (!isset($this->id)) {
71
//            $model = $this->save();
72
//        }
73
74
        return str_replace_last('{thread}', $this->id, config('messenger.store'));
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Aurawindsurfing\Messenger\Thread>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
75
    }
76
}
77