Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
40.00% |
4 / 10 |
CRAP | |
71.43% |
20 / 28 |
| Brand | |
0.00% |
0 / 1 |
|
40.00% |
4 / 10 |
12.33 | |
71.43% |
20 / 28 |
| __construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
10 / 10 |
|||
| getCacheBaseTags | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| booted | |
0.00% |
0 / 1 |
1.03 | |
70.00% |
7 / 10 |
|||
| services | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| questions | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| customerInfos | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| servicesStats | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| singleOptins | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| getAccountsAttribute | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| getTotalOptinsAttribute | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Models; |
| 4 | |
| 5 | use Illuminate\Database\Eloquent\Factories\HasFactory; |
| 6 | use Illuminate\Database\Eloquent\Model; |
| 7 | use Illuminate\Support\Str; |
| 8 | use Kwaadpepper\CrudPolicies\Enums\CrudAction; |
| 9 | use Kwaadpepper\CrudPolicies\Enums\CrudType; |
| 10 | use Kwaadpepper\CrudPolicies\Traits\IsCrudModel; |
| 11 | use Rennokki\QueryCache\Traits\QueryCacheable; |
| 12 | |
| 13 | class Brand extends Model |
| 14 | { |
| 15 | use IsCrudModel; |
| 16 | use HasFactory; |
| 17 | use QueryCacheable; |
| 18 | |
| 19 | /** @var string */ |
| 20 | protected $crudLabelColumn = 'name'; |
| 21 | |
| 22 | /** @var string */ |
| 23 | protected $crudValueColumn = 'id'; |
| 24 | |
| 25 | /** |
| 26 | * The timeout cache for this model |
| 27 | * |
| 28 | * @var integer |
| 29 | */ |
| 30 | protected $cacheFor = 0; |
| 31 | |
| 32 | /** |
| 33 | * The attributes that are mass assignable. |
| 34 | * |
| 35 | * @var string[] |
| 36 | */ |
| 37 | protected $fillable = [ |
| 38 | 'name' |
| 39 | ]; |
| 40 | |
| 41 | /** |
| 42 | * Brand |
| 43 | * |
| 44 | * @param array $attributes |
| 45 | */ |
| 46 | public function __construct(array $attributes = []) |
| 47 | { |
| 48 | $this->cacheFor = config('api.servicesDBCache') * 60; |
| 49 | |
| 50 | self::$editableProperties = [ |
| 51 | 'name' => [ |
| 52 | 'label' => Str::ucfirst(trans('models.common.name')), |
| 53 | 'type' => CrudType::string(), |
| 54 | 'validate' => [ |
| 55 | 'min:5', |
| 56 | 'max:255' |
| 57 | ], |
| 58 | 'actions' => [ |
| 59 | CrudAction::viewAny(), |
| 60 | CrudAction::view(), |
| 61 | CrudAction::create(), |
| 62 | CrudAction::update() |
| 63 | ] |
| 64 | ] |
| 65 | ]; |
| 66 | parent::__construct($attributes); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Set the base cache tags that will be present |
| 71 | * on all queries. |
| 72 | * |
| 73 | * ! This is used to flush caching |
| 74 | * |
| 75 | * @return array |
| 76 | */ |
| 77 | protected function getCacheBaseTags(): array |
| 78 | { |
| 79 | return [ |
| 80 | 'crm_brands', |
| 81 | ]; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Perform any actions required after the model boots. |
| 86 | * |
| 87 | * @return void |
| 88 | */ |
| 89 | protected static function booted() |
| 90 | { |
| 91 | static::created(function () { |
| 92 | // * Flush Eloquent cache. |
| 93 | self::flushQueryCache(); |
| 94 | }); |
| 95 | static::updated(function () { |
| 96 | // * Flush Eloquent cache. |
| 97 | self::flushQueryCache(); |
| 98 | }); |
| 99 | static::deleted(function () { |
| 100 | // * Flush Eloquent cache. |
| 101 | self::flushQueryCache(); |
| 102 | }); |
| 103 | } |
| 104 | |
| 105 | // * RELATIONS |
| 106 | |
| 107 | /** |
| 108 | * Service hasMany relation |
| 109 | * |
| 110 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
| 111 | */ |
| 112 | public function services() |
| 113 | { |
| 114 | return $this->hasMany(Service::class); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Question hasMany relation |
| 119 | * |
| 120 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
| 121 | */ |
| 122 | public function questions() |
| 123 | { |
| 124 | return $this->hasMany(Question::class); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * CustomerInfo hasMany relation |
| 129 | * |
| 130 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
| 131 | */ |
| 132 | public function customerInfos() |
| 133 | { |
| 134 | return $this->hasMany(CustomerInfo::class); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * ServiceStat hasMany relation |
| 139 | * |
| 140 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
| 141 | */ |
| 142 | public function servicesStats() |
| 143 | { |
| 144 | return $this->hasMany(ServiceStat::class); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * SingleOptin hasMany relation |
| 149 | * |
| 150 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
| 151 | */ |
| 152 | public function singleOptins() |
| 153 | { |
| 154 | return $this->hasMany(SingleOptin::class); |
| 155 | } |
| 156 | |
| 157 | // * METHODS |
| 158 | |
| 159 | /** |
| 160 | * Account attribute accessor |
| 161 | * |
| 162 | * @return integer |
| 163 | */ |
| 164 | public function getAccountsAttribute(): int |
| 165 | { |
| 166 | return $this->customerInfos()->count(); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Total optins attribute accessor |
| 171 | * |
| 172 | * @return integer |
| 173 | */ |
| 174 | public function getTotalOptinsAttribute(): int |
| 175 | { |
| 176 | return $this->customerInfos()->where('opt_in', 1)->count() + $this->singleOptins()->count(); |
| 177 | } |
| 178 | } |