InteractionRepository   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
eloc 19
c 2
b 0
f 0
dl 0
loc 42
ccs 0
cts 18
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getRecentlyPlayed() 0 16 2
A getUserFavorites() 0 9 1
A getModelClass() 0 3 1
1
<?php
2
3
namespace App\Repositories;
4
5
use App\Models\Interaction;
6
use App\Models\User;
7
use App\Repositories\Traits\ByCurrentUser;
8
use Illuminate\Database\Query\Builder;
9
use Illuminate\Support\Collection;
10
11
class InteractionRepository extends AbstractRepository
12
{
13
    use ByCurrentUser;
14
15
    public function getModelClass(): string
16
    {
17
        return Interaction::class;
18
    }
19
20
    /**
21
     * Get all songs favorited by a user.
22
     */
23
    public function getUserFavorites(User $user): Collection
24
    {
25
        return $this->model->where([
26
            'user_id' => $user->id,
27
            'liked' => true,
28
        ])
29
            ->with('song')
30
            ->get()
31
            ->pluck('song');
32
    }
33
34
    /**
35
     * @return Interaction[]
36
     */
37
    public function getRecentlyPlayed(User $user, ?int $count = null): array
38
    {
39
        /** @var Builder $query */
40
        $query = $this->model
41
            ->where('user_id', $user->id)
42
            ->where('play_count', '>', 0)
43
            ->orderBy('updated_at', 'DESC');
44
45
        if ($count) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $count of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
46
            $query = $query->take($count);
47
        }
48
49
        return $query
50
            ->get()
51
            ->pluck('song_id')
52
            ->all();
53
    }
54
}
55