Completed
Pull Request — master (#48)
by claudio
03:05
created

MeetingsController::store()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2.004

Importance

Changes 4
Bugs 2 Features 0
Metric Value
dl 0
loc 13
ccs 9
cts 10
cp 0.9
rs 9.4286
c 4
b 2
f 0
cc 2
eloc 8
nc 2
nop 2
crap 2.004
1
<?php
2
3
namespace plunner\Http\Controllers\Employees\Planners;
4
5
use Illuminate\Http\Request;
6
7
use plunner\Http\Requests;
8
use plunner\Http\Requests\Employees\MeetingRequest;
9
use plunner\Http\Controllers\Controller;
10
11
use plunner\Meeting;
12
use plunner\Group;
13
14
class MeetingsController extends Controller
15
{
16
    /**
17
     * ExampleController constructor.
18
     */
19 30
    public function __construct()
20
    {
21 30
        config(['auth.model' => \plunner\Planner::class]);
22 30
        config(['jwt.user' => \plunner\Planner::class]);
23 30
        $this->middleware('jwt.authandrefresh:mode-en');
24 30
    }
25
26
    /**
27
     * Display a listing of the resource.
28
     *
29
     *  @param int $groupId
30
     * @return mixed
31
     */
32 2
    public function index($groupId)
33
    {
34 2
        $group = Group::findOrFail($groupId);
35 2
        $this->authorize($group);
36 2
        return $group->meetings;
37
        //TODO get only current meetings via a get query
38
    }
39
40
    /**
41
     * Display the specified resource.
42
     *
43
     * @param int $groupId
44
     * @param int $meetingId
45
     * @return mixed
46
     */
47 6
    public function show($groupId, $meetingId)
48
    {
49 6
        $group = Group::findOrFail($groupId);
50 6
        $this->authorize($group);
51 5
        $meeting = Meeting::where('group_id', $groupId)->findOrFail($meetingId);
52 3
        $this->authorize($meeting);
53 3
        return $meeting;
54
    }
55
56
    /*
57
     * Check if a meeting with this title already exists for this group.
58
     */
59
    private function checkTitleAlreadyExists($title, $group)
60
    {
61 12
        return in_array($title, array_map(function($meeting)
62
        {
63 12
            return $meeting['title'];
64 12
        },
65 12
        $group->meetings->toArray()));
66
    }
67
68
    /**
69
     * Store a newly created resource in storage.
70
     *
71
     * @param MeetingRequest $request
72
     * @param int $groupId
73
     * @return static
74
     */
75 10
    public function store(MeetingRequest $request, $groupId)
76
    {
77 10
        $group = Group::findOrFail($groupId);
78 10
        $this->authorize($group);
79 10
        $input = $request->all();
80 10
        if ($this->checkTitleAlreadyExists($input['title'], $group))//TODO create methods inside model is nto the correct way
81 10
        {
82 2
            abort(422); //TODO thsi is note the correct way
83
            //TODO This is not a contraint to do. but anyway if we want to do constraints we have to use requests
84
        }
85 10
        $meeting = $group->meetings()->create($input);
86 10
        return $meeting;
87
    }
88
89
    /**
90
     * Update the specified resource in storage.
91
     *
92
     * @param MeetingRequest $request
93
     * @param int $meetingId
94
     * @param int $groupId
95
     * @return mixed
96
     */
97 8
    public function update(MeetingRequest $request, $groupId, $meetingId)
98
    {
99 8
        $meeting = Meeting::findOrFail($meetingId);
100 6
        $this->authorize($meeting);
101 4
        $group = Group::findOrFail($groupId);
102 4
        $this->authorize($group);
103 4
        $input = $request->all();
104 4
        if ($this->checkTitleAlreadyExists($input['title'], $group))
105 4
        {
106
            abort(422);
107
        }
108 4
        $meeting->update($input);
109 4
        return $meeting;
110
    }
111
112
    /**
113
     * Remove the specified resource from storage.
114
     *
115
     * @param int $groupId
116
     * @param int $meetingId
117
     * @return mixed
118
     */
119 8
    public function destroy($groupId, $meetingId)
120
    {
121 8
        $meeting = Meeting::findOrFail($meetingId);
122 6
        $this->authorize($meeting);
123 4
        $group = Group::findOrFail($groupId);
124 4
        $this->authorize($group);
125 4
        $meeting->delete();
126 4
        return $meeting;
127
    }
128
}
129