ModelController::field()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 8
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
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 (Input::all() as $name => $value) {
47
                if ($value != '' && isset($this->fields[$name])) {
48
                    $this->fields[$name]->filter($query, $value);
49
                }
50
            }
51
52
            return $query;
53
        })->with($this->modelObject->getRelations())->orderBy($sort['column'], $sort['direction'])->paginate();
54
55
        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...
56
    }
57
58
    /**
59
     * Todo: rethink the way relations are made
60
     *
61
     * Store a newly created resource in storage.
62
     *
63
     * @return Response
64
     */
65
    public function store()
66
    {
67
        $data = $this->filterInputData();
68
69
        $validator = $this->validator($data);
70
        if ($validator->fails()) {
71
            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'); (Mascame\Artificer\Http\Controllers\ModelController) 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...
72
        }
73
74
        $this->handleData($data);
75
76
        $this->model->guard($this->modelObject->getOption('guarded', array()));
77
78
        $item = $this->model->create(with($this->handleFiles($data)));
79
80
        $relation_on_create = '_set_relation_on_create';
81
        if (Input::has($relation_on_create)) {
82
            $relateds = array(
83
                'id' => $item->id,
84
                'modelClass' => $this->modelObject->class,
85
                'foreign' => Input::get('_set_relation_on_create_foreign')
86
            );
87
88
            Session::push($relation_on_create . '_' . Input::get($relation_on_create), $relateds);
89
        }
90
91
        if (Session::has($relation_on_create . '_' . $this->modelObject->name)) {
92
            $relations = Session::get($relation_on_create . '_' . $this->modelObject->name);
93
94
            foreach ($relations as $relation) {
95
                $related_item = $relation['modelClass']::find($relation['id']);
96
                $related_item->$relation['foreign'] = $item->id;
97
                $related_item->save();
98
            }
99
100
            Session::forget($relation_on_create . '_' . $this->modelObject->name);
101
        }
102
103
        if (Request::ajax()) {
104
            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...
105
        }
106
107
        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...
108
    }
109
110
    /**
111
     * Display the specified resource.
112
     *
113
     * @param  int $id
114
     * @return Response
115
     */
116
    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...
117
    {
118
        $this->handleData($this->model->findOrFail($id));
119
120
        return View::make($this->getView('show'))->with('items', $this->data);
121
    }
122
123
    /**
124
     * Display the specified post.
125
     *
126
     * @return Response
127
     */
128
    public function all($modelName, $data = null, $sort = null)
129
    {
130
        $sort = $this->getSort();
131
132
        $data = $this->model->with($this->modelObject->getRelations())->orderBy($sort['column'],
133
            $sort['direction'])->get();
134
135
        return parent::all($modelName, $data, $sort);
136
    }
137
138
    /**
139
     * Show the form for editing the specified post.
140
     *
141
     * @param  int $id
142
     * @return Response
143
     */
144
    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...
145
    {
146
        $this->handleData($this->model->with($this->modelObject->getRelations())->findOrFail($id));
147
148
        $form = array(
149
            'form_action_route' => 'admin.model.update',
150
            'form_method' => 'put'
151
        );
152
153
        return View::make($this->getView('edit'))->with('items', $this->data)->with($form);
154
    }
155
156
    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...
157
    {
158
        $this->handleData($this->model->with($this->modelObject->getRelations())->findOrFail($id));
159
160
        $this->fields[$field]->showFullField = true;
161
162
        return \HTML::field($this->fields[$field], AdminOption::get('icons'));
163
    }
164
165
    protected function handleAjaxResponse($item)
166
    {
167
        return Response::json(array(
168
                'item' => $item->toArray(),
169
                'refresh' => URL::route('admin.model.field.edit', array(
170
                    'slug' => Input::get('_standalone_origin'),
171
                    'id' => Input::get('_standalone_origin_id'),
172
                    'field' => ':fieldName:'
173
                ))
174
            )
175
        );
176
    }
177
178
    /**
179
     * Update the specified resource in storage.
180
     *
181
     * @param  int $id
182
     * @return Response
183
     */
184
185
    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...
186
    {
187
        $item = $this->model->findOrFail($id);
188
189
        $data = $this->filterInputData();
190
191
        $validator = $this->validator($data);
192
        if ($validator->fails()) {
193
            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); (Mascame\Artificer\Http\Controllers\ModelController) 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...
194
        }
195
196
        $item->update(with($this->handleFiles($data)));
197
198
        if (Request::ajax()) {
199
            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...
200
        }
201
202
        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...
203
    }
204
205
    /**
206
     * Remove the specified resource from storage.
207
     *
208
     * @param  int $id
209
     * @return Response
210
     */
211
    public function destroy($modelName, $id)
212
    {
213
        $event_info = array(
214
            array(
215
                "model" => $modelName,
216
                "id" => $id
217
            )
218
        );
219
220
        Event::fire('artificer.model.before.destroy', $event_info);
221
222
        if ($this->model->destroy($id)) {
223
            Notification::success('<b>Success!</b> The record has been deleted!', true);
224
            Event::fire('artificer.model.after.destroy', $event_info);
225
        } else {
226
            Notification::danger('<b>Failed!</b> The record could not be deleted!');
227
        }
228
229
        if (Request::ajax()) {
230
            // todo
231
            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...
232
        }
233
234
        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...
235
236
//		return Redirect::route('admin.model.all', array('slug' => $this->modelObject->getRouteName()));
237
    }
238
239
}