Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
57.14% covered (warning)
57.14%
8 / 14
57.14% covered (warning)
57.14%
4 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
FeatureRepository
57.14% covered (warning)
57.14%
8 / 14
57.14% covered (warning)
57.14%
4 / 7
15.38
0.00% covered (danger)
0.00%
0 / 1
 getAll
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getAllActive
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 findById
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 findByKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 create
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 update
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 delete
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace App\Http\Repositories;
4
5use App\Http\Models\Feature;
6use App\Http\Repositories\interfaces\IFeatureRepository;
7use Illuminate\Support\Collection;
8
9/**
10 * Repository for feature data access operations.
11 *
12 * This repository handles all database queries related to features,
13 * keeping data access logic separate from business logic.
14 */
15class FeatureRepository implements IFeatureRepository
16{
17    /**
18     * Get all features with optional category filtering.
19     *
20     * @param  string|null  $category  Optional category to filter by
21     * @return Collection<int, Feature> Collection of features
22     */
23    public function getAll(?string $category = null): Collection
24    {
25        $query = Feature::query();
26
27        if ($category) {
28            $query->where('category', $category);
29        }
30
31        return $query->orderBy('display_order')->orderBy('name')->get();
32    }
33
34    /**
35     * Get all active features ordered by display order.
36     *
37     * @param  string|null  $category  Optional category to filter by
38     * @return Collection<int, Feature> Collection of active features
39     */
40    public function getAllActive(?string $category = null): Collection
41    {
42        $query = Feature::active();
43
44        if ($category) {
45            $query->where('category', $category);
46        }
47
48        return $query->orderBy('display_order')->orderBy('name')->get();
49    }
50
51    /**
52     * Find a feature by its ID.
53     *
54     * @param  string  $id  The feature ID
55     * @return Feature|null The feature or null if not found
56     */
57    public function findById(string $id): ?Feature
58    {
59        return Feature::find($id);
60    }
61
62    /**
63     * Find a feature by its key.
64     *
65     * @param  string  $key  The feature key
66     * @return Feature|null The feature or null if not found
67     */
68    public function findByKey(string $key): ?Feature
69    {
70        return Feature::where('key', $key)->first();
71    }
72
73    /**
74     * Create a new feature.
75     *
76     * @param  array{
77     *     key: string,
78     *     name: string,
79     *     description?: string|null,
80     *     value_type: string,
81     *     category: string,
82     *     default_value?: mixed,
83     *     is_active?: bool,
84     *     display_order?: int
85     * }  $data  The feature data
86     * @return Feature The created feature
87     */
88    public function create(array $data): Feature
89    {
90        return Feature::create($data);
91    }
92
93    /**
94     * Update an existing feature.
95     *
96     * @param  Feature  $feature  The feature to update
97     * @param  array{
98     *     key?: string,
99     *     name?: string,
100     *     description?: string|null,
101     *     value_type?: string,
102     *     category?: string,
103     *     default_value?: mixed,
104     *     is_active?: bool,
105     *     display_order?: int
106     * }  $data  The update data
107     * @return Feature The updated feature
108     */
109    public function update(Feature $feature, array $data): Feature
110    {
111        $feature->update($data);
112
113        return $feature->fresh();
114    }
115
116    /**
117     * Delete a feature.
118     *
119     * @param  Feature  $feature  The feature to delete
120     * @return bool True if deleted successfully
121     */
122    public function delete(Feature $feature): bool
123    {
124        return $feature->delete();
125    }
126}