Completed
Push — master ( 7949b1...be02a5 )
by Nick
07:10
created

GenericResourceController::show()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace EloquentJs\Laravel\Controllers;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Http\Request;
7
use Illuminate\Routing\Controller;
8
9
class GenericResourceController extends Controller
10
{
11
    /**
12
     * @var Model
13
     */
14
    protected $model;
15
16
    /**
17
     * Set the model for this resource controller.
18
     *
19
     * @param Model $model
20
     */
21
    public function setModel(Model $model)
22
    {
23
        $this->model = $model;
24
    }
25
26
    /**
27
     * Display a listing of the resource.
28
     *
29
     * @return \Illuminate\Http\Response
30
     */
31
    public function index()
32
    {
33
        return $this->model->useEloquentJs()->get();
34
    }
35
36
    /**
37
     * Store a newly created resource in storage.
38
     *
39
     * @param  \Illuminate\Http\Request  $request
40
     * @return Model
41
     */
42
    public function store(Request $request)
43
    {
44
        return $this->model->create($request->all());
45
    }
46
47
    /**
48
     * Display the specified resource.
49
     *
50
     * @param  int  $id
51
     * @return \Illuminate\Http\Response
52
     */
53
    public function show($id)
54
    {
55
        return $this->model->useEloquentJs()->find($id);
56
    }
57
58
    /**
59
     * Update the specified resource in storage.
60
     *
61
     * @param  \Illuminate\Http\Request  $request
62
     * @param  int  $id
63
     * @return \Illuminate\Http\Response
64
     */
65
    public function update(Request $request, $id)
66
    {
67
        if ($id === '*') {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of $id (integer) and '*' (string) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
68
            return $this->model->newQuery()->useEloquentJs()->update($request->all());
69
        }
70
71
        $resource = $this->model->findOrFail($id);
72
73
        $resource->update($request->all());
74
75
        return $resource;
76
    }
77
78
    /**
79
     * Remove the specified resource from storage.
80
     *
81
     * @param  int  $id
82
     * @return \Illuminate\Http\Response
83
     */
84
    public function destroy($id)
85
    {
86
        if ($id === '*') {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of $id (integer) and '*' (string) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
87
            return $this->model->newQuery()->useEloquentJs()->delete();
88
        }
89
90
        $resource = $this->model->findOrFail($id);
91
92
        return ['success' => $resource->delete()];
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('success' => $resource->delete()); (array) is incompatible with the return type documented by EloquentJs\Laravel\Contr...urceController::destroy of type Illuminate\Http\Response.

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...
93
    }
94
}
95