Completed
Push — master ( 5d49af...15d463 )
by Jeff
06:07
created

EmployeeRepository::listRoles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Shop\Employees\Repositories;
4
5
use Jsdecena\Baserepo\BaseRepository;
6
use App\Shop\Employees\Employee;
7
use App\Shop\Employees\Exceptions\EmployeeNotFoundException;
8
use App\Shop\Employees\Repositories\Interfaces\EmployeeRepositoryInterface;
9
use Illuminate\Database\Eloquent\ModelNotFoundException;
10
use Illuminate\Support\Collection;
11
use Illuminate\Support\Facades\Auth;
12
use Illuminate\Support\Facades\Hash;
13
14
class EmployeeRepository extends BaseRepository implements EmployeeRepositoryInterface
15
{
16
    /**
17
     * EmployeeRepository constructor.
18
     *
19
     * @param Employee $employee
20
     */
21
    public function __construct(Employee $employee)
22
    {
23
        parent::__construct($employee);
24
        $this->model = $employee;
25
    }
26
27
    /**
28
     * List all the employees
29
     *
30
     * @param string $order
31
     * @param string $sort
32
     *
33
     * @return Collection
34
     */
35
    public function listEmployees(string $order = 'id', string $sort = 'desc'): Collection
36
    {
37
        return $this->all(['*'], $order, $sort);
38
    }
39
40
    /**
41
     * Create the employee
42
     *
43
     * @param array $data
44
     *
45
     * @return Employee
46
     */
47
    public function createEmployee(array $data): Employee
48
    {
49
        $data['password'] = Hash::make($data['password']);
50
        return $this->create($data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->create($data) returns the type Illuminate\Database\Eloquent\Model which includes types incompatible with the type-hinted return App\Shop\Employees\Employee.
Loading history...
51
    }
52
53
    /**
54
     * Find the employee by id
55
     *
56
     * @param int $id
57
     *
58
     * @return Employee
59
     */
60
    public function findEmployeeById(int $id): Employee
61
    {
62
        try {
63
            return $this->findOneOrFail($id);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->findOneOrFail($id) returns the type Illuminate\Database\Eloquent\Model which includes types incompatible with the type-hinted return App\Shop\Employees\Employee.
Loading history...
64
        } catch (ModelNotFoundException $e) {
65
            throw new EmployeeNotFoundException;
66
        }
67
    }
68
69
    /**
70
     * Update employee
71
     *
72
     * @param array $params
73
     *
74
     * @return bool
75
     */
76
    public function updateEmployee(array $params): bool
77
    {
78
        return $this->model->update($params);
79
    }
80
81
    /**
82
     * @param array $roleIds
83
     */
84
    public function syncRoles(array $roleIds)
85
    {
86
        $this->model->roles()->sync($roleIds);
87
    }
88
89
    /**
90
     * @return Collection
91
     */
92
    public function listRoles(): Collection
93
    {
94
        return $this->model->roles()->get();
95
    }
96
97
    /**
98
     * @param string $roleName
99
     *
100
     * @return bool
101
     */
102
    public function hasRole(string $roleName): bool
103
    {
104
        return $this->model->hasRole($roleName);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->model->hasRole($roleName) could return the type Illuminate\Database\Eloquent\Builder which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
105
    }
106
107
    /**
108
     * @param Employee $employee
109
     *
110
     * @return bool
111
     */
112
    public function isAuthUser(Employee $employee): bool
113
    {
114
        $isAuthUser = false;
115
        if (Auth::guard('employee')->user()->id == $employee->id) {
116
            $isAuthUser = true;
117
        }
118
        return $isAuthUser;
119
    }
120
121
    /**
122
     * @return bool
123
     * @throws \Exception
124
     */
125
    public function deleteEmployee() : bool
126
    {
127
        return $this->delete();
128
    }
129
}
130