Completed
Push — dev ( d311da...db57f8 )
by Zach
03:29
created

ContentCrud::update()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 4
nop 3
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Larafolio\Http\Content;
4
5
use Illuminate\Http\Request;
6
use Larafolio\Models\HasContent;
7
use Illuminate\Support\Collection;
8
use Larafolio\Http\Requests\AddResourceRequest;
9
10
class ContentCrud
11
{
12
    /**
13
     * Return all.
14
     *
15
     * @param Collection $collection Colelction of all resources.
16
     *
17
     * @return \Illuminate\Http\JsonResponse
18
     */
19
    public function index(Collection $collection)
20
    {
21
        return response()->json($collection);
22
    }
23
24
    /**
25
     * Show an individual resource in the manager.
26
     *
27
     * @param Larafolio\Models\HasContent $model Resource to show.
28
     * @param string                      $type  Type of resource (page, project etc.).
29
     *
30
     * @return \Illuminate\Http\Response
31
     */
32
    public function show(HasContent $model, $type)
33
    {
34
        $images = $model->imagesWithProps();
35
36
        return view($this->makeRoute('show', $type), [
37
            $type => $model,
38
            'images' => $images,
39
        ]);
40
    }
41
42
    /**
43
     * Return the create resource page.
44
     *
45
     * @param string $type Type of resource (page, project etc.).
46
     *
47
     * @return \Illuminate\Http\Response
48
     */
49
    public function create($type)
50
    {
51
        return view($this->makeRoute('add', $type));
52
    }
53
54
    /**
55
     * Add a new resource to the portfolio.
56
     *
57
     * @param Larafolio\Http\Requests\AddResourceRequest $request Form request.
58
     * @param User                                       $user    User object.
59
     * @param string                                     $type    Type of resource (page, project etc.).
60
     *
61
     * @return \Illuminate\Http\Response
62
     */
63
    public function store(AddResourceRequest $request, $user, $type)
64
    {
65
        $addMethod = $this->makeMethod('add', $type);
66
67
        $model = $user->{$addMethod}($request->all());
68
69
        if ($request->ajax()) {
70
            return response()->json([$type => $model]);
71
        }
72
73
        return redirect(route("show-{$type}", [$type => $model]));
74
    }
75
76
    /**
77
     * Return the resource edit form view.
78
     *
79
     * @param Larafolio\Models\HasContent $model Resource to show edit form for.
80
     *
81
     * @return \Illuminate\Http\Response
82
     */
83
    public function edit(HasContent $model)
84
    {
85
        $type = $this->getTypeFromModel($model);
86
87
        $nextBlock = $model->blocks->pluck('order')->max() + 1;
1 ignored issue
show
Documentation introduced by
The property blocks does not exist on object<Larafolio\Models\HasContent>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
88
89
        $nextLink = $model->links->pluck('order')->max() + 1;
1 ignored issue
show
Documentation introduced by
The property links does not exist on object<Larafolio\Models\HasContent>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
90
91
        return view($this->makeRoute('edit', $type), [
92
            $type => $model,
93
            'nextBlock' => $nextBlock,
94
            'nextLink' => $nextLink,
95
        ]);
96
    }
97
98
    /**
99
     * Update a resource.
100
     *
101
     * @param \Illuminate\Http\Request    $request Request data.
102
     * @param Larafolio\Models\HasContent $model   Resource to update.
103
     * @param User                        $user    User object.
104
     *
105
     * @return \Illuminate\Http\Response
106
     */
107
    public function update(Request $request, HasContent $model, $user)
108
    {
109
        $type = $this->getTypeFromModel($model);
110
111
        if ($model->trashed()) {
112
            $restoreMethod = $this->makeMethod('restore', $type);
113
114
            $user->{$restoreMethod}($model);
115
        } else {
116
            $updateMethod = $this->makeMethod('update', $type);
117
118
            $user->{$updateMethod}($model, $request->all());
119
        }
120
121
        if ($request->ajax()) {
122
            return response()->json([$type => $model]);
123
        }
124
125
        return redirect(route("show-{$type}", [$type => $model]));
126
    }
127
128
    /**
129
     * Remove a resource from the portfolio.
130
     *
131
     * @param \Illuminate\Http\Request    $request Request data.
132
     * @param Larafolio\Models\HasContent $model   Resource to update.
133
     * @param User                        $user    User object.
134
     *
135
     * @return \Illuminate\Http\Response
136
     */
137
    public function destroy(Request $request, HasContent $model, $user)
138
    {
139
        $type = $this->getTypeFromModel($model);
140
141
        if ($model->trashed()) {
142
            $purgeMethod = $this->makeMethod('purge', $type);
143
144
            $user->{$purgeMethod}($model);
145
        } else {
146
            $removeMethod = $this->makeMethod('remove', $type);
147
148
            $user->{$removeMethod}($model);
149
        }
150
151
        if ($request->ajax()) {
152
            return response()->json(true);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
153
        }
154
155
        return redirect(route('dashboard'));
156
    }
157
158
    /**
159
     * Make HasContent method.
160
     *
161
     * @param  string $verb Action to perform.
162
     * @param  string $type Name of resource.
163
     *
164
     * @return string
165
     */
166
    protected function makeMethod($verb, $type)
167
    {
168
        return $verb.ucfirst($type);
169
    }
170
171
    /**
172
     * Make route name.
173
     *
174
     * @param  string $verb Route action to perform.
175
     * @param  string $type Name of resource.
176
     *
177
     * @return string
178
     */
179
    protected function makeRoute($verb, $type)
180
    {
181
        return "larafolio::{$type}s.{$verb}";
182
    }
183
184
    /**
185
     * Get the model type (short class name) from the model.
186
     *
187
     * @param  HasContent $model Model to get name of.
188
     *
189
     * @return string
190
     */
191
    protected function getTypeFromModel(HasContent $model)
192
    {
193
        $reflection = new \ReflectionClass($model);
194
195
        return lcfirst($reflection->getShortName());
196
    }
197
}
198