ProjectController   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 135
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 5

7 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 4 1
A show() 0 15 2
A create() 0 4 1
A store() 0 10 2
A edit() 0 14 1
A update() 0 16 3
A destroy() 0 16 3
1
<?php
2
3
namespace Larafolio\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use Larafolio\Models\Project;
7
use Larafolio\Http\Requests\AddProjectRequest;
8
9
class ProjectController extends Controller
10
{
11
    /**
12
     * Return all projects.
13
     *
14
     * @return \Illuminate\Http\JsonResponse
15
     */
16
    public function index()
17
    {
18
        return response()->json(Project::all());
1 ignored issue
show
Bug introduced by
It seems like \Larafolio\Models\Project::all() targeting Illuminate\Database\Eloquent\Model::all() can also be of type object<Illuminate\Database\Eloquent\Collection>; however, Illuminate\Contracts\Rou...ResponseFactory::json() does only seem to accept string|array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
19
    }
20
21
    /**
22
     * Show an individual project in the manager.
23
     *
24
     * @param string $slug Slug of the project to show.
25
     *
26
     * @return \Illuminate\Http\Response
27
     */
28
    public function show($slug)
29
    {
30
        $project = Project::withBlocks($slug)->first();
31
32
        if (!$project) {
33
            abort(404, "No project with slug {$slug} found.");
34
        }
35
36
        $images = $project->imagesWithProps();
37
38
        return view('larafolio::projects.show', [
39
            'project' => $project,
40
            'images'  => $images,
41
        ]);
42
    }
43
44
    /**
45
     * Return the project create page.
46
     *
47
     * @return \Illuminate\Http\Response
48
     */
49
    public function create()
50
    {
51
        return view('larafolio::projects.add');
52
    }
53
54
    /**
55
     * Add a new project to the portfolio.
56
     *
57
     * @param AddProjectRequest $request Form request.
58
     *
59
     * @return \Illuminate\Http\Response
60
     */
61
    public function store(AddProjectRequest $request)
62
    {
63
        $project = $this->user->addProject($request->all());
64
65
        if ($request->ajax()) {
66
            return response()->json(['project' => $project]);
67
        }
68
69
        return redirect(route('show-project', ['project' => $project]));
70
    }
71
72
    /**
73
     * Return the project edit form view.
74
     *
75
     * @param string $slug Slug for the project to edit.
76
     *
77
     * @return \Illuminate\Http\Response
78
     */
79
    public function edit($slug)
80
    {
81
        $project = Project::full($slug)->first();
82
83
        $nextBlock = $project->blocks->pluck('order')->max() + 1;
84
85
        $nextLink = $project->links->pluck('order')->max() + 1;
86
87
        return view('larafolio::projects.edit', [
88
            'project'   => $project,
89
            'nextBlock' => $nextBlock,
90
            'nextLink'  => $nextLink,
91
        ]);
92
    }
93
94
    /**
95
     * Update a project.
96
     *
97
     * @param \Illuminate\Http\Request $request Request data.
98
     * @param string                   $slug    Slug of project to update.
99
     *
100
     * @return \Illuminate\Http\Response
101
     */
102
    public function update(Request $request, $slug)
103
    {
104
        $project = Project::withTrashed()->where('slug', $slug)->first();
1 ignored issue
show
Bug introduced by
The method withTrashed() does not exist on Larafolio\Models\Project. Did you maybe mean trashed()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
105
106
        if ($project->trashed()) {
107
            $this->user->restoreProject($project);
108
        } else {
109
            $this->user->updateProject($project, $request->all());
110
        }
111
112
        if ($request->ajax()) {
113
            return response()->json(['project' => $project]);
114
        }
115
116
        return redirect(route('show-project', ['project' => $project]));
117
    }
118
119
    /**
120
     * Remove a project from the portfolio.
121
     *
122
     * @param \Illuminate\Http\Request $request Request data.
123
     * @param string                   $slug    Slug of project to remove.
124
     *
125
     * @return \Illuminate\Http\Response
126
     */
127
    public function destroy(Request $request, $slug)
128
    {
129
        $project = Project::withTrashed()->where('slug', $slug)->first();
1 ignored issue
show
Bug introduced by
The method withTrashed() does not exist on Larafolio\Models\Project. Did you maybe mean trashed()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
130
131
        if ($project->trashed()) {
132
            $this->user->purgeProject($project);
133
        } else {
134
            $this->user->removeProject($project);
135
        }
136
137
        if ($request->ajax()) {
138
            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...
139
        }
140
141
        return redirect(route('dashboard'));
142
    }
143
}
144