Completed
Push — master ( 867505...d34c07 )
by Ariel
12:14
created

Vacancy::scopeFuture()   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 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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 16
    public function appointments()
65
    {
66 16
        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 1
    public function scopeFuture($query)
108
    {
109 1
        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()
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...
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()
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...
168
#    {
169
#        $count = $this->appointments()->active()->count();
170
#
171
#        return $this->capacity - $count;
172
#    }
173
174
    /**
175
     * get capacity.
176
     *
177
     * @return int Capacity of the vacancy (in appointment instances)
178
     */
179 17
    public function getCapacityAttribute()
180
    {
181 17
        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 4
    public function hasRoomBetween(Carbon $startAt, Carbon $finishAt)
200
    {
201 4
        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 4
                                        ->active()
203 4
                                        ->affectingInterval($startAt, $finishAt)
204 4
                                        ->count() &&
205 4
            ($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