Completed
Push — master ( e8868a...fb856e )
by claudio
04:41
created

Employee::verifyEmployee()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
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
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
64
     */
65 2
    public function company()
66
    {
67 2
        return $this->belongsTo('plunner\Company');
68
    }
69
70
    /**
71
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
72
     */
73 18
    public function groups()
74
    {
75 18
        return $this->belongsToMany('plunner\Group', 'employee_group', 'employee_id'); //needed for planner model
76
    }
77
78
    /**
79
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
80
     */
81 12
    public function calendars()
82
    {
83 12
        return $this->hasMany('plunner\Calendar');
84
    }
85
86
    /**
87
     * Get the e-mail address where password reset links are sent.
88
     * This is needed for multiple user type login
89
     *
90
     * Make email unique
91
     *
92
     * @return string
93
     */
94 2
    public function getEmailForPasswordReset()
95
    {
96 2
        list(, $caller) = debug_backtrace(false);
97 2
        if(isset($caller['class']))
98 2
            $caller = explode('\\', $caller['class']);
99
        else
100
            $caller = '';
101
102
        //check if this function is called by email sender
103 2
        if ((count($caller) && $caller[count($caller) - 1] == 'PasswordBroker') || (defined('HHVM_VERSION') && $caller == ''))
104 2
            return $this->email;
105
        //return unique identify for token repository
106 2
        return $this->email . $this->company->id;
107
    }
108
109
    /**
110
     * @param Group $group
111
     * @return bool
112
     */
113 8
    public function belongsToGroup(Group $group)
114
    {
115 8
        $group = $this->groups()->where('id', $group->id)->first();
116 8
        if(is_object($group) && $group->exists)
117 8
            return true;
118 6
        return false;
119
    }
120
121
    /*
122
     * for a normal employee the policyCheckable methods say if the employee can se or not the element
123
     */
124
125
    /**
126
     * @param Group $group
127
     * @return bool
128
     */
129 4
    public function verifyGroup(Group $group)
130
    {
131 4
        return $this->belongsToGroup($group);
132
    }
133
134
    /**
135
     * @param Employee $employee
136
     * @return bool
137
     */
138
    public function verifyEmployee(Employee $employee)
139
    {
140
        return $employee->company_id === $this->company_id;
141
    }
142
143
    /**
144
     * @param Company $company
145
     * @return bool
146
     */
147
    public function verifyCompany(Company $company)
148
    {
149
        return $company->id === $this->company_id;
150
    }
151
152
    /**
153
     * the employee can modify a calendar
154
     * @param Calendar $calendar
155
     * @return bool
156
     */
157 8
    public function verifyCalendar(Calendar $calendar)
158
    {
159
        //TODO test this
160 8
        return $calendar->employee_id == $this->id;
161
    }
162
}
163