Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
RolePlayProjectsRepository
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
3 / 3
4
100.00% covered (success)
100.00%
1 / 1
 listForUser
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
 countForUser
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 cloneIdsOf
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace App\Http\Repositories;
4
5use App\Http\Models\Auth\User;
6use App\Http\Models\RolePlayProjects;
7use App\Http\Repositories\interfaces\IRolePlayProjectsRepository;
8use Carbon\Carbon;
9use Illuminate\Database\Eloquent\Collection;
10
11/**
12 * Eloquent-backed implementation of {@see IRolePlayProjectsRepository}.
13 */
14class RolePlayProjectsRepository implements IRolePlayProjectsRepository
15{
16    /**
17     * {@inheritDoc}
18     */
19    public function listForUser(User $user, array $filters = []): Collection
20    {
21        return RolePlayProjects::where('user_id', $user->id)
22            ->when(! empty($filters['type']), function ($query) use ($filters) {
23                return $query->where('type', $filters['type']);
24            })
25            ->when(! empty($filters['last_practiced_days']), function ($query) use ($filters) {
26                return $query->whereHas('conversations', function ($q) use ($filters) {
27                    $q->where('created_at', '<=', Carbon::now()->subDays((int) $filters['last_practiced_days'])->startOfDay());
28                });
29            })
30            ->orderBy('created_at', 'desc')
31            ->get();
32    }
33
34    /**
35     * {@inheritDoc}
36     */
37    public function countForUser(User $user): int
38    {
39        return RolePlayProjects::where('user_id', $user->id)->count();
40    }
41
42    /**
43     * {@inheritDoc}
44     */
45    public function cloneIdsOf(string $companyProjectId, ?string $userId = null): array
46    {
47        $query = RolePlayProjects::where('company_project_id', $companyProjectId);
48
49        if ($userId !== null) {
50            $query->where('user_id', $userId);
51        }
52
53        // Moloquent's `_id` is an ObjectID which stringifies to an empty
54        // string when plucked directly. Use the accessor (`id`) so the hex
55        // representation survives the serialize.
56        return $query->pluck('id')->map(fn ($id) => (string) $id)->all();
57    }
58}