Passed
Push — master ( 78190a...90a731 )
by Jeff
05:50
created

EmployeeController::updateEmployee()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Shop\Admins\Requests\CreateEmployeeRequest;
6
use App\Shop\Admins\Requests\UpdateEmployeeRequest;
7
use App\Shop\Employees\Repositories\EmployeeRepository;
8
use App\Shop\Employees\Repositories\Interfaces\EmployeeRepositoryInterface;
9
use App\Shop\Roles\Repositories\RoleRepositoryInterface;
10
use App\Http\Controllers\Controller;
11
use Illuminate\Support\Facades\Hash;
12
13
class EmployeeController extends Controller
14
{
15
    /**
16
     * @var EmployeeRepositoryInterface
17
     */
18
    private $employeeRepo;
19
    /**
20
     * @var RoleRepositoryInterface
21
     */
22
    private $roleRepo;
23
24
    /**
25
     * EmployeeController constructor.
26
     *
27
     * @param EmployeeRepositoryInterface $employeeRepository
28
     * @param RoleRepositoryInterface $roleRepository
29
     */
30
    public function __construct(
31
        EmployeeRepositoryInterface $employeeRepository,
32
        RoleRepositoryInterface $roleRepository
33
    ) {
34
        $this->employeeRepo = $employeeRepository;
35
        $this->roleRepo = $roleRepository;
36
    }
37
38
    /**
39
     * Display a listing of the resource.
40
     *
41
     * @return \Illuminate\Http\Response
42
     */
43
    public function index()
44
    {
45
        $list = $this->employeeRepo->listEmployees('created_at', 'desc');
46
47
        return view('admin.employees.list', [
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('admin.emplo...Results($list->all()))) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
48
            'employees' => $this->employeeRepo->paginateArrayResults($list->all())
49
        ]);
50
    }
51
52
    /**
53
     * Show the form for creating a new resource.
54
     *
55
     * @return \Illuminate\Http\Response
56
     */
57
    public function create()
58
    {
59
        $roles = $this->roleRepo->listRoles();
60
61
        return view('admin.employees.create', compact('roles'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('admin.emplo...ate', compact('roles')) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
62
    }
63
64
    /**
65
     * Store a newly created resource in storage.
66
     *
67
     * @param  CreateEmployeeRequest $request
68
     *
69
     * @return \Illuminate\Http\Response
70
     */
71
    public function store(CreateEmployeeRequest $request)
72
    {
73
        $employee = $this->employeeRepo->createEmployee($request->all());
74
75
        if ($request->has('role')) {
76
            $employeeRepo = new EmployeeRepository($employee);
77
            $employeeRepo->syncRoles([$request->input('role')]);
78
        }
79
80
        return redirect()->route('admin.employees.index');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route('admin.employees.index') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
81
    }
82
83
    /**
84
     * Display the specified resource.
85
     *
86
     * @param  int $id
87
     *
88
     * @return \Illuminate\Http\Response
89
     */
90
    public function show(int $id)
91
    {
92
        $employee = $this->employeeRepo->findEmployeeById($id);
93
        return view('admin.employees.show', ['employee' => $employee]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('admin.emplo...mployee' => $employee)) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
94
    }
95
96
    /**
97
     * Show the form for editing the specified resource.
98
     *
99
     * @param  int $id
100
     *
101
     * @return \Illuminate\Http\Response
102
     */
103
    public function edit(int $id)
104
    {
105
        $employee = $this->employeeRepo->findEmployeeById($id);
106
        $roles = $this->roleRepo->listRoles('created_at', 'desc');
107
        $isCurrentUser = $this->employeeRepo->isAuthUser($employee);
108
109
        return view(
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('admin.emplo...uck('role_id')->all())) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
110
            'admin.employees.edit',
111
            [
112
                'employee' => $employee,
113
                'roles' => $roles,
114
                'isCurrentUser' => $isCurrentUser,
115
                'selectedIds' => $employee->roles()->pluck('role_id')->all()
116
            ]
117
        );
118
    }
119
120
    /**
121
     * Update the specified resource in storage.
122
     *
123
     * @param UpdateEmployeeRequest $request
124
     * @param  int $id
125
     *
126
     * @return \Illuminate\Http\Response
127
     */
128
    public function update(UpdateEmployeeRequest $request, $id)
129
    {
130
        $employee = $this->employeeRepo->findEmployeeById($id);
131
        $isCurrentUser = $this->employeeRepo->isAuthUser($employee);
132
133
        $empRepo = new EmployeeRepository($employee);
134
        $empRepo->updateEmployee($request->except('_token', '_method', 'password'));
135
136
        if ($request->has('password') && !empty($request->input('password'))) {
137
            $employee->password = Hash::make($request->input('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...
138
            $employee->save();
139
        }
140
141
        if ($request->has('roles') and !$isCurrentUser) {
142
            $employee->roles()->sync($request->input('roles'));
143
        } elseif (!$isCurrentUser) {
144
            $employee->roles()->detach();
145
        }
146
147
        return redirect()->route('admin.employees.edit', $id)
0 ignored issues
show
Bug introduced by
$id of type integer is incompatible with the type array expected by parameter $parameters of Illuminate\Routing\Redirector::route(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

147
        return redirect()->route('admin.employees.edit', /** @scrutinizer ignore-type */ $id)
Loading history...
Bug Best Practice introduced by
The expression return redirect()->route...', 'Update successful') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
148
            ->with('message', 'Update successful');
149
    }
150
151
    /**
152
     * Remove the specified resource from storage.
153
     *
154
     * @param  int $id
155
     *
156
     * @return \Illuminate\Http\Response
157
     * @throws \Exception
158
     */
159
    public function destroy(int $id)
160
    {
161
        $employee = $this->employeeRepo->findEmployeeById($id);
162
        $employeeRepo = new EmployeeRepository($employee);
163
        $employeeRepo->deleteEmployee();
164
165
        return redirect()->route('admin.employees.index')->with('message', 'Delete successful');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route...', 'Delete successful') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
166
    }
167
168
    /**
169
     * @param $id
170
     *
171
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
172
     */
173
    public function getProfile($id)
174
    {
175
        $employee = $this->employeeRepo->findEmployeeById($id);
176
        return view('admin.employees.profile', ['employee' => $employee]);
177
    }
178
179
    /**
180
     * @param UpdateEmployeeRequest $request
181
     * @param $id
182
     *
183
     * @return \Illuminate\Http\RedirectResponse
184
     */
185
    public function updateProfile(UpdateEmployeeRequest $request, $id)
186
    {
187
        $employee = $this->employeeRepo->findEmployeeById($id);
188
189
        $update = new EmployeeRepository($employee);
190
        $update->updateEmployee($request->except('_token', '_method', 'password'));
191
192
        if ($request->has('password') && $request->input('password') != '') {
193
            $update->updateEmployee($request->only('password'));
194
        }
195
196
        return redirect()->route('admin.employee.profile', $id)
197
            ->with('message', 'Update successful');
198
    }
199
}
200