Completed
Push — master ( 5a99d5...4a640b )
by Jeff
14:01
created

EmployeeRepository::findEmployeeById()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Shop\Employees\Repositories;
4
5
use App\Shop\Base\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
13
14
class EmployeeRepository extends BaseRepository implements EmployeeRepositoryInterface
15
{
16
    /**
17
     * EmployeeRepository constructor.
18
     * @param Employee $employee
19
     */
20
    public function __construct(Employee $employee)
21
    {
22
        parent::__construct($employee);
23
        $this->model = $employee;
24
    }
25
26
    /**
27
     * List all the employees
28
     *
29
     * @param string $order
30
     * @param string $sort
31
     * @return array
32
     */
33
    public function listEmployees(string $order = 'id', string $sort = 'desc'): Collection
34
    {
35
        return $this->all(['*'], $order, $sort);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->all(array('*'), $order, $sort) also could return the type Illuminate\Database\Eloquent\Collection which is incompatible with the documented return type array.
Loading history...
36
    }
37
38
    /**
39
     * Create the employee
40
     *
41
     * @param array $params
42
     * @return Employee
43
     */
44
    public function createEmployee(array $params): Employee
45
    {
46
        $collection = collect($params);
47
48
        $employee = new Employee(($collection->except('password'))->all());
49
        $employee->password = bcrypt($collection->only('password'));
0 ignored issues
show
Bug Best Practice introduced by
The property password does not exist on App\Shop\Employees\Employee. Since you implemented __set, consider adding a @property annotation.
Loading history...
50
        $employee->save();
51
52
        return $employee;
53
    }
54
55
    /**
56
     * Find the employee by id
57
     *
58
     * @param int $id
59
     * @return Employee
60
     */
61
    public function findEmployeeById(int $id): Employee
62
    {
63
        try {
64
            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...
65
        } catch (ModelNotFoundException $e) {
66
            throw new EmployeeNotFoundException;
67
        }
68
    }
69
70
    /**
71
     * Update employee
72
     *
73
     * @param array $params
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
     * @return bool
100
     */
101
    public function hasRole(string $roleName): bool
102
    {
103
        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...
104
    }
105
106
    /**
107
     * @param Employee $employee
108
     * @return bool
109
     */
110
    public function isAuthUser(Employee $employee): bool
111
    {
112
        $isAuthUser = false;
113
        if (Auth::guard('admin')->user()->id == $employee->id)
114
        {
115
           $isAuthUser = true;
116
        }
117
        return $isAuthUser;
118
    }
119
120
121
}
122