Completed
Push — master ( 171615...93e03d )
by claudio
15:47
created

MeetingsController::getImg()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 5
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 6
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
        if ($ret === false)
119
            return \Response::json(['error' => 'The file is not uploaded'], 404);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Response::json(a...s not uploaded'), 404); (Illuminate\Http\JsonResponse) is incompatible with the return type documented by plunner\Http\Controllers...gsController::showImage of type plunner\Http\Controllers...ners\MeetingsController.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
120
        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...
121
            ->header('Content-Type', 'image/jpeg');
122
    }
123
124
    private static function getImg(Meeting $meeting)
125
    {
126
        $name = 'meetings/' . $meeting->id;
127
        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...
128
            return false;
129
        return \Storage::get($name);
130
    }
131
132
    /**
133
     * Update the specified resource in storage.
134
     *
135
     * @param MeetingRequest $request
136
     * @param int $meetingId
137
     * @param int $groupId
138
     * @return mixed
139
     */
140 9
    public function update(MeetingRequest $request, $groupId, $meetingId)
141
    {
142 9
        $group = Group::findOrFail($groupId);
143 9
        $this->authorize($group);
144 6
        $meeting = Meeting::findOrFail($meetingId);
145 3
        $this->authorize($meeting);
146 3
        $input = $request->all();
147
        //the planner cannot modify the duration of a planed meeting
148 3
        if ($meeting->start_time != NULL && $meeting->duration != $input['duration'])
149 2
            return Response::json(['error' => 'the meeting is already planned, you cannot change the duration'], 422);
150 3
        $meeting->update($input);
151 3
        return $meeting;
152
    }
153
154
    /**
155
     * Remove the specified resource from storage.
156
     *
157
     * @param int $groupId
158
     * @param int $meetingId
159
     * @return mixed
160
     */
161 9
    public function destroy($groupId, $meetingId)
162
    {
163 9
        $group = Group::findOrFail($groupId);
164 9
        $this->authorize($group);
165 6
        $meeting = Meeting::findOrFail($meetingId);
166 3
        $this->authorize($meeting);
167 3
        $meeting->delete();
168 3
        return $meeting;
169
    }
170
}
171