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