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

Vacancy   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 212
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 83.72%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 20
c 4
b 1
f 0
lcom 2
cbo 2
dl 0
loc 212
ccs 36
cts 43
cp 0.8372
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A business() 0 4 1
A service() 0 4 1
A appointments() 0 4 1
A scopeForDate() 0 4 1
A scopeForDateTime() 0 5 1
A isHoldingAnyFor() 0 13 3
A getCapacityAttribute() 0 4 1
A getAvailableCapacityBetween() 0 10 3
A scopeFuture() 0 4 1
A scopeForService() 0 4 1
A isFull() 0 4 1
A getFreeSlotsCount() 0 6 1
A hasRoom() 0 4 1
A hasRoomBetween() 0 8 3
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 Timegridio\Concierge\Models\Business $business
10
 * @property string $date Local timezone date of the published Vacancy
11
 */
12
class Vacancy extends EloquentModel
13
{
14
    /**
15
     * The attributes that are mass assignable.
16
     *
17
     * @var array
18
     */
19
    protected $fillable = ['business_id', 'service_id', 'date', 'start_at', 'finish_at', 'capacity'];
20
21
    /**
22
     * The attributes that aren't mass assignable.
23
     *
24
     * @var array
25
     */
26
    protected $guarded = ['id'];
27
28
    /**
29
     * The attributes that should be mutated to dates.
30
     *
31
     * @var array
32
     */
33
    protected $dates = ['start_at', 'finish_at'];
34
35
    ///////////////////
36
    // Relationships //
37
    ///////////////////
38
39
    /**
40
     * belongs to Business.
41
     *
42
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo Relationship Vacancy belongs to Business query
43
     */
44 14
    public function business()
45
    {
46 14
        return $this->belongsTo(Business::class);
47
    }
48
49
    /**
50
     * for Service.
51
     *
52
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo Relationship Vacancy is for providing Service query
53
     */
54 15
    public function service()
55
    {
56 15
        return $this->belongsTo(Service::class);
57
    }
58
59
    /**
60
     * holds many Appointments.
61
     *
62
     * @return \Illuminate\Database\Eloquent\Relations\HasMany Relationship Vacancy belongs to Business query
63
     */
64 14
    public function appointments()
65
    {
66 14
        return $this->hasMany(Appointment::class);
67
    }
68
69
    ////////////
70
    // Scopes //
71
    ////////////
72
73
    /**
74
     * Scope For Date.
75
     *
76
     * @param Illuminate\Database\Query $query
77
     * @param Carbon                    $date  Date of inquiry
78
     *
79
     * @return Illuminate\Database\Query Scoped query
80
     */
81 4
    public function scopeForDate($query, Carbon $date)
82
    {
83 4
        return $query->where('date', '=', $date->toDateString());
84
    }
85
86
    /**
87
     * Scope For DateTime.
88
     *
89
     * @param Illuminate\Database\Query $query
90
     * @param Carbon                    $datetime Date and Time of inquiry
91
     *
92
     * @return Illuminate\Database\Query Scoped query
93
     */
94 6
    public function scopeForDateTime($query, Carbon $datetime)
95
    {
96 6
        return $query->where('start_at', '<=', $datetime->toDateTimeString())
97 6
                        ->where('finish_at', '>=', $datetime->toDateTimeString());
98
    }
99
100
    /**
101
     * Scope only Future.
102
     *
103
     * @param Illuminate\Database\Query $query
104
     *
105
     * @return Illuminate\Database\Query Scoped query
106
     */
107
    public function scopeFuture($query)
108
    {
109
        return $query->where('date', '>', Carbon::now());
110
    }
111
112
    /**
113
     * Scope For Service.
114
     *
115
     * @param Illuminate\Database\Query $query
116
     * @param int serviceId  $service Inquired Service to filter
117
     *
118
     * @return Illuminate\Database\Query Scoped query
119
     */
120 12
    public function scopeForService($query, $serviceId)
121
    {
122 12
        return $query->where('service_id', '=', $serviceId);
123
    }
124
125
    /////////////////////
126
    // Soft Attributes //
127
    /////////////////////
128
129
    /**
130
     * is Holding Any Appointment for given User.
131
     *
132
     * ToDo: Remove from here as needs knowledge from User
133
     *
134
     * @param int $userId User to check belonging Appointments
135
     *
136
     * @return bool Vacancy holds at least one Appointment of User
137
     */
138 2
    public function isHoldingAnyFor($userId)
139
    {
140 2
        $appointments = $this->appointments()->get();
141
142 2
        foreach ($appointments as $appointment) {
143 2
            $contact = $appointment->contact()->first();
144 2
            if ($contact->isProfileOf($userId)) {
145 1
                return true;
146
            }
147 1
        }
148
149 1
        return false;
150
    }
151
152
    /**
153
     * is Full.
154
     *
155
     * @return bool Vacancy is fully booked
156
     */
157
    public function isFull()
158
    {
159
        return $this->getFreeSlotsCount() <= 0;
160
    }
161
162
    /**
163
     * get free slots count.
164
     *
165
     * @return int Count Capacity minus Used
166
     */
167
    public function getFreeSlotsCount()
168
    {
169
        $count = $this->appointments()->active()->count();
170
171
        return $this->capacity - $count;
0 ignored issues
show
Documentation introduced by
The property capacity does not exist on object<Timegridio\Concierge\Models\Vacancy>. 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...
172
    }
173
174
    /**
175
     * get capacity.
176
     *
177
     * @return int Capacity of the vacancy (in appointment instances)
178
     */
179 15
    public function getCapacityAttribute()
180
    {
181 15
        return intval($this->attributes['capacity']);
182
    }
183
184
    /**
185
     * has Room.
186
     *
187
     * @return bool There is more capacity than used
188
     */
189 4
    public function hasRoom()
190
    {
191 4
        return $this->capacity > $this->appointments()->active()->count();
0 ignored issues
show
Documentation introduced by
The property capacity does not exist on object<Timegridio\Concierge\Models\Vacancy>. 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...
192
    }
193
194
    /**
195
     * has Room between time.
196
     *
197
     * @return bool There is more capacity than used
198
     */
199 2
    public function hasRoomBetween(Carbon $startAt, Carbon $finishAt)
200
    {
201 2
        return $this->capacity > $this->appointments()
0 ignored issues
show
Documentation introduced by
The property capacity does not exist on object<Timegridio\Concierge\Models\Vacancy>. 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...
202 2
                                        ->active()
203 2
                                        ->affectingInterval($startAt, $finishAt)
204 2
                                        ->count() &&
205 2
            ($this->start_at <= $startAt && $this->finish_at >= $finishAt);
0 ignored issues
show
Documentation introduced by
The property start_at does not exist on object<Timegridio\Concierge\Models\Vacancy>. 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...
Documentation introduced by
The property finish_at does not exist on object<Timegridio\Concierge\Models\Vacancy>. 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...
206
    }
207
208
    /**
209
     * Get available capacity between time.
210
     *
211
     * @return int Available capacity
212
     */
213 8
    public function getAvailableCapacityBetween(Carbon $startAt, Carbon $finishAt)
214
    {
215 8
        if (!($this->start_at <= $startAt && $this->finish_at >= $finishAt)) {
0 ignored issues
show
Documentation introduced by
The property start_at does not exist on object<Timegridio\Concierge\Models\Vacancy>. 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...
Documentation introduced by
The property finish_at does not exist on object<Timegridio\Concierge\Models\Vacancy>. 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...
216 7
            return 0;
217
        }
218
219 8
        $count = $this->appointments()->active()->affectingInterval($startAt, $finishAt)->count();
220
221 8
        return $this->capacity - intval($count);
0 ignored issues
show
Documentation introduced by
The property capacity does not exist on object<Timegridio\Concierge\Models\Vacancy>. 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...
222
    }
223
}
224