Completed
Push — dev ( 061f38...c1e495 )
by Marc
02:36
created

ModelController::handleAjaxResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 1
eloc 7
nc 1
nop 1
1
<?php namespace Mascame\Artificer\Http\Controllers;
2
3
use Event;
4
use Illuminate\Database\Eloquent\Collection;
5
use Input;
6
use Mascame\Artificer\Options\AdminOption;
7
use Redirect;
8
use Request;
9
use Response;
10
use Session;
11
use URL;
12
use View;
13
14
class ModelController extends BaseModelController
15
{
16
17
    /**
18
     * Show the form for creating a new resource.
19
     *
20
     * @return Response
21
     */
22
    public function create()
23
    {
24
        $this->handleData($this->modelObject->schema->getInstance());
25
26
        $form = array(
27
            'form_action_route' => 'admin.model.store',
28
            'form_method' => 'post'
29
        );
30
31
        return View::make($this->getView('edit'))->with('items', $this->data)->with($form);
32
    }
33
34
    /**
35
     * @param $modelName
36
     * @return $this
37
     */
38
    public function filter($modelName)
39
    {
40
        $this->handleData($this->model->firstOrFail());
41
42
        $sort = $this->getSort();
43
44
        $data = $this->model->where(function ($query) {
45
46
            foreach (\Request::all() as $name => $value) {
47
                if ($value != '' && isset($this->fields[$name])) {
48
                    $this->fields[$name]->filter($query, $value);
49
                }
50
            }
51
52
            return null;
53
        })
54
            ->with($this->modelObject->getRelations())
55
            ->orderBy($sort['column'], $sort['direction'])
56
            ->get();
57
58
        return parent::all($modelName, $data, $sort);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (all() instead of filter()). Are you sure this is correct? If so, you might want to change this to $this->all().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
59
    }
60
61
    /**
62
     * Todo: rethink the way relations are made
63
     *
64
     * Store a newly created resource in storage.
65
     *
66
     * @return Response
67
     */
68
    public function store()
69
    {
70
        $data = $this->filterInputData();
71
72
        $validator = $this->validator($data);
73
74
        if ($validator->fails()) {
75
            return $this->redirect($validator, 'admin.model.create');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->redirect($... 'admin.model.create'); (redirect) is incompatible with the return type documented by Mascame\Artificer\Http\C...\ModelController::store of type Illuminate\Contracts\Routing\ResponseFactory.

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...
76
        }
77
78
        $this->handleData($data);
79
80
        $this->model->guard($this->modelObject->getGuarded());
81
        $this->model->fillable($this->modelObject->getOption('fillable', []));
82
83
        $item = $this->model->create(with($this->handleFiles($data)));
84
85
        if (Request::ajax()) {
86
            return $this->handleAjaxResponse($item);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->handleAjaxResponse($item); (Illuminate\Http\JsonResponse) is incompatible with the return type documented by Mascame\Artificer\Http\C...\ModelController::store of type Illuminate\Contracts\Routing\ResponseFactory.

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...
87
        }
88
89
        return Redirect::route('admin.model.all', array('slug' => $this->modelObject->getRouteName()));
0 ignored issues
show
Bug introduced by
The method route() does not seem to exist on object<redirect>.

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...
90
    }
91
92
    /**
93
     * Display the specified resource.
94
     *
95
     * @param  int $id
96
     * @return Response
97
     */
98
    public function show($modelName, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $modelName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
99
    {
100
        $this->handleData($this->model->findOrFail($id));
101
102
        return View::make($this->getView('show'))->with('item', $this->data);
103
    }
104
105
    /**
106
     * Display the specified post.
107
     *
108
     * @return Response
109
     */
110
    public function all($modelName, $data = null, $sort = null)
111
    {
112
        $sort = $this->getSort();
113
114
        $data = $this->model->with($this->modelObject->getRelations())->orderBy($sort['column'],
115
            $sort['direction'])->get();
116
117
        return parent::all($modelName, $data, $sort);
118
    }
119
120
    /**
121
     * Show the form for editing the specified post.
122
     *
123
     * @param  int $id
124
     * @return Response
125
     */
126
    public function edit($modelName, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $modelName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
127
    {
128
        $this->handleData(
129
            $this->model->with($this->modelObject->getRelations())->findOrFail($id)
130
        );
131
132
        $form = array(
133
            'form_action_route' => 'admin.model.update',
134
            'form_method' => 'put'
135
        );
136
137
        return View::make($this->getView('edit'))
138
            ->with('items', $this->data)
139
            ->with($form);
140
    }
141
142
    public function field($modelName, $id, $field)
0 ignored issues
show
Unused Code introduced by
The parameter $modelName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
143
    {
144
        $this->handleData($this->model->with($this->modelObject->getRelations())->findOrFail($id));
145
146
        $this->fields[$field]->showFullField = true;
147
148
        return \HTML::field($this->fields[$field], AdminOption::get('icons'));
149
    }
150
151
    protected function handleAjaxResponse($item)
152
    {
153
        return Response::json(array(
154
                'item' => $item->toArray(),
155
                'refresh' => URL::route('admin.model.field.edit', array(
156
                    'slug' => Input::get('_standalone_origin'),
157
                    'id' => Input::get('_standalone_origin_id'),
158
                    'field' => ':fieldName:'
159
                ))
160
            )
161
        );
162
    }
163
164
    /**
165
     * Update the specified resource in storage.
166
     *
167
     * @param  int $id
168
     * @return Response
169
     */
170
171
    public function update($modelName, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $modelName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
172
    {
173
        $item = $this->model->findOrFail($id);
174
175
        $data = $this->filterInputData();
176
177
        $validator = $this->validator($data);
178
        if ($validator->fails()) {
179
            return $this->redirect($validator, 'admin.model.edit', $id);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->redirect($...dmin.model.edit', $id); (redirect) is incompatible with the return type documented by Mascame\Artificer\Http\C...ModelController::update of type Illuminate\Contracts\Routing\ResponseFactory.

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...
180
        }
181
182
        $item->update(with($this->handleFiles($data)));
183
184
        if (Request::ajax()) {
185
            return $this->handleAjaxResponse($item);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->handleAjaxResponse($item); (Illuminate\Http\JsonResponse) is incompatible with the return type documented by Mascame\Artificer\Http\C...ModelController::update of type Illuminate\Contracts\Routing\ResponseFactory.

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...
186
        }
187
188
        return Redirect::route('admin.model.all', array('slug' => $this->modelObject->getRouteName()));
0 ignored issues
show
Bug introduced by
The method route() does not seem to exist on object<redirect>.

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...
189
    }
190
191
    /**
192
     * Remove the specified resource from storage.
193
     *
194
     * @param  int $id
195
     * @return Response
196
     */
197
    public function destroy($modelName, $id)
198
    {
199
        $event_info = array(
200
            array(
201
                "model" => $modelName,
202
                "id" => $id
203
            )
204
        );
205
206
        Event::fire('artificer.model.before.destroy', $event_info);
207
208
        if ($this->model->destroy($id)) {
209
            Notification::success('<b>Success!</b> The record has been deleted!', true);
210
            Event::fire('artificer.model.after.destroy', $event_info);
211
        } else {
212
            Notification::danger('<b>Failed!</b> The record could not be deleted!');
213
        }
214
215
        if (Request::ajax()) {
216
            // todo
217
            return Response::json(array());
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Response::json(array()); (Illuminate\Http\JsonResponse) is incompatible with the return type documented by Mascame\Artificer\Http\C...odelController::destroy of type Illuminate\Contracts\Routing\ResponseFactory.

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...
218
        }
219
220
        return Redirect::back();
0 ignored issues
show
Bug introduced by
The method back() does not seem to exist on object<redirect>.

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...
221
222
//		return Redirect::route('admin.model.all', array('slug' => $this->modelObject->getRouteName()));
223
    }
224
225
}