Completed
Push — dev ( 917cb5...992eab )
by Marc
10:46
created

ModelController::store()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 44
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

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