Completed
Pull Request — master (#28)
by claudio
05:35
created

Employee::company()   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
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
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 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 14
    public function calendars()
82
    {
83 14
        return $this->hasMany('plunner\Calendar');
84
    }
85
86
    /**
87
     * meetings where the user participates
88
     * to get all meetings where the user can go user groups with meetings
89
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
90
     */
91 2
    public function meetings(){
92
        //TODO durign the inserting chek if the meeting is of a group of the user
93 2
        return $this->belongsToMany(Meeting::class);
94
    }
95
96
    /**
97
     * Get the e-mail address where password reset links are sent.
98
     * This is needed for multiple user type login
99
     *
100
     * Make email unique
101
     *
102
     * @return string
103
     */
104 2
    public function getEmailForPasswordReset()
105
    {
106 2
        list(, $caller) = debug_backtrace(false);
107 2
        if(isset($caller['class']))
108 2
            $caller = explode('\\', $caller['class']);
109
        else
110
            $caller = '';
111
112
        //check if this function is called by email sender
113 2
        if ((count($caller) && $caller[count($caller) - 1] == 'PasswordBroker') || (defined('HHVM_VERSION') && $caller == ''))
114 2
            return $this->email;
115
        //return unique identify for token repository
116 2
        return $this->email . $this->company->id;
117
    }
118
119
    /**
120
     * @param Group $group
121
     * @return bool
122
     */
123 8
    public function belongsToGroup(Group $group)
124
    {
125 8
        $group = $this->groups()->where('id', $group->id)->first();
126 8
        if(is_object($group) && $group->exists)
127 8
            return true;
128 6
        return false;
129
    }
130
131
    /*
132
     * for a normal employee the policyCheckable methods say if the employee can se or not the element
133
     */
134
135
    /**
136
     * @param Group $group
137
     * @return bool
138
     */
139 4
    public function verifyGroup(Group $group)
140
    {
141 4
        return $this->belongsToGroup($group);
142
    }
143
144
    /**
145
     * @param Employee $employee
146
     * @return bool
147
     */
148
    public function verifyEmployee(Employee $employee)
149
    {
150
        return $employee->company_id === $this->company_id;
151
    }
152
153
    /**
154
     * @param Company $company
155
     * @return bool
156
     */
157
    public function verifyCompany(Company $company)
158
    {
159
        return $company->id === $this->company_id;
160
    }
161
162
    /**
163
     * the employee can modify a calendar
164
     * @param Calendar $calendar
165
     * @return bool
166
     */
167 8
    public function verifyCalendar(Calendar $calendar)
168
    {
169
        //TODO test this
170 8
        return $calendar->employee_id == $this->id;
171
    }
172
}
173