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