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