Completed
Pull Request — master (#14)
by claudio
10:12
created

Employee::verifyCalendar()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 5
ccs 2
cts 2
cp 1
rs 9.4286
cc 1
eloc 2
nc 1
nop 1
crap 1
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 database table used by the model.
50
     *
51
     * @var string
52
     */
53
     //protected $table = 'employees';
54
55
    /**
56
     * The attributes that are mass assignable.
57
     *
58
     * @var array
59
     */
60
    protected $fillable = ['name', 'email', 'password'];
61
62
    /**
63
     * The attributes excluded from the model's JSON form.
64
     *
65
     * @var array
66
     */
67
    protected $hidden = ['password', 'remember_token', 'pivot'];
68
69
    /**
70
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
71
     */
72 2
    public function company()
73
    {
74 2
        return $this->belongsTo('plunner\Company');
75
    }
76
77
    /**
78
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
79
     */
80 18
    public function groups()
81
    {
82 18
        return $this->belongsToMany('plunner\Group', 'employee_group', 'employee_id'); //needed for planner model
83
    }
84
85
    /**
86
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
87
     */
88
    public function meetings()
89
    {
90
        return $this->belongsToMany('plunner\Meeting');
91
    }
92
93
    /**
94
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
95
     */
96 12
    public function calendars()
97
    {
98 12
        return $this->hasMany('plunner\Calendar');
99
    }
100
101
    /**
102
     * Get the e-mail address where password reset links are sent.
103
     * This is needed for multiple user type login
104
     *
105
     * Make email unique
106
     *
107
     * @return string
108
     */
109 2
    public function getEmailForPasswordReset()
110
    {
111 2
        list(, $caller) = debug_backtrace(false);
112 2
        if(isset($caller['class']))
113 2
            $caller = explode('\\', $caller['class']);
114
        else
115
            $caller = '';
116
117
        //check if this function is called by email sender
118 2
        if ((count($caller) && $caller[count($caller) - 1] == 'PasswordBroker') || (defined('HHVM_VERSION') && $caller == ''))
119 2
            return $this->email;
120
        //return unique identify for token repository
121 2
        return $this->email . $this->company->id;
122
    }
123
124
    /**
125
     * @param Group $group
126
     * @return bool
127
     */
128 8
    public function belongsToGroup(Group $group)
129
    {
130 8
        $group = $this->groups()->where('id', $group->id)->first();
131 8
        if(is_object($group) && $group->exists)
132 8
            return true;
133 6
        return false;
134
    }
135
136
    /*
137
     * for a normal employee the policyCheckable methods say if the employee can se or not the element
138
     */
139
140
    /**
141
     * @param Group $group
142
     * @return bool
143
     */
144 4
    public function verifyGroup(Group $group)
145
    {
146 4
        return $this->belongsToGroup($group);
147
    }
148
149
    /**
150
     * @param Employee $employee
151
     * @return bool
152
     */
153
    public function verifyEmployee(Employee $employee)
154
    {
155
        return $employee->company_id === $this->company_id;
156
    }
157
158
    /**
159
     * @param Company $company
160
     * @return bool
161
     */
162
    public function verifyCompany(Company $company)
163
    {
164
        return $company->id === $this->company_id;
165
    }
166
167
    /**
168
     * the employee can modify a calendar
169
     * @param Calendar $calendar
170
     * @return bool
171
     */
172 8
    public function verifyCalendar(Calendar $calendar)
173
    {
174
        //TODO test this
175 8
        return $calendar->employee_id == $this->id;
176
    }
177
}
178