Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
63.64% |
14 / 22 |
|
37.50% |
3 / 8 |
CRAP | |
0.00% |
0 / 1 |
| Category | |
63.64% |
14 / 22 |
|
37.50% |
3 / 8 |
16.82 | |
0.00% |
0 / 1 |
| auditExcept | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| user | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| parent | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| children | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| products | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| boot | |
84.62% |
11 / 13 |
|
0.00% |
0 / 1 |
3.03 | |||
| getAll | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| findById | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Models; |
| 4 | |
| 5 | use App\Models\admin\Product; |
| 6 | use App\Service\CacheService; |
| 7 | use App\Support\Auditable; |
| 8 | use Illuminate\Database\Eloquent\Factories\HasFactory; |
| 9 | use Illuminate\Database\Eloquent\Model; |
| 10 | |
| 11 | class Category extends Model |
| 12 | { |
| 13 | use Auditable, HasFactory; |
| 14 | |
| 15 | protected function auditExcept(): array |
| 16 | { |
| 17 | return ['slug', 'thumbnail']; |
| 18 | } |
| 19 | |
| 20 | protected $table = 'categories'; |
| 21 | |
| 22 | protected $fillable = [ |
| 23 | 'name', |
| 24 | 'slug', |
| 25 | 'thumbnail', |
| 26 | 'parent_id', |
| 27 | 'description', |
| 28 | 'isActive', |
| 29 | 'created_by', |
| 30 | 'tax_rate', |
| 31 | ]; |
| 32 | |
| 33 | protected $casts = [ |
| 34 | 'tax_rate' => 'decimal:2', |
| 35 | ]; |
| 36 | |
| 37 | public function user() |
| 38 | { |
| 39 | return $this->belongsTo(User::class, 'created_by'); |
| 40 | } |
| 41 | |
| 42 | public function parent() |
| 43 | { |
| 44 | return $this->belongsTo(Category::class, 'parent_id'); |
| 45 | } |
| 46 | |
| 47 | public function children() |
| 48 | { |
| 49 | return $this->hasMany(Category::class, 'parent_id'); |
| 50 | } |
| 51 | |
| 52 | public function products() |
| 53 | { |
| 54 | return $this->belongsToMany(Product::class, 'category_product', 'category_id', 'product_id'); |
| 55 | } |
| 56 | /** |
| 57 | * ########################################### |
| 58 | * # Repository Methods Start # |
| 59 | * ########################################### |
| 60 | * */ |
| 61 | |
| 62 | /** |
| 63 | * Automatic created_by value assigned from logged in user |
| 64 | * */ |
| 65 | protected static function boot() |
| 66 | { |
| 67 | parent::boot(); |
| 68 | |
| 69 | static::creating(function ($query) { |
| 70 | $query->created_by = auth()->id(); |
| 71 | }); |
| 72 | |
| 73 | // CACHING: the storefront caches the nav menu, the flat category |
| 74 | // list, and each category's product listing page (see |
| 75 | // app/Service/CacheService.php). Bust all of that the instant a |
| 76 | // category is created, updated, or deleted — same reasoning as |
| 77 | // Product::boot() (see that file for the getOriginal() note: inside |
| 78 | // saved(), getOriginal('parent_id') still returns the PRE-update |
| 79 | // parent, since Eloquent doesn't call syncOriginal() until after the |
| 80 | // 'saved' event fires — so a category moved under a different |
| 81 | // parent correctly busts both the old and new parent's listing). |
| 82 | static::saved(function ($category) { |
| 83 | CacheService::forgetCategory($category->id, $category->parent_id); |
| 84 | |
| 85 | $previousParentId = $category->getOriginal('parent_id'); |
| 86 | if ($previousParentId && $previousParentId != $category->parent_id) { |
| 87 | CacheService::forgetCategory(null, $previousParentId); |
| 88 | } |
| 89 | }); |
| 90 | |
| 91 | static::deleted(function ($category) { |
| 92 | CacheService::forgetCategory($category->id, $category->parent_id); |
| 93 | }); |
| 94 | } |
| 95 | |
| 96 | public static function getAll($is_pluck = false, $order_by_col_name = 'id') |
| 97 | { |
| 98 | $query = self::query(); |
| 99 | $query->with(['user', 'parent'])->latest($order_by_col_name); |
| 100 | |
| 101 | return $is_pluck ? $query->pluck('name', 'id') : $query->cursor(); |
| 102 | } |
| 103 | |
| 104 | public static function findById($id) |
| 105 | { |
| 106 | return self::query()->findOrFail($id); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * ########################################### |
| 111 | * # Repository Methods END # |
| 112 | * ########################################### |
| 113 | * */ |
| 114 | } |