Completed
Push — master ( 43f0d4...1b2d9c )
by Ariel
13:00
created

Business::humanresources()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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\Addressbook;
9
use Timegridio\Concierge\Models\Humanresource;
10
use Timegridio\Concierge\Presenters\BusinessPresenter;
11
use Timegridio\Concierge\Traits\IsIntoDomain;
12
use Timegridio\Concierge\Traits\Preferenceable;
13
14
class Business extends EloquentModel implements HasPresenter
15
{
16
    use SoftDeletes, Preferenceable, IsIntoDomain;
17
18
    /**
19
     * The attributes that are mass assignable.
20
     *
21
     * @var array
22
     */
23
    protected $fillable = ['name', 'description', 'timezone', 'postal_address', 'phone', 'social_facebook', 'strategy',
24
        'plan', 'country_code', 'locale', ];
25
26
    /**
27
     * The attributes that should be mutated to dates.
28
     *
29
     * @var array
30
     */
31
    protected $dates = ['deleted_at'];
32
33
    /**
34
     * Define model events.
35
     *
36
     * @return void
37
     */
38 97
    public static function boot()
39
    {
40 97
        parent::boot();
41
42 97
        static::creating(function($business) {
43
44 95
            $business->slug = $business->makeSlug($business->name);
45
46 97
        });
47 97
    }
48
49
    /**
50
     * Make Slug.
51
     *
52
     * @param  string $name
53
     *
54
     * @return string
55
     */
56 95
    protected function makeSlug($name)
57
    {
58 95
        return str_slug($name);
59
    }
60
61
    ///////////////////
62
    // Relationships //
63
    ///////////////////
64
65
    /**
66
     * Belongs to a Category.
67
     *
68
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
69
     */
70 2
    public function category()
71
    {
72 2
        return $this->belongsTo(Category::class);
73
    }
74
75
    /**
76
     * Has a Contact addressbook.
77
     *
78
     * @return Illuminate\Database\Eloquent\Relations\BelongsToMany
79
     */
80 10
    public function addressbook()
81
    {
82 10
        return new Addressbook($this);
83
    }
84
85
    /**
86
     * Has a Contact addressbook.
87
     *
88
     * @return Illuminate\Database\Eloquent\Relations\BelongsToMany
89
     */
90 12
    public function contacts()
91
    {
92 12
        return $this->belongsToMany(Contact::class)
93 12
                    ->with('user')
94 12
                    ->withPivot('notes')
95 12
                    ->withTimestamps();
96
    }
97
98
    /**
99
     * Provides a catalog of Services.
100
     *
101
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
102
     */
103 27
    public function services()
104
    {
105 27
        return $this->hasMany(Service::class);
106
    }
107
108
    /**
109
     * Provides Services of Types.
110
     *
111
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
112
     */
113 1
    public function servicetypes()
114
    {
115 1
        return $this->hasMany(ServiceType::class);
116
    }
117
118
    /**
119
     * Publishes Vacancies.
120
     *
121
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
122
     */
123 42
    public function vacancies()
124
    {
125 42
        return $this->hasMany(Vacancy::class);
126
    }
127
128
    /**
129
     * Has many human resources.
130
     *
131
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
132
     */
133 2
    public function humanresources()
134
    {
135 2
        return $this->hasMany(Humanresource::class);
136
    }
137
138
    /**
139
     * Holds booked Appointments.
140
     *
141
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
142
     */
143 18
    public function bookings()
144
    {
145 18
        return $this->hasMany(Appointment::class);
146
    }
147
148
    /**
149
     * Is owned by Users.
150
     *
151
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
152
     */
153 32
    public function owners()
154
    {
155 32
        return $this->belongsToMany(config('auth.providers.users.model'))->withTimestamps();
156
    }
157
158
    /**
159
     * Belongs to a User.
160
     *
161
     * @return User
162
     */
163 1
    public function owner()
164
    {
165 1
        return $this->owners()->first();
166
    }
167
168
    /**
169
     * Get the real Users subscriptions count.
170
     *
171
     * @return Illuminate\Database\Query Relationship
172
     */
173
    public function subscriptionsCount()
174
    {
175
        return $this->belongsToMany(Contact::class)
176
                    ->selectRaw('id, count(*) as aggregate')
177
                    ->whereNotNull('user_id')
178
                    ->groupBy('business_id');
179
    }
180
181
    /**
182
     * get SubscriptionsCount Attribute.
183
     *
184
     * @return int Count of Contacts with real User held by this Business
185
     */
186 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...
187
    {
188
        // if relation is not loaded already, let's do it first
189
        if (!array_key_exists('subscriptionsCount', $this->relations)) {
190
            $this->load('subscriptionsCount');
191
        }
192
193
        $related = $this->getRelation('subscriptionsCount');
194
195
        // then return the count directly
196
        return ($related->count() > 0) ? (int) $related->first()->aggregate : 0;
197
    }
198
199
    ///////////////
200
    // Overrides //
201
    ///////////////
202
203
    //
204
205
    ///////////////
206
    // Presenter //
207
    ///////////////
208
209
    /**
210
     * Get presenter.
211
     *
212
     * @return BusinessPresenter Presenter class
213
     */
214 2
    public function getPresenterClass()
215
    {
216 2
        return BusinessPresenter::class;
217
    }
218
219
    ///////////////
220
    // Accessors //
221
    ///////////////
222
223
    /**
224
     * get route key.
225
     *
226
     * @return string Model slug
227
     */
228
    public function getRouteKey()
229
    {
230
        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...
231
    }
232
233
    //////////////
234
    // Mutators //
235
    //////////////
236
237
    /**
238
     * Set Slug.
239
     *
240
     * @return string Generated slug
241
     */
242 96
    public function setSlugAttribute()
243
    {
244 96
        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...
245
    }
246
247
    /**
248
     * Set name of the business.
249
     *
250
     * @param string $name Name of business
251
     */
252 95
    public function setNameAttribute($name)
253
    {
254 95
        $this->attributes['name'] = trim($name);
255 95
        $this->setSlugAttribute();
256 95
    }
257
258
    /**
259
     * Set Phone.
260
     *
261
     * Expected phone number is international format numeric only
262
     *
263
     * @param string $phone Phone number
264
     */
265 96
    public function setPhoneAttribute($phone)
266
    {
267 96
        $this->attributes['phone'] = trim($phone) ?: null;
268 96
    }
269
270
    /**
271
     * Set Postal Address.
272
     *
273
     * @param string $postalAddress Postal address
274
     */
275 96
    public function setPostalAddressAttribute($postalAddress)
276
    {
277 96
        $this->attributes['postal_address'] = trim($postalAddress) ?: null;
278 96
    }
279
280
    /**
281
     * Set Social Facebook.
282
     *
283
     */
284 95
    public function setSocialFacebookAttribute($facebookPageUrl)
285
    {
286 95
        $this->attributes['social_facebook'] = trim($facebookPageUrl) ?: null;
287 95
    }
288
}
289