Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
69.23% |
9 / 13 |
|
42.86% |
3 / 7 |
CRAP | |
0.00% |
0 / 1 |
| BrandDefinition | |
69.23% |
9 / 13 |
|
42.86% |
3 / 7 |
9.86 | |
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 | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| 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\Brand; |
| 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 BrandDefinition extends Definition |
| 12 | { |
| 13 | public function label(): string |
| 14 | { |
| 15 | return 'Brands'; |
| 16 | } |
| 17 | |
| 18 | public function model(): string |
| 19 | { |
| 20 | return Brand::class; |
| 21 | } |
| 22 | |
| 23 | public function columns(): array |
| 24 | { |
| 25 | return [ |
| 26 | ['key' => 'name', 'label' => 'Name', 'required' => true, 'rules' => 'string|max:255', 'sample' => 'Siemens'], |
| 27 | ['key' => 'slug', 'label' => 'Slug', 'rules' => 'nullable|string|max:255', 'sample' => 'siemens', |
| 28 | 'import' => fn ($v, $row) => $v ?: Str::slug($row['name'] ?? '')], |
| 29 | ['key' => 'description', 'label' => 'Description', 'rules' => 'nullable|string|max:1000', 'sample' => ''], |
| 30 | ]; |
| 31 | } |
| 32 | |
| 33 | public function query(): Builder |
| 34 | { |
| 35 | return Brand::query()->orderByDesc('id'); |
| 36 | } |
| 37 | |
| 38 | public function applyFilters(Builder $query, array $filters): Builder |
| 39 | { |
| 40 | $s = trim((string) ($filters['search'] ?? '')); |
| 41 | |
| 42 | return $query->when($s !== '', fn ($q) => $q->where('name', 'like', "%{$s}%")); |
| 43 | } |
| 44 | |
| 45 | public function uniqueBy(): array |
| 46 | { |
| 47 | return ['slug']; |
| 48 | } |
| 49 | |
| 50 | public function defaults(): array |
| 51 | { |
| 52 | return ['created_by' => Auth::id()]; |
| 53 | } |
| 54 | } |