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