InteractionService   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Test Coverage

Coverage 95%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 38
c 1
b 0
f 0
dl 0
loc 90
ccs 38
cts 40
cp 0.95
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A batchLike() 0 17 2
A increasePlayCount() 0 12 2
A toggleLike() 0 10 1
A __construct() 0 3 1
A batchUnlike() 0 12 1
1
<?php
2
3
namespace App\Services;
4
5
use App\Events\SongLikeToggled;
6
use App\Models\Interaction;
7
use App\Models\User;
8
9
class InteractionService
10
{
11
    private $interaction;
12
13 7
    public function __construct(Interaction $interaction)
14
    {
15 7
        $this->interaction = $interaction;
16 7
    }
17
18
    /**
19
     * Increase the number of times a song is played by a user.
20
     *
21
     * @return Interaction The affected Interaction object
22
     */
23 2
    public function increasePlayCount(string $songId, User $user): Interaction
24
    {
25 2
        return tap($this->interaction->firstOrCreate([
26 2
            'song_id' => $songId,
27 2
            'user_id' => $user->id,
28
        ]), static function (Interaction $interaction): void {
29 2
            if (!$interaction->exists) {
30
                $interaction->liked = false;
31
            }
32
33 2
            $interaction->play_count++;
34 2
            $interaction->save();
35 2
        });
36
    }
37
38
    /**
39
     * Like or unlike a song on behalf of a user.
40
     *
41
     * @return Interaction The affected Interaction object.
42
     */
43 2
    public function toggleLike(string $songId, User $user): Interaction
44
    {
45 2
        return tap($this->interaction->firstOrCreate([
46 2
            'song_id' => $songId,
47 2
            'user_id' => $user->id,
48
        ]), static function (Interaction $interaction): void {
49 2
            $interaction->liked = !$interaction->liked;
50 2
            $interaction->save();
51
52 2
            event(new SongLikeToggled($interaction));
53 2
        });
54
    }
55
56
    /**
57
     * Like several songs at once as a user.
58
     *
59
     * @param string[] $songIds
60
     *
61
     * @return Interaction[] The array of Interaction objects.
62
     */
63 2
    public function batchLike(array $songIds, User $user): array
64
    {
65
        return collect($songIds)->map(function ($songId) use ($user): Interaction {
66 2
            return tap($this->interaction->firstOrCreate([
67 2
                'song_id' => $songId,
68 2
                'user_id' => $user->id,
69
            ]), static function (Interaction $interaction): void {
70 2
                if (!$interaction->exists) {
71
                    $interaction->play_count = 0;
72
                }
73
74 2
                $interaction->liked = true;
75 2
                $interaction->save();
76
77 2
                event(new SongLikeToggled($interaction));
78 2
            });
79 2
        })->all();
80
    }
81
82
    /**
83
     * Unlike several songs at once.
84
     *
85
     * @param string[] $songIds
86
     */
87 2
    public function batchUnlike(array $songIds, User $user): void
88
    {
89 2
        $this->interaction
90 2
            ->whereIn('song_id', $songIds)
91 2
            ->where('user_id', $user->id)
92 2
            ->get()
93 2
            ->each(
94
                static function (Interaction $interaction): void {
95 2
                    $interaction->liked = false;
96 2
                    $interaction->save();
97
98 2
                    event(new SongLikeToggled($interaction));
99 2
                }
100
            );
101 2
    }
102
}
103