Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
64.00% |
16 / 25 |
|
28.57% |
2 / 7 |
CRAP | |
0.00% |
0 / 1 |
| CategoryDefinition | |
64.00% |
16 / 25 |
|
28.57% |
2 / 7 |
16.65 | |
0.00% |
0 / 1 |
| label | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| model | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| columns | |
72.22% |
13 / 18 |
|
0.00% |
0 / 1 |
5.54 | |||
| query | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| applyFilters | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| uniqueBy | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| defaults | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Services\ImportExport\Definitions; |
| 4 | |
| 5 | use App\Models\Category; |
| 6 | use App\Services\ImportExport\Definition; |
| 7 | use Illuminate\Database\Eloquent\Builder; |
| 8 | use Illuminate\Support\Facades\Auth; |
| 9 | use Illuminate\Support\Str; |
| 10 | |
| 11 | class CategoryDefinition extends Definition |
| 12 | { |
| 13 | public function label(): string |
| 14 | { |
| 15 | return 'Categories'; |
| 16 | } |
| 17 | |
| 18 | public function model(): string |
| 19 | { |
| 20 | return Category::class; |
| 21 | } |
| 22 | |
| 23 | public function columns(): array |
| 24 | { |
| 25 | return [ |
| 26 | ['key' => 'name', 'label' => 'Name', 'required' => true, 'rules' => 'string|max:255', 'sample' => 'Circuit Breakers'], |
| 27 | ['key' => 'slug', 'label' => 'Slug', 'rules' => 'nullable|string|max:255', 'sample' => 'circuit-breakers', |
| 28 | 'import' => fn ($v, $row) => $v ?: Str::slug($row['name'] ?? '')], |
| 29 | ['key' => 'parent_id', 'label' => 'Parent category', 'rules' => 'nullable|string|max:255', |
| 30 | 'sample' => '', 'help' => 'Name or id of the parent', |
| 31 | 'export' => fn ($row) => optional($row->parent)->name, |
| 32 | 'import' => function ($v) { |
| 33 | if (! $v) { |
| 34 | return null; |
| 35 | } |
| 36 | |
| 37 | return is_numeric($v) |
| 38 | ? (Category::whereKey($v)->value('id')) |
| 39 | : (Category::where('name', $v)->value('id')); |
| 40 | }], |
| 41 | ['key' => 'isActive', 'label' => 'Active', 'sample' => '1', |
| 42 | 'import' => fn ($v) => in_array(strtolower((string) $v), ['1', 'yes', 'true', 'active'], true) ? 1 : 0], |
| 43 | ['key' => 'description', 'label' => 'Description', 'rules' => 'nullable|string|max:1000', 'sample' => ''], |
| 44 | ]; |
| 45 | } |
| 46 | |
| 47 | public function query(): Builder |
| 48 | { |
| 49 | return Category::query()->with('parent')->orderByDesc('id'); |
| 50 | } |
| 51 | |
| 52 | public function applyFilters(Builder $query, array $filters): Builder |
| 53 | { |
| 54 | $s = trim((string) ($filters['search'] ?? '')); |
| 55 | |
| 56 | return $query->when($s !== '', fn ($q) => $q->where('name', 'like', "%{$s}%")); |
| 57 | } |
| 58 | |
| 59 | public function uniqueBy(): array |
| 60 | { |
| 61 | return ['slug']; |
| 62 | } |
| 63 | |
| 64 | public function defaults(): array |
| 65 | { |
| 66 | return ['created_by' => Auth::id(), 'isActive' => 1]; |
| 67 | } |
| 68 | } |