Completed
Push — master ( a1f3ed...74c577 )
by Kirill
02:49
created

User::getKarmaTextAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
namespace Domains;
3
4
use Carbon\Carbon;
5
use Core\Mappers\UserMapperTrait;
6
7
use Illuminate\Auth\Authenticatable;
8
use Illuminate\Auth\Passwords\CanResetPassword;
9
use Illuminate\Foundation\Auth\Access\Authorizable;
10
11
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
12
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
13
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
14
15
/**
16
 * Class User
17
 *
18
 * @property int $id
19
 * @property string $gitter_id
20
 * @property string $name
21
 * @property string $avatar
22
 * @property string $url
23
 * @property string $login
24
 * @property string $email
25
 * @property string $password
26
 * @property string $remember_token
27
 * @property Carbon $created_at
28
 * @property Carbon $updated_at
29
 *
30
 * === Accessors ===
31
 *
32
 * @property-read string $karma_text
33
 * @property-read string $thanks_text
34
 *
35
 * === Relations ===
36
 *
37
 * @property-read Achieve[] $achievements
38
 * @property-read Karma[] $karma
39
 * @property-read Karma[] $thanks
40
 *
41
 */
42
class User extends \Eloquent implements
43
    AuthenticatableContract,
44
    AuthorizableContract,
45
    CanResetPasswordContract
46
{
47
    use Authenticatable,
48
        Authorizable,
49
        CanResetPassword,
50
51
        // Gitter converter
52
        UserMapperTrait;
53
54
    /**
55
     * @var string
56
     */
57
    protected $table = 'users';
58
59
    /**
60
     * @var array
61
     */
62
    protected $hidden = ['password', 'remember_token'];
63
64
    /**
65
     * @var array
66
     */
67
    protected $fillable = ['gitter_id', 'url', 'login', 'name', 'avatar'];
68
69
    /**
70
     *
71
     */
72
    protected static function boot()
73
    {
74
        parent::boot();
75
    }
76
77
    /**
78
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
79
     */
80
    public function achievements()
81
    {
82
        return $this->hasMany(Achieve::class, 'user_id', 'id');
83
    }
84
85
    /**
86
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
87
     */
88
    public function karma()
89
    {
90
        return $this->hasMany(Karma::class, 'user_target_id', 'id');
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    public function getKarmaTextAttribute()
97
    {
98
        $karma = $this->karma->count();
0 ignored issues
show
Bug introduced by
The method count cannot be called on $this->karma (of type array<integer,object<Domains\Karma>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
99
100
        return ($karma > 0 ? '+' : '') . $karma;
101
    }
102
103
    /**
104
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
105
     */
106
    public function thanks()
107
    {
108
        return $this->hasMany(Karma::class, 'user_id', 'id');
109
    }
110
111
    /**
112
     * @return string
113
     */
114
    public function getThanksTextAttribute()
115
    {
116
        return $this->thanks->count();
0 ignored issues
show
Bug introduced by
The method count cannot be called on $this->thanks (of type array<integer,object<Domains\Karma>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
117
    }
118
119
    /**
120
     * @param $roomId
121
     * @return Carbon
122
     */
123
    public function getLastKarmaTimeForRoom($roomId)
124
    {
125
        $result = Karma::query()
126
            ->where('user_target_id', $this->id)
127
            ->where('room_id', $roomId)
128
            ->orderBy('created_at', 'desc')
129
            ->first();
130
131
        if ($result) {
132
            return $result->created_at;
133
        }
134
135
        return Carbon::createFromTimestamp(0);
136
    }
137
138
    /**
139
     * @param User $user
140
     * @param Message $message
141
     * @return static
142
     */
143
    public function addKarmaTo(User $user, Message $message)
144
    {
145
        return Karma::create([
146
            'room_id'        => \App::make(Room::class)->id,
147
            'message_id'     => $message->gitter_id,
148
            'user_id'        => $this->id,
149
            'user_target_id' => $user->id,
150
            'created_at'     => $message->created_at,
151
        ]);
152
    }
153
}
154