Completed
Push — master ( 23de65...877ae8 )
by Ariel
11:07
created

Business::subscriptionsCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 7
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Timegridio\Concierge\Models;
4
5
use Illuminate\Database\Eloquent\Model as EloquentModel;
6
use Illuminate\Database\Eloquent\SoftDeletes;
7
use McCool\LaravelAutoPresenter\HasPresenter;
8
use Timegridio\Concierge\Presenters\BusinessPresenter;
9
use Timegridio\Concierge\Traits\IsIntoDomain;
10
use Timegridio\Concierge\Traits\Preferenceable;
11
12
class Business extends EloquentModel implements HasPresenter
13
{
14
    use SoftDeletes, Preferenceable, IsIntoDomain;
15
16
    /**
17
     * The attributes that are mass assignable.
18
     *
19
     * @var array
20
     */
21
    protected $fillable = ['name', 'description', 'timezone', 'postal_address', 'phone', 'social_facebook', 'strategy',
22
        'plan', 'country_code', 'locale', ];
23
24
    /**
25
     * The attributes that should be mutated to dates.
26
     *
27
     * @var array
28
     */
29
    protected $dates = ['deleted_at'];
30
31
    /**
32
     * Define model events.
33
     *
34
     * @return void
35
     */
36 65
    public static function boot()
37
    {
38 65
        parent::boot();
39
40 65
        static::creating(function($business) {
41
42 63
            $business->slug = $business->makeSlug($business->name);
43
44 65
        });
45 65
    }
46
47
    /**
48
     * Make Slug.
49
     *
50
     * @param  string $name
51
     *
52
     * @return string
53
     */
54 63
    protected function makeSlug($name)
55
    {
56 63
        return str_slug($name);
57
    }
58
59
    ///////////////////
60
    // Relationships //
61
    ///////////////////
62
63
    /**
64
     * Belongs to a Category.
65
     *
66
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
67
     */
68 2
    public function category()
69
    {
70 2
        return $this->belongsTo(Category::class);
71
    }
72
73
    /**
74
     * Has a Contact addressbook.
75
     *
76
     * @return Illuminate\Database\Eloquent\Relations\BelongsToMany
77
     */
78 2
    public function contacts()
79
    {
80 2
        return $this->belongsToMany(Contact::class)
81 2
                    ->with('user')
82 2
                    ->withPivot('notes')
83 2
                    ->withTimestamps();
84
    }
85
86
    /**
87
     * Provides a catalog of Services.
88
     *
89
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
90
     */
91 10
    public function services()
92
    {
93 10
        return $this->hasMany(Service::class);
94
    }
95
96
    /**
97
     * Provides Services of Types.
98
     *
99
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
100
     */
101 1
    public function servicetypes()
102
    {
103 1
        return $this->hasMany(ServiceType::class);
104
    }
105
106
    /**
107
     * Publishes Vacancies.
108
     *
109
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
110
     */
111 26
    public function vacancies()
112
    {
113 26
        return $this->hasMany(Vacancy::class);
114
    }
115
116
    /**
117
     * Holds booked Appointments.
118
     *
119
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
120
     */
121 1
    public function bookings()
122
    {
123 1
        return $this->hasMany(Appointment::class);
124
    }
125
126
    /**
127
     * Is owned by Users.
128
     *
129
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
130
     */
131 15
    public function owners()
132
    {
133 15
        return $this->belongsToMany(config('auth.providers.users.model'))->withTimestamps();
134
    }
135
136
    /**
137
     * Belongs to a User.
138
     *
139
     * @return User
140
     */
141 1
    public function owner()
142
    {
143 1
        return $this->owners()->first();
144
    }
145
146
    /**
147
     * Get the real Users subscriptions count.
148
     *
149
     * @return Illuminate\Database\Query Relationship
150
     */
151
    public function subscriptionsCount()
152
    {
153
        return $this->belongsToMany(Contact::class)
154
                    ->selectRaw('id, count(*) as aggregate')
155
                    ->whereNotNull('user_id')
156
                    ->groupBy('business_id');
157
    }
158
159
    /**
160
     * get SubscriptionsCount Attribute.
161
     *
162
     * @return int Count of Contacts with real User held by this Business
163
     */
164 View Code Duplication
    public function getSubscriptionsCountAttribute()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
165
    {
166
        // if relation is not loaded already, let's do it first
167
        if (!array_key_exists('subscriptionsCount', $this->relations)) {
168
            $this->load('subscriptionsCount');
169
        }
170
171
        $related = $this->getRelation('subscriptionsCount');
172
173
        // then return the count directly
174
        return ($related->count() > 0) ? (int) $related->first()->aggregate : 0;
175
    }
176
177
    ///////////////
178
    // Overrides //
179
    ///////////////
180
181
    //
182
183
    ///////////////
184
    // Presenter //
185
    ///////////////
186
187
    /**
188
     * Get presenter.
189
     *
190
     * @return BusinessPresenter Presenter class
191
     */
192 2
    public function getPresenterClass()
193
    {
194 2
        return BusinessPresenter::class;
195
    }
196
197
    ///////////////
198
    // Accessors //
199
    ///////////////
200
201
    /**
202
     * get route key.
203
     *
204
     * @return string Model slug
205
     */
206
    public function getRouteKey()
207
    {
208
        return $this->slug;
0 ignored issues
show
Documentation introduced by
The property slug does not exist on object<Timegridio\Concierge\Models\Business>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
209
    }
210
211
    //////////////
212
    // Mutators //
213
    //////////////
214
215
    /**
216
     * Set Slug.
217
     *
218
     * @return string Generated slug
219
     */
220 64
    public function setSlugAttribute()
221
    {
222 64
        return $this->attributes['slug'] = str_slug($this->name);
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<Timegridio\Concierge\Models\Business>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
223
    }
224
225
    /**
226
     * Set name of the business.
227
     *
228
     * @param string $name Name of business
229
     */
230 63
    public function setNameAttribute($name)
231
    {
232 63
        $this->attributes['name'] = trim($name);
233 63
        $this->setSlugAttribute();
234 63
    }
235
236
    /**
237
     * Set Phone.
238
     *
239
     * Expected phone number is international format numeric only
240
     *
241
     * @param string $phone Phone number
242
     */
243 64
    public function setPhoneAttribute($phone)
244
    {
245 64
        $this->attributes['phone'] = trim($phone) ?: null;
246 64
    }
247
248
    /**
249
     * Set Postal Address.
250
     *
251
     * @param string $postalAddress Postal address
252
     */
253 64
    public function setPostalAddressAttribute($postalAddress)
254
    {
255 64
        $this->attributes['postal_address'] = trim($postalAddress) ?: null;
256 64
    }
257
258
    /**
259
     * Set Social Facebook.
260
     *
261
     */
262 63
    public function setSocialFacebookAttribute($facebookPageUrl)
263
    {
264 63
        $this->attributes['social_facebook'] = trim($facebookPageUrl) ?: null;
265 63
    }
266
}
267