Completed
Pull Request — master (#48)
by claudio
05:21
created

Employee::verifyMeeting()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 5
ccs 0
cts 2
cp 0
rs 9.4286
c 1
b 1
f 0
cc 1
eloc 2
nc 1
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
 */
41
class Employee extends Model implements AuthenticatableContract,
42
                                        AuthorizableContract,
43
                                        CanResetPasswordContract,
44
                                        PolicyCheckable
45
{
46
    use Authenticatable, Authorizable, CanResetPassword;
47
48
    /**
49
     * The attributes that are mass assignable.
50
     *
51
     * @var array
52
     */
53
    protected $fillable = ['name', 'email', 'password'];
54
55
    /**
56
     * The attributes excluded from the model's JSON form.
57
     *
58
     * @var array
59
     */
60
    protected $hidden = ['password', 'remember_token', 'pivot'];
61
62
    /**
63
     * @var array
64
     */
65
    protected $appends = ['is_planner'];
66
67 23
    public function getIsPlannerAttribute()
68
    {
69 23
        return !($this->groupsManagedRelationship()->get()->isEmpty());
70
    }
71
72
    /**
73
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
74
     */
75 2
    public function company()
76
    {
77 2
        return $this->belongsTo('plunner\Company');
78
    }
79
80
    /**
81
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
82
     */
83 46
    public function groups()
84
    {
85 46
        return $this->belongsToMany('plunner\Group', 'employee_group', 'employee_id'); //needed for planner model
86
    }
87
88
    /**
89
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
90
     */
91 14
    public function calendars()
92
    {
93 14
        return $this->hasMany('plunner\Calendar');
94
    }
95
96
    /**
97
     * meetings where the user participates
98
     * to get all meetings where the user can go user groups with meetings
99
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
100
     */
101 2
    public function meetings(){
102
        //TODO durign the inserting chek if the meeting is of a group of the user
103 2
        return $this->belongsToMany(Meeting::class);
104
    }
105
106
    /**
107
     * Get the e-mail address where password reset links are sent.
108
     * This is needed for multiple user type login
109
     *
110
     * Make email unique
111
     *
112
     * @return string
113
     */
114 2
    public function getEmailForPasswordReset()
115
    {
116 2
        list(, $caller) = debug_backtrace(false);
117 2
        if(isset($caller['class']))
118 2
            $caller = explode('\\', $caller['class']);
119
        else
120
            $caller = '';
121
122
        //check if this function is called by email sender
123 2
        if ((count($caller) && $caller[count($caller) - 1] == 'PasswordBroker') || (defined('HHVM_VERSION') && $caller == ''))
124 2
            return $this->email;
125
        //return unique identify for token repository
126 2
        return $this->email . $this->company->id;
127
    }
128
129
    /**
130
     * @param Group $group
131
     * @return bool
132
     */
133 8
    public function belongsToGroup(Group $group)
134
    {
135 8
        $group = $this->groups()->where('id', $group->id)->first();
136 8
        return (is_object($group) && $group->exists);
137
    }
138
139
    /*
140
     * for a normal employee the policyCheckable methods say if the employee can se or not the element
141
     */
142
143
    /**
144
     * @param Group $group
145
     * @return bool
146
     */
147 4
    public function verifyGroup(Group $group)
148
    {
149 4
        return $this->belongsToGroup($group);
150
    }
151
152
    /**
153
     * @param Employee $employee
154
     * @return bool
155
     */
156
    public function verifyEmployee(Employee $employee)
157
    {
158
        return $employee->company_id === $this->company_id;
159
    }
160
161
    /**
162
     * @param Company $company
163
     * @return bool
164
     */
165
    public function verifyCompany(Company $company)
166
    {
167
        return $company->id === $this->company_id;
168
    }
169
170
    /**
171
     * the employee can modify a calendar
172
     * @param Calendar $calendar
173
     * @return bool
174
     */
175 8
    public function verifyCalendar(Calendar $calendar)
176
    {
177
        //TODO test this
178 8
        return $calendar->employee_id == $this->id;
179
    }
180
181
    /**
182
     * @param Meeting $meeting
183
     * @return bool
184
     */
185
    public function verifyMeeting(Meeting $meeting)
186
    {
187
        //TODO implement and test this
188
        return false;
189
    }
190
191
    /**
192
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
193
     */
194 49
    protected function groupsManagedRelationship()
195
    {
196 49
        return $this->HasMany(Group::class, 'planner_id');
197
    }
198
}
199