EmployeesController::store()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 2
crap 1
1
<?php
2
3
namespace plunner\Http\Controllers\Companies\Groups;
4
5
use Illuminate\Support\Facades\Response;
6
use plunner\Employee;
7
use plunner\Group;
8
use plunner\Http\Controllers\Controller;
9
use plunner\Http\Requests\Companies\Groups\EmployeeRequest;
10
11
/**
12
 * Class EmployeesController
13
 * @package plunner\Http\Controllers\Companies\Groups
14
 * @author Claudio Cardinale <[email protected]>
15
 * @copyright 2015 Claudio Cardinale
16
 * @version 1.0.0
17
 */
18
class EmployeesController extends Controller
19
{
20 24
    public function __construct()
21
    {
22 24
        config(['auth.model' => \plunner\Company::class]);
23 24
        config(['jwt.user' => \plunner\Company::class]);
24 24
        $this->middleware('jwt.authandrefresh:mode-cn');
25 24
    }
26
27
28
    /**
29
     * Display a listing of the resource.
30
     *
31
     * @param  int $groupId
32
     * @return \Illuminate\Http\Response
33
     */
34 3
    public function index($groupId)
35
    {
36
        //
37 3
        $group = Group::findOrFail($groupId);
38 3
        $this->authorize($group);
39 3
        return $group->employees;
40
    }
41
42
    /**
43
     * Store a newly created resource in storage.
44
     *
45
     * @param  EmployeeRequest $request
46
     * @param  int $groupId
47
     * @return \Illuminate\Http\Response
48
     */
49 6
    public function store(EmployeeRequest $request, $groupId)
50
    {
51
        //
52 6
        $group = Group::findOrFail($groupId);
53 6
        $this->authorize($group);
54 3
        $id = $request->all()['id'];
55 3
        $group->employees()->attach($id);
56 3
        return $group->employees;
57
    }
58
59
    /**
60
     * Remove the specified resource from storage.
61
     *
62
     * @param  int $groupId
63
     * @param  int $employeeId
64
     * @return \Illuminate\Http\Response
65
     */
66 12
    public function destroy($groupId, $employeeId)
67
    {
68
        //
69 12
        $employee = Employee::findOrFail($employeeId);
70 12
        $this->authorize($employee);
71 6
        $group = Group::findOrFail($groupId);
72 6
        if (!$employee->belongsToGroup($group))
73 6
            return Response::json(['error' => 'employId <> groupId'], 404);
74 3
        $employee->groups()->detach($groupId);
75 3
        return $group->employees;
76
    }
77
}
78