Completed
Push — master ( cb49ac...f14b58 )
by claudio
13:10
created

Employee::verifyMeeting()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4286
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
3
namespace plunner;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Auth\Authenticatable;
7
use Illuminate\Auth\Passwords\CanResetPassword;
8
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
9
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
10
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
11
use Illuminate\Foundation\Auth\Access\Authorizable;
12
13
/**
14
 * Class Employee
15
 *
16
 * @package plunner
17
 * @author Claudio Cardinale <[email protected]>
18
 * @copyright 2015 Claudio Cardinale
19
 * @version 1.0.0
20
 * @property integer $id
21
 * @property string $name
22
 * @property string $email
23
 * @property string $password
24
 * @property integer $company_id
25
 * @property string $remember_token
26
 * @property \Carbon\Carbon $created_at
27
 * @property \Carbon\Carbon $updated_at
28
 * @property-read \plunner\Company $company
29
 * @property-read \Illuminate\Database\Eloquent\Collection|\plunner\Group[] $groups
30
 * @property-read \Illuminate\Database\Eloquent\Collection|\plunner\Meeting[] $meetings
31
 * @property-read \Illuminate\Database\Eloquent\Collection|\plunner\Calendar[] $calendars
32
 * @method static \Illuminate\Database\Query\Builder|\plunner\Employee whereId($value)
33
 * @method static \Illuminate\Database\Query\Builder|\plunner\Employee whereName($value)
34
 * @method static \Illuminate\Database\Query\Builder|\plunner\Employee whereEmail($value)
35
 * @method static \Illuminate\Database\Query\Builder|\plunner\Employee wherePassword($value)
36
 * @method static \Illuminate\Database\Query\Builder|\plunner\Employee whereCompanyId($value)
37
 * @method static \Illuminate\Database\Query\Builder|\plunner\Employee whereRememberToken($value)
38
 * @method static \Illuminate\Database\Query\Builder|\plunner\Employee whereCreatedAt($value)
39
 * @method static \Illuminate\Database\Query\Builder|\plunner\Employee whereUpdatedAt($value)
40
 * @property-read mixed $is_planner
41
 */
42
class Employee extends Model implements AuthenticatableContract,
43
                                        AuthorizableContract,
44
                                        CanResetPasswordContract,
45
                                        PolicyCheckable
46
{
47
    use Authenticatable, Authorizable, CanResetPassword;
48
49
    /**
50
     * The attributes that are mass assignable.
51
     *
52
     * @var array
53
     */
54
    protected $fillable = ['name', 'email', 'password'];
55
56
    /**
57
     * The attributes excluded from the model's JSON form.
58
     *
59
     * @var array
60
     */
61
    protected $hidden = ['password', 'remember_token', 'pivot'];
62
63
    /**
64
     * @var array
65
     */
66
    protected $appends = ['is_planner'];
67
68 23
    public function getIsPlannerAttribute()
69
    {
70 23
        return !($this->groupsManagedRelationship()->get()->isEmpty());
71
    }
72
73
    /**
74
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
75
     */
76 2
    public function company()
77
    {
78 2
        return $this->belongsTo('plunner\Company');
79
    }
80
81
    /**
82
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
83
     */
84 76
    public function groups()
85
    {
86 76
        return $this->belongsToMany('plunner\Group', 'employee_group', 'employee_id'); //needed for planner model
87
    }
88
89
    /**
90
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
91
     */
92 32
    public function calendars()
93
    {
94 32
        return $this->hasMany('plunner\Calendar');
95
    }
96
97
    /**
98
     * meetings where the user participates
99
     * to get all meetings where the user can go user groups with meetings
100
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
101
     */
102 10
    public function meetings(){
103
        //TODO durign the inserting chek if the meeting is of a group of the user
104 10
        return $this->belongsToMany(Meeting::class);
105
    }
106
107
    /**
108
     * Get the e-mail address where password reset links are sent.
109
     * This is needed for multiple user type login
110
     *
111
     * Make email unique
112
     *
113
     * @return string
114
     */
115 2
    public function getEmailForPasswordReset()
116
    {
117 2
        list(, $caller) = debug_backtrace(false);
118 2
        if(isset($caller['class']))
119 2
            $caller = explode('\\', $caller['class']);
120
        else
121
            $caller = '';
122
123
        //check if this function is called by email sender
124 2
        if ((count($caller) && $caller[count($caller) - 1] == 'PasswordBroker') || (defined('HHVM_VERSION') && $caller == ''))
125 2
            return $this->email;
126
        //return unique identify for token repository
127 2
        return $this->email . $this->company->id;
128
    }
129
130
    /**
131
     * @param Group $group
132
     * @return bool
133
     */
134 8
    public function belongsToGroup(Group $group)
135
    {
136 8
        $group = $this->groups()->where('id', $group->id)->first();
137 8
        return (is_object($group) && $group->exists);
138
    }
139
140
    /*
141
     * for a normal employee the policyCheckable methods say if the employee can se or not the element
142
     */
143
144
    /**
145
     * @param Group $group
146
     * @return bool
147
     */
148 4
    public function verifyGroup(Group $group)
149
    {
150 4
        return $this->belongsToGroup($group);
151
    }
152
153
    /**
154
     * @param Employee $employee
155
     * @return bool
156
     */
157
    public function verifyEmployee(Employee $employee)
158
    {
159
        return $employee->company_id === $this->company_id;
160
    }
161
162
    /**
163
     * @param Company $company
164
     * @return bool
165
     */
166
    public function verifyCompany(Company $company)
167
    {
168
        return $company->id === $this->company_id;
169
    }
170
171
    /**
172
     * the employee can modify a calendar
173
     * @param Calendar $calendar
174
     * @return bool
175
     */
176 22
    public function verifyCalendar(Calendar $calendar)
177
    {
178
        //TODO test this
179 22
        return $calendar->employee_id == $this->id;
180
    }
181
182
    /**
183
     * @param Timeslot $timeslot
184
     * @return bool
185
     */
186 8
    public function verifyTimeslot(Timeslot $timeslot)
187
    {
188
        //TODO test this
189 8
        return $timeslot->calendar->employee_id == $this->id;
0 ignored issues
show
Bug introduced by
The property calendar does not seem to exist. Did you mean calendar_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...
190
    }
191
192
    /**
193
     * @param Meeting $meeting
194
     * @return bool
195
     */
196 4
    public function verifyMeeting(Meeting $meeting)
197
    {
198
        //TODO test this
199 4
        $meeting = $this->meetings()->whereId($meeting->id)->first();
200 4
        return (is_object($meeting) && $meeting->exists);
201
    }
202
203
    /**
204
     * @param MeetingTimeslot $meetingTimeslot
205
     * @return bool
206
     */
207
    public function verifyMeetingTimeslot(MeetingTimeslot $meetingTimeslot)
208
    {
209
        //TODO implement and test this
210
        return false;
211
    }
212
213
    /**
214
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
215
     */
216 61
    protected function groupsManagedRelationship()
217
    {
218 61
        return $this->HasMany(Group::class, 'planner_id');
219
    }
220
}
221