Completed
Push — master ( b98dcf...958b55 )
by Ariel
07:39
created

Vacancy   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 17
c 3
b 1
f 0
lcom 2
cbo 2
dl 0
loc 178
ccs 36
cts 36
cp 1
rs 10

11 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 scopeForService() 0 4 1
A isHoldingAnyFor() 0 13 3
A getCapacityAttribute() 0 4 1
A hasRoom() 0 4 1
A hasRoomBetween() 0 8 3
A getAvailableCapacityBetween() 0 10 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 12
    public function business()
45
    {
46 12
        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 12
    public function service()
55
    {
56 12
        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 12
    public function appointments()
65
    {
66 12
        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 For Service.
102
     *
103
     * @param Illuminate\Database\Query $query
104
     * @param int serviceId  $service Inquired Service to filter
105
     *
106
     * @return Illuminate\Database\Query Scoped query
107
     */
108 12
    public function scopeForService($query, $serviceId)
109
    {
110 12
        return $query->where('service_id', '=', $serviceId);
111
    }
112
113
    /////////////////////
114
    // Soft Attributes //
115
    /////////////////////
116
117
    /**
118
     * is Holding Any Appointment for given User.
119
     *
120
     * ToDo: Remove from here as needs knowledge from User
121
     *
122
     * @param int $userId User to check belonging Appointments
123
     *
124
     * @return bool Vacancy holds at least one Appointment of User
125
     */
126 2
    public function isHoldingAnyFor($userId)
127
    {
128 2
        $appointments = $this->appointments()->get();
129
130 2
        foreach ($appointments as $appointment) {
131 2
            $contact = $appointment->contact()->first();
132 2
            if ($contact->isProfileOf($userId)) {
133 1
                return true;
134
            }
135 1
        }
136
137 1
        return false;
138
    }
139
140
    /**
141
     * get capacity.
142
     *
143
     * @return int Capacity of the vacancy (in appointment instances)
144
     */
145 12
    public function getCapacityAttribute()
146
    {
147 12
        return intval($this->attributes['capacity']);
148
    }
149
150
    /**
151
     * has Room.
152
     *
153
     * @return bool There is more capacity than used
154
     */
155 4
    public function hasRoom()
156
    {
157 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...
158
    }
159
160
    /**
161
     * has Room between time.
162
     *
163
     * @return bool There is more capacity than used
164
     */
165 2
    public function hasRoomBetween(Carbon $startAt, Carbon $finishAt)
166
    {
167 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...
168 2
                                        ->active()
169 2
                                        ->affectingInterval($startAt, $finishAt)
170 2
                                        ->count() &&
171 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...
172
    }
173
174
    /**
175
     * Get available capacity between time.
176
     *
177
     * @return int Available capacity
178
     */
179 6
    public function getAvailableCapacityBetween(Carbon $startAt, Carbon $finishAt)
180
    {
181 6
        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...
182 5
            return 0;
183
        }
184
185 6
        $count = $this->appointments()->active()->affectingInterval($startAt, $finishAt)->count();
186
187 6
        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...
188
    }
189
}
190