Completed
Push — master ( 93e03d...77b43e )
by claudio
11:24
created

MeetingsController::showImage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 12
ccs 0
cts 10
cp 0
rs 9.4285
cc 2
eloc 10
nc 2
nop 2
crap 6
1
<?php
2
3
namespace plunner\Http\Controllers\Employees\Meetings;
4
5
use Illuminate\Http\Request;
6
use plunner\Http\Controllers\Controller;
7
use plunner\Http\Requests;
8
use plunner\Meeting;
9
10
class MeetingsController extends Controller
11
{
12 15
    public function __construct()
13
    {
14 15
        config(['auth.model' => \plunner\Employee::class]);
15 15
        config(['jwt.user' => \plunner\Employee::class]);
16 15
        $this->middleware('jwt.authandrefresh:mode-en');
17 15
    }
18
19
    /**
20
     * Display a listing of the resource.
21
     *
22
     * @param Request $request needed for get query to get only current planed meetings
23
     * @return \Illuminate\Http\Response
24
     */
25 6
    public function index(Request $request)
26
    {
27 6
        $employee = \Auth::user();
28 6
        $meetings = $employee->meetings();
29 6
        if ($request->query('current'))
30 3
            $meetings->where(function ($query) { //parenthesis for conditions ...(C1 OR C2)...
31 3
                $query->where('start_time', '=', NULL);//to be planned
32
                //datetime to consider timezone, don't use mysql NOW()
33 3
                $query->orWhere('start_time', '>=', new \DateTime());//planned
34 3
            });
35 6
        return $meetings->get();
36
    }
37
38
    /**
39
     * Display the specified resource.
40
     *
41
     * @param  int $id
42
     * @return \Illuminate\Http\Response
43
     */
44 6
    public function show($id)
45
    {
46 6
        $meeting = Meeting::findOrFail($id);
47 6
        $this->authorize($meeting);
48 3
        return $meeting;
49
    }
50
51
    /**
52
     * Store a newly created resource in storage.
53
     *
54
     * @param int $groupId
55
     * @param int $meetingId
56
     * @return static
57
     */
58
    public function showImage($groupId, $meetingId)
59
    {
60
        $group = Group::findOrFail($groupId);
61
        $this->authorize($group);
62
        $meeting = Group::findOrFail($meetingId);
63
        $this->authorize($meeting);
64
        $ret = self::getImg($meeting);
65
        if ($ret === false)
66
            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...ings\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...
67
        return (new Response($ret, 200))
68
            ->header('Content-Type', 'image/jpeg');
69
    }
70
71
    private static function getImg(Meeting $meeting)
72
    {
73
        $name = 'meetings/' . $meeting->id;
74
        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...
75
            return false;
76
        return \Storage::get($name);
77
    }
78
}
79