Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
19 / 19 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| RolePlayPersonaTemplateRepository | |
100.00% |
19 / 19 |
|
100.00% |
5 / 5 |
6 | |
100.00% |
1 / 1 |
| listVisibleTo | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
2 | |||
| find | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| create | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| update | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| delete | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Repositories; |
| 4 | |
| 5 | use App\Http\Models\RolePlayPersonaTemplate; |
| 6 | use Illuminate\Support\Collection; |
| 7 | |
| 8 | /** |
| 9 | * Data access for {@see RolePlayPersonaTemplate}. |
| 10 | * |
| 11 | * Encapsulates the visibility scoping rules so the controller and service |
| 12 | * never have to know whether a template is "personal" or "company". |
| 13 | */ |
| 14 | class RolePlayPersonaTemplateRepository |
| 15 | { |
| 16 | /** |
| 17 | * List templates visible to the supplied user. |
| 18 | * |
| 19 | * Returns a merged set of: |
| 20 | * - the user's own personal templates, plus |
| 21 | * - all company-visibility templates belonging to the user's company. |
| 22 | * |
| 23 | * @return Collection<int, RolePlayPersonaTemplate> |
| 24 | */ |
| 25 | public function listVisibleTo(string $userId, ?string $companyId): Collection |
| 26 | { |
| 27 | return RolePlayPersonaTemplate::query() |
| 28 | ->where(function ($q) use ($userId, $companyId) { |
| 29 | $q->where(function ($p) use ($userId) { |
| 30 | $p->where('visibility', RolePlayPersonaTemplate::VISIBILITY_PERSONAL) |
| 31 | ->where('user_id', $userId); |
| 32 | }); |
| 33 | |
| 34 | if ($companyId) { |
| 35 | $q->orWhere(function ($p) use ($companyId) { |
| 36 | $p->where('visibility', RolePlayPersonaTemplate::VISIBILITY_COMPANY) |
| 37 | ->where('company_id', $companyId); |
| 38 | }); |
| 39 | } |
| 40 | }) |
| 41 | ->orderBy('created_at', 'desc') |
| 42 | ->get(); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Find a template by id, returning null if it does not exist. |
| 47 | */ |
| 48 | public function find(string $id): ?RolePlayPersonaTemplate |
| 49 | { |
| 50 | return RolePlayPersonaTemplate::find($id); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Create a new template row. |
| 55 | * |
| 56 | * @param array<string, mixed> $attributes |
| 57 | */ |
| 58 | public function create(array $attributes): RolePlayPersonaTemplate |
| 59 | { |
| 60 | return RolePlayPersonaTemplate::create($attributes); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Update an existing template. |
| 65 | * |
| 66 | * @param array<string, mixed> $attributes |
| 67 | */ |
| 68 | public function update(RolePlayPersonaTemplate $template, array $attributes): RolePlayPersonaTemplate |
| 69 | { |
| 70 | $template->update($attributes); |
| 71 | |
| 72 | return $template->fresh(); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Delete a template. |
| 77 | */ |
| 78 | public function delete(RolePlayPersonaTemplate $template): void |
| 79 | { |
| 80 | $template->delete(); |
| 81 | } |
| 82 | } |