MeetingsController   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 60.27%

Importance

Changes 14
Bugs 6 Features 0
Metric Value
wmc 15
c 14
b 6
f 0
lcom 1
cbo 6
dl 0
loc 160
ccs 44
cts 73
cp 0.6027
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A index() 0 14 2
A show() 0 10 1
A store() 0 8 1
A putImg() 0 4 1
A showImage() 0 14 2
A getImg() 0 7 2
A update() 0 13 3
A destroy() 0 9 1
A storeImage() 0 11 1
1
<?php
2
3
namespace plunner\Http\Controllers\Employees\Planners;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Http\Response;
7
use plunner\Group;
8
use plunner\Http\Controllers\Controller;
9
use plunner\Http\Requests;
10
use plunner\Http\Requests\Employees\Meeting\MeetingRequest;
11
use plunner\Meeting;
12
13
class MeetingsController extends Controller
14
{
15 45
    public function __construct()
16
    {
17 45
        config(['auth.model' => \plunner\Planner::class]);
18 45
        config(['jwt.user' => \plunner\Planner::class]);
19 45
        $this->middleware('jwt.authandrefresh:mode-en');
20 45
    }
21
22
    /**
23
     * Display a listing of the resource.
24
     * ?current=1 -> to exclude old planed meetings
25
     *
26
     * @param int $groupId
27
     * @param Request $request needed for get query to get only current planed meetings (to be planned are all retrieved)
28
     * @return mixed
29
     */
30 6
    public function index($groupId, Request $request)
31
    {
32 6
        $group = Group::findOrFail($groupId);
33 6
        $this->authorize($group);
34 6
        $meetings = $group->meetings();
35 6
        if ($request->query('current'))
36 3
            $meetings->where(function ($query) { //parenthesis for conditions ...(C1 OR C2)...
37 3
                $query->where('start_time', '=', NULL);//to be planned
38
                //datetime to consider timezone, don't use mysql NOW()
39 3
                $query->orWhere('start_time', '>=', new \DateTime());//planned
40 3
            });
41
42 6
        return $meetings->get();
43
    }
44
45
    /**
46
     * Display the specified resource.
47
     *
48
     * @param int $groupId
49
     * @param int $meetingId
50
     * @return mixed
51
     */
52 9
    public function show($groupId, $meetingId)
53
    {
54 9
        $group = Group::findOrFail($groupId);
55 9
        $this->authorize($group);
56
        //Meeting::where('group_id', $groupId)->findOrFail($meetingId);
57
        //it is good but expensive and useless for the user experience
58 6
        $meeting = Meeting::findOrFail($meetingId);
59 3
        $this->authorize($meeting);
60 3
        return $meeting;
61
    }
62
63
    /**
64
     * Store a newly created resource in storage.
65
     *
66
     * @param MeetingRequest $request
67
     * @param int $groupId
68
     * @return static
69
     */
70 12
    public function store(MeetingRequest $request, $groupId)
71
    {
72 12
        $group = Group::findOrFail($groupId);
73 12
        $this->authorize($group);
74 12
        $input = $request->all();
75 12
        $meeting = $group->meetings()->create($input);
76 12
        return $meeting;
77
    }
78
79
    /**
80
     * Store a newly created resource in storage.
81
     *
82
     * @param \Illuminate\Http\Request $request
83
     * @param int $groupId
84
     * @param int $meetingId
85
     * @return static
86
     */
87
    public function storeImage(\Illuminate\Http\Request $request, $groupId, $meetingId)
88
    {
89
        $this->validate($request, ['file' => 'required|image']);
90
        $group = Group::findOrFail($groupId);
91
        $this->authorize($group);
92
        $meeting = Meeting::findOrFail($meetingId);
93
        $this->authorize($meeting);
94
        $file = $request->file('file');
95
        self::putImg($file, $meeting);
96
        return $meeting;
97
    }
98
99
    private static function putImg($file, Meeting $meeting)
100
    {
101
        \Storage::put('meetings/' . $meeting->id, \File::get($file));
0 ignored issues
show
Bug introduced by
The method put() does not seem to exist on object<Illuminate\Contracts\Filesystem\Factory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
102
    }
103
104
    /**
105
     * Store a newly created resource in storage.
106
     *
107
     * @param int $groupId
108
     * @param int $meetingId
109
     * @return static
110
     */
111
    public function showImage($groupId, $meetingId)
112
    {
113
        $group = Group::findOrFail($groupId);
114
        $this->authorize($group);
115
        $meeting = Meeting::findOrFail($meetingId);
116
        $this->authorize($meeting);
117
        $ret = self::getImg($meeting);
118
        $blank = storage_path('img/meetings.jpg');
119
        if ($ret === false)
120
            return (new Response(file_get_contents($blank), 200))
121
                ->header('Content-Type', 'image/jpeg');
122
        return (new Response($ret, 200))
123
            ->header('Content-Type', 'image/jpeg');
124
    }
125
126
    private static function getImg(Meeting $meeting)
127
    {
128
        $name = 'meetings/' . $meeting->id;
129
        if (!\Storage::exists($name))
0 ignored issues
show
Bug introduced by
The method exists() does not seem to exist on object<Illuminate\Contracts\Filesystem\Factory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
130
            return false;
131
        return \Storage::get($name);
132
    }
133
134
    /**
135
     * Update the specified resource in storage.
136
     *
137
     * @param MeetingRequest $request
138
     * @param int $meetingId
139
     * @param int $groupId
140
     * @return mixed
141
     */
142 9
    public function update(MeetingRequest $request, $groupId, $meetingId)
143
    {
144 9
        $group = Group::findOrFail($groupId);
145 9
        $this->authorize($group);
146 6
        $meeting = Meeting::findOrFail($meetingId);
147 3
        $this->authorize($meeting);
148 3
        $input = $request->all();
149
        //the planner cannot modify the duration of a planed meeting
150 3
        if ($meeting->start_time != NULL && $meeting->duration != $input['duration'])
151 2
            return Response::json(['error' => 'the meeting is already planned, you cannot change the duration'], 422);
0 ignored issues
show
Bug introduced by
The method json() does not seem to exist on object<Illuminate\Http\Response>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
152 3
        $meeting->update($input);
153 3
        return $meeting;
154
    }
155
156
    /**
157
     * Remove the specified resource from storage.
158
     *
159
     * @param int $groupId
160
     * @param int $meetingId
161
     * @return mixed
162
     */
163 9
    public function destroy($groupId, $meetingId)
164
    {
165 9
        $group = Group::findOrFail($groupId);
166 9
        $this->authorize($group);
167 6
        $meeting = Meeting::findOrFail($meetingId);
168 3
        $this->authorize($meeting);
169 3
        $meeting->delete();
170 3
        return $meeting;
171
    }
172
}
173