Completed
Push — master ( 5c0fa0...21c269 )
by Manu
02:15
created

StudiesController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
namespace Scool\Inventory\Http\Controllers;
3
use Prettus\Validator\Contracts\ValidatorInterface;
4
use Prettus\Validator\Exceptions\ValidatorException;
5
use Scool\Inventory\Http\Requests\StudyCreateRequest;
6
use Scool\Inventory\Http\Requests\StudyUpdateRequest;
7
use Scool\Inventory\Repositories\StudyRepository;
8
use Scool\Inventory\Validators\StudyValidator;
9
10
/**
11
 * Class StudiesController
12
 * @package Scool\Inventory\Http\Controllers
13
 */
14
class StudiesController extends Controller
15
{
16
    /**
17
     * @var StudyRepository
18
     */
19
    protected $repository;
20
    /**
21
     * @var StudyValidator
22
     */
23
    protected $validator;
24
    public function __construct(StudyRepository $repository, StudyValidator $validator)
25
    {
26
        $this->repository = $repository;
27
        $this->validator  = $validator;
28
    }
29
    /**
30
     * Display a listing of the resource.
31
     *
32
     * @return \Illuminate\Http\Response
33
     */
34 View Code Duplication
    public function index()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
    {
36
        $this->repository->pushCriteria(app('Prettus\Repository\Criteria\RequestCriteria'));
37
        $studies = $this->repository->all();
38
        if (request()->wantsJson()) {
39
            return response()->json([
40
                'data' => $studies,
41
            ]);
42
        }
43
        return view('studies.index', compact('studies'));
44
    }
45
    /**
46
     * Store a newly created resource in storage.
47
     *
48
     * @param  StudyCreateRequest $request
49
     *
50
     * @return \Illuminate\Http\Response
51
     */
52 View Code Duplication
    public function store(StudyCreateRequest $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54
        try {
55
            $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_CREATE);
56
            $study = $this->repository->create($request->all());
57
            $response = [
58
                'message' => 'Study created.',
59
                'data'    => $study->toArray(),
60
            ];
61
            if ($request->wantsJson()) {
62
                return response()->json($response);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return response()->json($response); (Illuminate\Http\JsonResponse) is incompatible with the return type documented by Scool\Inventory\Http\Con...tudiesController::store 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...
63
            }
64
            return redirect()->back()->with('message', $response['message']);
65
        } catch (ValidatorException $e) {
0 ignored issues
show
Bug introduced by
The class Prettus\Validator\Exceptions\ValidatorException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
66
            if ($request->wantsJson()) {
67
                return response()->json([
0 ignored issues
show
Bug Best Practice introduced by
The return type of return response()->json(... $e->getMessageBag())); (Illuminate\Http\JsonResponse) is incompatible with the return type documented by Scool\Inventory\Http\Con...tudiesController::store 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...
68
                    'error'   => true,
69
                    'message' => $e->getMessageBag()
70
                ]);
71
            }
72
            return redirect()->back()->withErrors($e->getMessageBag())->withInput();
73
        }
74
    }
75
    /**
76
     * Display the specified resource.
77
     *
78
     * @param  int $id
79
     *
80
     * @return \Illuminate\Http\Response
81
     */
82 View Code Duplication
    public function show($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84
        $study = $this->repository->find($id);
85
        if (request()->wantsJson()) {
86
            return response()->json([
87
                'data' => $study,
88
            ]);
89
        }
90
        return view('studies.show', compact('study'));
91
    }
92
    /**
93
     * Show the form for editing the specified resource.
94
     *
95
     * @param  int $id
96
     *
97
     * @return \Illuminate\Http\Response
98
     */
99
    public function edit($id)
100
    {
101
        $study = $this->repository->find($id);
102
        return view('studies.edit', compact('study'));
103
    }
104
    /**
105
     * Update the specified resource in storage.
106
     *
107
     * @param  StudyUpdateRequest $request
108
     * @param  string            $id
109
     *
110
     * @return Response
111
     */
112 View Code Duplication
    public function update(StudyUpdateRequest $request, $id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
    {
114
        try {
115
            $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_UPDATE);
116
            $study = $this->repository->update($id, $request->all());
117
            $response = [
118
                'message' => 'Study updated.',
119
                'data'    => $study->toArray(),
120
            ];
121
            if ($request->wantsJson()) {
122
                return response()->json($response);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return response()->json($response); (Illuminate\Http\JsonResponse) is incompatible with the return type documented by Scool\Inventory\Http\Con...udiesController::update of type Scool\Inventory\Http\Controllers\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...
123
            }
124
            return redirect()->back()->with('message', $response['message']);
125
        } catch (ValidatorException $e) {
0 ignored issues
show
Bug introduced by
The class Prettus\Validator\Exceptions\ValidatorException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
126
            if ($request->wantsJson()) {
127
                return response()->json([
0 ignored issues
show
Bug Best Practice introduced by
The return type of return response()->json(... $e->getMessageBag())); (Illuminate\Http\JsonResponse) is incompatible with the return type documented by Scool\Inventory\Http\Con...udiesController::update of type Scool\Inventory\Http\Controllers\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...
128
                    'error'   => true,
129
                    'message' => $e->getMessageBag()
130
                ]);
131
            }
132
            return redirect()->back()->withErrors($e->getMessageBag())->withInput();
133
        }
134
    }
135
    /**
136
     * Remove the specified resource from storage.
137
     *
138
     * @param  int $id
139
     *
140
     * @return \Illuminate\Http\Response
141
     */
142
    public function destroy($id)
143
    {
144
        $deleted = $this->repository->delete($id);
145
        if (request()->wantsJson()) {
146
            return response()->json([
0 ignored issues
show
Bug Best Practice introduced by
The return type of return response()->json(...deleted' => $deleted)); (Illuminate\Http\JsonResponse) is incompatible with the return type documented by Scool\Inventory\Http\Con...diesController::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...
147
                'message' => 'Study deleted.',
148
                'deleted' => $deleted,
149
            ]);
150
        }
151
        return redirect()->back()->with('message', 'Study deleted.');
152
    }
153
}