Completed
Push — master ( 21057e...5ab18c )
by claudio
12:21 queued 36s
created

MeetingsController::update()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 5
Bugs 3 Features 0
Metric Value
c 5
b 3
f 0
dl 0
loc 13
ccs 10
cts 10
cp 1
rs 9.4285
cc 3
eloc 10
nc 2
nop 3
crap 3
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\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 Requests\Request $request
83
     * @param int $groupId
84
     * @param int $meetingId
85
     * @return static
86
     */
87
    public function storeImage(Requests\Request $request, $groupId, $meetingId)
88
    {
89
        $this->validate($request, ['data' => 'required|image']);
90
        $group = Group::findOrFail($groupId);
91
        $this->authorize($group);
92
        $meeting = Group::findOrFail($meetingId);
93
        $this->authorize($meeting);
94
        $file = $request->file('data');
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 = Group::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))
0 ignored issues
show
Unused Code introduced by
The call to Response::__construct() has too many arguments starting with file_get_contents($blank).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Bug introduced by
The method header() does not seem to exist on object<Illuminate\Support\Facades\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...
121
                ->header('Content-Type', 'image/jpeg');
122
        return (new Response($ret, 200))
0 ignored issues
show
Unused Code introduced by
The call to Response::__construct() has too many arguments starting with $ret.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Bug introduced by
The method header() does not seem to exist on object<Illuminate\Support\Facades\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...
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);
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