MeetingsController   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 14

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 14
dl 0
loc 167
c 0
b 0
f 0
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A index() 0 7 1
A create() 0 4 1
A store() 0 21 2
A parseAttachmentIds() 0 10 1
A show() 0 17 2
A edit() 0 8 1
A update() 0 21 2
A destroy() 0 6 1
1
<?php
2
3
/**
4
 * Storgman - Student Organizations Management
5
 * Copyright (C) 2014-2016, Dejan Angelov <[email protected]>
6
 *
7
 * This file is part of Storgman.
8
 *
9
 * Storgman is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * Storgman is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with Storgman.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 * @package Storgman
23
 * @copyright Copyright (C) 2014-2016, Dejan Angelov <[email protected]>
24
 * @license https://github.com/angelov/storgman/blob/master/LICENSE
25
 * @author Dejan Angelov <[email protected]>
26
 */
27
28
namespace Angelov\Storgman\Meetings\Http\Controllers;
29
30
use Angelov\Storgman\Core\Http\Controllers\BaseController;
31
use Angelov\Storgman\Meetings\Commands\CreateMeetingCommand;
32
use Angelov\Storgman\Meetings\Commands\DeleteMeetingCommand;
33
use Angelov\Storgman\Meetings\Commands\UpdateMeetingCommand;
34
use Angelov\Storgman\Meetings\Commands\UpdateMeetingReportCommand;
35
use Angelov\Storgman\Meetings\Exceptions\NoPreviousMeetingException;
36
use Angelov\Storgman\Meetings\Http\Requests\StoreMeetingRequest;
37
use Angelov\Storgman\Meetings\MeetingsPaginator;
38
use Angelov\Storgman\Meetings\MeetingsService;
39
use Angelov\Storgman\Members\Member;
40
use Illuminate\Contracts\Auth\Guard;
41
use Illuminate\Contracts\View\View;
42
use Illuminate\Http\JsonResponse;
43
use Illuminate\Http\RedirectResponse;
44
use Illuminate\Http\Request;
45
use Angelov\Storgman\Meetings\Repositories\MeetingsRepositoryInterface;
46
use Angelov\Storgman\Members\Repositories\MembersRepositoryInterface;
47
48
class MeetingsController extends BaseController
49
{
50
    protected $meetings;
51
    protected $members;
52
    protected $meetingsService;
53
54
    public function __construct(MeetingsRepositoryInterface $meetings, MembersRepositoryInterface $members, MeetingsService $meetingsService)
55
    {
56
        $this->meetings = $meetings;
57
        $this->members = $members;
58
        $this->meetingsService = $meetingsService;
59
    }
60
61
    /**
62
     * Display a listing of the meetings.
63
     * GET /meetings
64
     *
65
     * @param Request $request
66
     * @param MeetingsPaginator $paginator
67
     * @return View
68
     */
69
    public function index(Request $request, MeetingsPaginator $paginator)
70
    {
71
        $page = $request->get('page', 1);
72
        $meetings = $paginator->get($page, ['attendants']);
73
74
        return view('meetings.index', compact('meetings'));
75
    }
76
77
    /**
78
     * Show the form for creating a new meeting report.
79
     * GET /meetings/create
80
     *
81
     * @return View
82
     */
83
    public function create()
84
    {
85
        return view('meetings.create');
86
    }
87
88
    /**
89
     * Store a newly created meeting report in storage.
90
     * POST /meetings
91
     *
92
     * @param StoreMeetingRequest $request
93
     * @param Guard $auth
94
     * @return RedirectResponse
95
     */
96
    public function store(StoreMeetingRequest $request, Guard $auth)
97
    {
98
        $title = $request->get('title', '');
99
        $location = $request->get('location');
100
        $date = $request->get('date') ." ". $request->get('time');
101
        $details = $request->get('details', '');
102
        $notifyMembers = $request->get('notify') == '1' ? true : false;
103
        $attachments = $this->parseAttachmentIds($request->get('attachments'));
104
105
        /** @var Member $author */
106
        $author = $auth->user();
107
        $authorId = $author->getId();
108
109
        $command = new CreateMeetingCommand($title, $location, $date, $details, $authorId, $attachments, $notifyMembers);
110
111
        $meeting = dispatch($command);
112
113
        session()->flash('action-message', 'Meeting added successfully.');
114
115
        return redirect()->route('meetings.show', $meeting->getId());
116
    }
117
118
    // @todo move to separate class or something
119
    private function parseAttachmentIds($attachments)
120
    {
121
        $attachments = json_decode($attachments, true);
122
123
        $attachments = array_map(function($id) {
124
            return (int) $id;
125
        }, $attachments);
126
127
        return $attachments;
128
    }
129
130
    /**
131
     * Display the specified resource.
132
     * GET /meetings/{id}
133
     *
134
     * @param  int      $id
135
     * @return View
136
     */
137
    public function show($id)
138
    {
139
        $meeting = $this->meetings->get($id);
140
141
        $averageAttendants = $this->meetings->getAverageNumberOfAttendants();
142
        $previousMeeting = null;
143
144
        try {
145
            $previousMeeting = $this->meetings->getPreviousMeeting($meeting);
146
        } catch (NoPreviousMeetingException $e) {
147
            $previousMeeting = null;
148
        }
149
150
        $attendantsType = json_encode($this->meetings->getAttendantsTypeForMeeting($meeting));
151
152
        return view('meetings.show', compact('meeting', 'averageAttendants', 'previousMeeting', 'attendantsType'));
153
    }
154
155
    /**
156
     * Show the form for editing the specified resource.
157
     * GET /meetings/{id}/edit
158
     *
159
     * @param  int      $id
160
     * @return View
161
     */
162
    public function edit($id)
163
    {
164
        $meeting = $this->meetings->get($id);
165
        $attendants = $meeting->getAttendants();
166
        $attendantsIds = $this->meetingsService->prepareAttendantsIds($attendants);
167
168
        return view('meetings.edit', compact('meeting', 'attendantsIds'));
169
    }
170
171
    /**
172
     * Update the specified resource in storage.
173
     * PUT /meetings/{id}
174
     *
175
     * @param StoreMeetingRequest $request
176
     * @param  int $id
177
     * @return RedirectResponse
178
     */
179
    public function update(StoreMeetingRequest $request, $id)
180
    {
181
        $title = $request->get('title');
182
        $date = $request->get('date') ." ". $request->get('time');
183
        $details = $request->get('details', '');
184
        $location = $request->get('location');
185
        $attachments = $this->parseAttachmentIds($request->get('attachments'));
186
187
        dispatch(new UpdateMeetingCommand($id, $title, $location, $date, $details, $attachments));
188
189
        $minutes = $request->get('minutes', '');
190
        $attendants = $this->meetingsService->parseAttendantsIds($request->get('attendants', ''));
191
192
        if (count($attendants) > 0) { // maybe we should get the Meeting object and check there?
193
            dispatch(new UpdateMeetingReportCommand($id, $attendants, $minutes));
194
        }
195
196
        session()->flash('action-message', 'Meeting updated successfully.');
197
198
        return redirect()->route('meetings.index');
199
    }
200
201
    /**
202
     * Remove the specified resource from storage.
203
     * DELETE /meetings/{id}
204
     *
205
     * @param  int      $id
206
     * @return JsonResponse
207
     */
208
    public function destroy($id)
209
    {
210
        dispatch(new DeleteMeetingCommand($id));
211
212
        return $this->successfulJsonResponse('Meeting deleted successfully.');
213
    }
214
}
215