Completed
Push — master ( 592d83...bfc36f )
by Ariel
13:33
created

Vacancy::getCapacityAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2.0625
1
<?php
2
3
namespace Timegridio\Concierge\Models;
4
5
use Carbon\Carbon;
6
use Illuminate\Database\Eloquent\Model as EloquentModel;
7
8
/**
9
 * @property int $id
10
 * @property int $business_id
11
 * @property int $service_id
12
 * @property int $humanresource_id
13
 * @property string $date
14
 * @property Carbon\Carbon $start_at
15
 * @property Carbon\Carbon $finish_at
16
 * @property int $capacity
17
 */
18
class Vacancy extends EloquentModel
19
{
20
    /**
21
     * The attributes that are mass assignable.
22
     *
23
     * @var array
24
     */
25
    protected $fillable = [
26
        'business_id',
27
        'service_id',
28
        'humanresource_id',
29
        'date',
30
        'start_at',
31
        'finish_at',
32
        'capacity',
33
    ];
34
35
    /**
36
     * The attributes that aren't mass assignable.
37
     *
38
     * @var array
39
     */
40
    protected $guarded = ['id'];
41
42
    /**
43
     * The attributes that should be mutated to dates.
44
     *
45
     * @var array
46
     */
47
    protected $dates = ['start_at', 'finish_at'];
48
49
    ///////////////////
50
    // Relationships //
51
    ///////////////////
52
53
    /**
54
     * belongs to Business.
55
     *
56
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo Relationship Vacancy belongs to Business query
57
     */
58 16
    public function business()
59
    {
60 16
        return $this->belongsTo(Business::class);
61
    }
62
63
    /**
64
     * for Service.
65
     *
66
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo Relationship Vacancy is for providing Service query
67
     */
68 26
    public function service()
69
    {
70 26
        return $this->belongsTo(Service::class);
71
    }
72
73
    /**
74
     * Humanresource.
75
     *
76
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
77
     */
78 18
    public function humanresource()
79
    {
80 18
        return $this->belongsTo(Humanresource::class);
81
    }
82
83
    /**
84
     * holds many Appointments.
85
     *
86
     * @return \Illuminate\Database\Eloquent\Relations\HasMany Relationship Vacancy belongs to Business query
87
     */
88 10
    public function appointments()
89
    {
90 10
        return $this->hasMany(Appointment::class);
91
    }
92
93
    /**
94
     * Humanresource Slug.
95
     *
96
     * @return string
97
     */
98
    public function humanresourceSlug()
99
    {
100
        if ($this->humanresource) {
0 ignored issues
show
Bug introduced by
The property humanresource does not seem to exist. Did you mean humanresource_id?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
101
            return $this->humanresource->slug;
0 ignored issues
show
Bug introduced by
The property humanresource does not seem to exist. Did you mean humanresource_id?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
102
        }
103
104
        return '';
105
    }
106
107
    ////////////
108
    // Scopes //
109
    ////////////
110
111
    /**
112
     * Scope For Date.
113
     *
114
     * @param Illuminate\Database\Query $query
115
     * @param Carbon                    $date  Date of inquiry
116
     *
117
     * @return Illuminate\Database\Query Scoped query
118
     */
119 6
    public function scopeForDate($query, Carbon $date)
120
    {
121 6
        return $query->where('date', '=', $date->toDateString());
122
    }
123
124
    /**
125
     * Scope For DateTime.
126
     *
127
     * @param Illuminate\Database\Query $query
128
     * @param Carbon                    $datetime Date and Time of inquiry
129
     *
130
     * @return Illuminate\Database\Query Scoped query
131
     */
132 6
    public function scopeForDateTime($query, Carbon $datetime)
133
    {
134 6
        return $query->where('start_at', '<=', $datetime->toDateTimeString())
135 6
                        ->where('finish_at', '>=', $datetime->toDateTimeString());
136
    }
137
138
    /**
139
     * Scope only Future.
140
     *
141
     * @param Illuminate\Database\Query $query
142
     *
143
     * @return Illuminate\Database\Query Scoped query
144
     */
145 1
    public function scopeFuture($query)
146
    {
147 1
        return $query->where('date', '>', Carbon::now());
148
    }
149
150
    /**
151
     * Scope For Service.
152
     *
153
     * @param Illuminate\Database\Query $query
154
     * @param int serviceId  $service Inquired Service to filter
155
     *
156
     * @return Illuminate\Database\Query Scoped query
157
     */
158 14
    public function scopeForService($query, $serviceId)
159
    {
160 14
        return $query->where('service_id', '=', $serviceId);
161
    }
162
163
    /////////////////////
164
    // Soft Attributes //
165
    /////////////////////
166
167
    /**
168
     * is Holding Any Appointment for given User.
169
     *
170
     * ToDo: Remove from here as needs knowledge from User
171
     *
172
     * @param int $userId User to check belonging Appointments
173
     *
174
     * @return bool Vacancy holds at least one Appointment of User
175
     */
176 2
    public function isHoldingAnyFor($userId)
177
    {
178 2
        $appointments = $this->appointments()->get();
179
180 2
        foreach ($appointments as $appointment) {
181 2
            $contact = $appointment->contact()->first();
182 2
            if ($contact->isProfileOf($userId)) {
183 1
                return true;
184
            }
185 1
        }
186
187 1
        return false;
188
    }
189
190
    /**
191
     * is Full.
192
     *
193
     * @return bool Vacancy is fully booked
194
     */
195
#    public function isFull()
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
196
#    {
197
#        return $this->getFreeSlotsCount() <= 0;
198
#    }
199
200
    /**
201
     * get free slots count.
202
     *
203
     * @return int Count Capacity minus Used
204
     */
205
#    public function getFreeSlotsCount()
0 ignored issues
show
Unused Code Comprehensibility introduced by
49% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
206
#    {
207
#        $count = $this->appointments()->active()->count();
208
#
209
#        return $this->capacity - $count;
210
#    }
211
212
    /**
213
     * get capacity.
214
     *
215
     * @return int Capacity of the vacancy (in appointment instances)
216
     */
217 17
    public function getCapacityAttribute()
218
    {
219 17
        if ($this->humanresource) {
0 ignored issues
show
Bug introduced by
The property humanresource does not seem to exist. Did you mean humanresource_id?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
220
            return intval($this->humanresource->capacity);
0 ignored issues
show
Bug introduced by
The property humanresource does not seem to exist. Did you mean humanresource_id?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
221
        }
222
223 17
        return intval($this->attributes['capacity']);
224
    }
225
226
    /**
227
     * has Room.
228
     *
229
     * @return bool There is more capacity than used
230
     */
231 4
    public function hasRoom()
232
    {
233 4
        return $this->capacity > $this->appointments()->active()->count();
234
    }
235
236
    /**
237
     * has Room between time.
238
     *
239
     * @return bool There is more capacity than used
240
     */
241 4
    public function hasRoomBetween(Carbon $startAt, Carbon $finishAt)
242
    {
243 4
        return $this->capacity > $this->business
0 ignored issues
show
Bug introduced by
The property business does not seem to exist. Did you mean business_id?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
244 4
                                      ->bookings()
245 4
                                      ->active()
246 4
                                      ->affectingInterval($startAt, $finishAt)
247 4
                                      ->affectingHumanresource($this->humanresource_id)
248 4
                                      ->count() &&
249 4
            ($this->start_at <= $startAt && $this->finish_at >= $finishAt);
250
    }
251
252
    /**
253
     * Get available capacity between time.
254
     *
255
     * @return int Available capacity
256
     */
257 8
    public function getAvailableCapacityBetween(Carbon $startAt, Carbon $finishAt)
258
    {
259 8
        if (!($this->start_at <= $startAt && $this->finish_at >= $finishAt)) {
260 7
            return 0;
261
        }
262
263 8
        $count = $this->business
0 ignored issues
show
Bug introduced by
The property business does not seem to exist. Did you mean business_id?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
264 8
                      ->bookings()
265 8
                      ->with('humanresource')
266 8
                      ->active()
267 8
                      ->affectingHumanresource($this->humanresource_id)
268 8
                      ->affectingInterval($startAt, $finishAt)
269 8
                      ->count();
270
271 8
        return $this->capacity - intval($count);
272
    }
273
}
274