Completed
Push — master ( a0021a...b62e87 )
by claudio
14:16 queued 08:46
created

MeetingTimeslotsController::destroy()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 17
ccs 13
cts 13
cp 1
rs 9.2
cc 4
eloc 13
nc 3
nop 3
crap 4
1
<?php
2
3
namespace plunner\Http\Controllers\Employees\Planners;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Facades\Response;
7
use plunner\Group;
8
use plunner\Http\Controllers\Controller;
9
use plunner\Http\Requests;
10
use plunner\Http\Requests\Employees\Meeting\MeetingTimeslotRequest;
11
use plunner\Meeting;
12
use plunner\MeetingTimeslot;
13
14
15
class MeetingTimeslotsController extends Controller
16
{
17 42
    public function __construct()
18
    {
19 42
        config(['auth.model' => \plunner\Planner::class]);
20 42
        config(['jwt.user' => \plunner\Planner::class]);
21 42
        $this->middleware('jwt.authandrefresh:mode-en');
22 42
    }
23
24
    /**
25
     * @param $groupId
26
     * @param $meetingId
27
     * @return mixed
28
     */
29 6
    public function index($groupId, $meetingId)
30
    {
31 6
        $group = Group::findOrFail($groupId);
32 6
        $this->authorize($group);
33 6
        $meeting = Meeting::findOrFail($meetingId);
34 6
        $this->authorize($meeting);
35
36 6
        if ($meeting->group_id == $groupId)
37 5
            return $meeting->timeslots;
38 3
        return Response::json(['error' => 'meeting->group_id <> groupId'], 403);
39
    }
40
41
    /**
42
     * Display the specified resource.
43
     *
44
     * @param int $groupId
45
     * @param int $meetingId
46
     * @param int $timeslotId
47
     * @return mixed
48
     */
49 12
    public function show($groupId, $meetingId, $timeslotId)
50
    {
51 12
        $group = Group::findOrFail($groupId);
52 12
        $this->authorize($group);
53 12
        $meeting = Meeting::findOrFail($meetingId);
54 12
        $this->authorize($meeting);
55 12
        $timeslot = MeetingTimeslot::findOrFail($timeslotId);
56 9
        $this->authorize($timeslot);
57
58 9
        if ($meeting->group_id == $groupId && $timeslot->meeting_id == $meetingId)
59 8
            return $timeslot;
60 3
        return Response::json(['error' => 'meeting->group_id <> groupId || timeslot->meeting_id <> meetingId'], 403);
61
    }
62
63
    /**
64
     * @param MeetingTimeslotRequest $request
65
     * @param $groupId
66
     * @param $meetingId
67
     * @return mixed
68
     */
69 9
    public function store(MeetingTimeslotRequest $request, $groupId, $meetingId)
70
    {
71 9
        $group = Group::findOrFail($groupId);
72 9
        $this->authorize($group);
73 9
        $meeting = Meeting::findOrFail($meetingId);
74 9
        $this->authorize($meeting);
75 9
        if ($meeting->start_time != NULL)
76 7
            return Response::json(['error' => 'the meeting is already planned'], 422);
77
78 6
        $input = $request->all();
79
80 6
        if ($meeting->group_id == $groupId) {
81 3
            $timeslot = $meeting->timeslots()->create($input);
82 3
            return $timeslot;
83
        }
84 3
        return Response::json(['error' => 'meeting->group_id <> groupId'], 403);
85
    }
86
87
    /**
88
     * @param MeetingTimeslotRequest $request
89
     * @param $groupId
90
     * @param $meetingId
91
     * @param $timeslotId
92
     */
93 9
    public function update(MeetingTimeslotRequest $request, $groupId, $meetingId, $timeslotId)
94
    {
95 9
        $group = Group::findOrFail($groupId);
96 9
        $this->authorize($group);
97 9
        $meeting = Meeting::findOrFail($meetingId);
98 9
        $this->authorize($meeting);
99 9
        if ($meeting->start_time != NULL)
100 7
            return Response::json(['error' => 'the meeting is already planned'], 422);
101 6
        $timeslot = MeetingTimeslot::findOrFail($timeslotId);
102 6
        $this->authorize($timeslot);
103
104 6
        $input = $request->all();
105
106 6
        if ($meeting->group_id == $groupId && $timeslot->meeting_id == $meetingId) {
107 3
            $timeslot->update($input);
108 3
            return $timeslot;
109
        }
110 3
        return Response::json(['error' => 'meeting->group_id <> groupId || timeslot->meeting_id <> meetingId'], 403);
111
    }
112
113
    /**
114
     * Remove the specified resource from storage.
115
     *
116
     * @param int $groupId
117
     * @param int $meetingId
118
     * @param int $timeslotId
119
     * @return mixed
120
     */
121 9
    public function destroy($groupId, $meetingId, $timeslotId)
122
    {
123 9
        $group = Group::findOrFail($groupId);
124 9
        $this->authorize($group);
125 9
        $meeting = Meeting::findOrFail($meetingId);
126 9
        $this->authorize($meeting);
127 9
        if ($meeting->start_time != NULL)
128 7
            return Response::json(['error' => 'the meeting is already planned'], 422);
129 6
        $timeslot = MeetingTimeslot::findOrFail($timeslotId);
130 6
        $this->authorize($timeslot);
131
132 6
        if ($meeting->group_id == $groupId && $timeslot->meeting_id == $meetingId) {
133 3
            $timeslot->delete();
134 3
            return $timeslot;
135
        }
136 3
        return Response::json(['error' => 'meeting->group_id <> groupId || timeslot->meeting_id <> meetingId'], 403);
137
    }
138
}
139