ReleaseComment   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 44
dl 0
loc 130
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getCommentsForUserRange() 0 9 1
A getCommentsRange() 0 8 1
A getCommentCountForUser() 0 5 1
A addComment() 0 25 3
A getCommentCount() 0 3 1
A getComments() 0 3 1
A release() 0 3 1
A updateReleaseCommentCount() 0 4 1
A getCommentById() 0 3 1
A user() 0 3 1
A deleteComment() 0 6 2
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Relations\BelongsTo;
8
9
/**
10
 * App\Models\ReleaseComment.
11
 *
12
 * @property int $id
13
 * @property int $releases_id FK to releases.id
14
 * @property string $text
15
 * @property bool $isvisible
16
 * @property bool $issynced
17
 * @property string|null $gid
18
 * @property string|null $cid
19
 * @property string $text_hash
20
 * @property string $username
21
 * @property int $users_id
22
 * @property \Carbon\Carbon|null $created_at
23
 * @property \Carbon\Carbon|null $updated_at
24
 * @property string|null $host
25
 * @property bool $shared
26
 * @property string $shareid
27
 * @property string $siteid
28
 * @property int|null $sourceid
29
 * @property mixed $nzb_guid
30
 * @property-read Release $release
31
 * @property-read User $user
32
 *
33
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseComment whereCid($value)
34
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseComment whereCreatedAt($value)
35
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseComment whereGid($value)
36
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseComment whereHost($value)
37
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseComment whereId($value)
38
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseComment whereIssynced($value)
39
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseComment whereIsvisible($value)
40
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseComment whereNzbGuid($value)
41
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseComment whereReleasesId($value)
42
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseComment whereShared($value)
43
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseComment whereShareid($value)
44
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseComment whereSiteid($value)
45
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseComment whereSourceid($value)
46
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseComment whereText($value)
47
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseComment whereTextHash($value)
48
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseComment whereUpdatedAt($value)
49
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseComment whereUsername($value)
50
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseComment whereUsersId($value)
51
 *
52
 * @mixin \Eloquent
53
 *
54
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseComment newModelQuery()
55
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseComment newQuery()
56
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseComment query()
57
 */
58
class ReleaseComment extends Model
59
{
60
    /**
61
     * @var array
62
     */
63
    protected $guarded = [];
64
65
    /**
66
     * @var bool
67
     */
68
    protected $dateFormat = false;
69
70
    public function release(): BelongsTo
71
    {
72
        return $this->belongsTo(Release::class, 'releases_id');
73
    }
74
75
    public function user(): BelongsTo
76
    {
77
        return $this->belongsTo(User::class, 'users_id');
78
    }
79
80
    /**
81
     * Get a comment by id.
82
     *
83
     *
84
     * @return Model|null|static
85
     */
86
    public static function getCommentById($id)
87
    {
88
        return self::query()->where('id', $id)->first();
89
    }
90
91
    public static function getComments($id): array
92
    {
93
        return self::query()->where('releases_id', $id)->orderByDesc('created_at')->get()->toArray();
94
    }
95
96
    public static function getCommentCount(): int
97
    {
98
        return self::query()->count(['id']);
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::query()->count(array('id')) could return the type Illuminate\Database\Eloquent\Builder which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
99
    }
100
101
    /**
102
     * Delete single comment on the site.
103
     */
104
    public static function deleteComment($id): void
105
    {
106
        $res = self::getCommentById($id);
107
        if ($res) {
108
            self::query()->where('id', $id)->delete();
109
            self::updateReleaseCommentCount($res['gid']);
110
        }
111
    }
112
113
    /**
114
     * Add a release_comments row.
115
     *
116
     *
117
     *
118
     * @throws \Exception
119
     */
120
    public static function addComment($id, $gid, $text, $userid, $host): int
121
    {
122
        if (config('nntmux:settings.store_user_ip') === false) {
123
            $host = '';
124
        }
125
126
        $username = User::query()->where('id', $userid)->first(['username']);
127
        $username = ($username === null ? 'ANON' : $username['username']);
128
129
        $comid = self::query()
130
            ->insertGetId(
131
                [
132
                    'releases_id' => $id,
133
                    'gid' => $gid,
134
                    'text' => $text,
135
                    'users_id' => $userid,
136
                    'created_at' => now(),
137
                    'updated_at' => now(),
138
                    'host' => $host,
139
                    'username' => $username,
140
                ]
141
            );
142
        self::updateReleaseCommentCount($id);
143
144
        return $comid;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $comid could return the type Illuminate\Database\Eloquent\Builder which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
145
    }
146
147
    /**
148
     * Get release_comments rows by limit.
149
     */
150
    public static function getCommentsRange(): LengthAwarePaginator
151
    {
152
        $range = self::query()
153
            ->select(['release_comments.*', 'releases.guid'])
154
            ->leftJoin('releases', 'releases.id', '=', 'release_comments.releases_id')
155
            ->orderByDesc('release_comments.created_at');
0 ignored issues
show
Bug introduced by
'release_comments.created_at' of type string is incompatible with the type Closure|Illuminate\Datab...\Database\Query\Builder expected by parameter $column of Illuminate\Database\Query\Builder::orderByDesc(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

155
            ->orderByDesc(/** @scrutinizer ignore-type */ 'release_comments.created_at');
Loading history...
156
157
        return $range->paginate(config('nntmux.items_per_page'));
158
    }
159
160
    /**
161
     * Update the denormalised count of comments for a release.
162
     */
163
    public static function updateReleaseCommentCount($gid): void
164
    {
165
        $commentCount = self::query()->where('gid', '=', 'releases.gid')->where('isvisible', '=', 1)->count(['id']);
166
        Release::query()->where('gid', $gid)->update(['comments' => $commentCount]);
167
    }
168
169
    /**
170
     * Get a count of all comments for a user.
171
     */
172
    public static function getCommentCountForUser($uid): int
173
    {
174
        $res = self::query()->where(['users_id' => $uid, 'isvisible' => 1])->count(['id']);
175
176
        return $res;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $res could return the type Illuminate\Database\Eloquent\Builder which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
177
    }
178
179
    public static function getCommentsForUserRange($uid): LengthAwarePaginator
180
    {
181
        return self::query()
182
            ->select(['release_comments.*', 'r.guid', 'r.searchname', 'u.username'])
183
            ->join('releases as r', 'r.id', '=', 'release_comments.releases_id')
184
            ->leftJoin('users as u', 'u.id', '=', 'release_comments.users_id')
185
            ->where('users_id', $uid)
186
            ->orderByDesc('created_at')
0 ignored issues
show
Bug introduced by
'created_at' of type string is incompatible with the type Closure|Illuminate\Datab...\Database\Query\Builder expected by parameter $column of Illuminate\Database\Query\Builder::orderByDesc(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

186
            ->orderByDesc(/** @scrutinizer ignore-type */ 'created_at')
Loading history...
187
            ->paginate(config('nntmux.items_per_page'));
188
    }
189
}
190