Completed
Pull Request — master (#5)
by claudio
11:05 queued 03:52
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

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
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 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'];
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 4
    public function groups()
81
    {
82 4
        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
    public function calendars()
97
    {
98
        return $this->hasMany('App\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
        $caller = explode('\\', $caller['class']);
113
114
        //check if this function is called by email sender
115 2
        if ($caller[count($caller) - 1] == 'PasswordBroker')
116 2
            return $this->email;
117
        //return unique identify for token repository
118 2
        return $this->email . $this->company->id;
119
    }
120
121
    /**
122
     * @param Group $group
123
     * @return bool
124
     */
125 4
    public function belongsToGroup(Group $group)
126
    {
127 4
        $group = $this->groups()->where('id', $group->id)->first();
128 4
        if(is_object($group) && $group->exists)
129 4
            return true;
130 4
        return false;
131
    }
132
133
    /*
134
     * for a normal employee the policyCheckable methods say if the employee can se or not the element
135
     */
136
137
    /**
138
     * @param Group $group
139
     * @return bool
140
     */
141
    public function verifyGroup(Group $group)
142
    {
143
        return $this->belongsToGroup($group);
144
    }
145
146
    /**
147
     * @param Employee $employee
148
     * @return bool
149
     */
150
    public function verifyEmployee(Employee $employee)
151
    {
152
        return $employee->company_id === $this->company_id;
153
    }
154
155
    /**
156
     * @param Company $company
157
     * @return bool
158
     */
159
    public function verifyCompany(Company $company)
160
    {
161
        return $company->id === $this->company_id;
162
    }
163
}
164