Completed
Push — dev ( 82e8fe...02e08a )
by Zach
03:24
created

PortfolioController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 63
rs 10
c 1
b 0
f 0
wmc 5
lcom 1
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 22 1
A update() 0 10 3
A dashboardIcons() 0 9 1
1
<?php
2
3
namespace Larafolio\Http\Controllers;
4
5
use Larafolio\Models\Page;
6
use Illuminate\Http\Request;
7
use Larafolio\Models\Project;
8
use Larafolio\Models\UserTraits\updatePageOrder;
9
10
class PortfolioController extends Controller
11
{
12
    /**
13
     * Show the manager dashboard.
14
     *
15
     * @return \Illuminate\Http\Response
16
     */
17
    public function index()
18
    {
19
        $projects = Project::all()->sortBy('order')->values();
20
21
        $projectImages = $projects->mapWithKeys(function (Project $project) {
22
            return [$project->name() => $project->getProjectImageUrl()];
23
        })->objectIfEmpty();
24
25
        $projectBlocks = $projects->mapWithKeys(function (Project $project) {
26
            return [$project->name() => $project->getProjectBlockText()];
27
        })->objectIfEmpty();
28
29
        $pages = Page::all()->sortBy('order')->values();
30
31
        return view('larafolio::projects.index', [
32
            'projects' => $projects,
33
            'projectImages'   => $projectImages,
34
            'projectBlocks'   => $projectBlocks,
35
            'pages' => $pages,
36
            'icons' => $this->dashboardIcons(),
37
        ]);
38
    }
39
40
    /**
41
     * Update project order in portfolio.
42
     *
43
     * @param Request $request Request data containing all projects.
44
     *
45
     * @return \Illuminate\Http\Response
46
     */
47
    public function update(Request $request)
48
    {
49
        if ($request->has('projects')) {
50
            $updated = $this->user->updateProjectOrder($request->get('projects'));
51
        } else if ($request->has('pages')) {
52
            $updated = $this->user->updatePageOrder($request->get('pages'));
53
        }
54
        
55
        return response()->json($updated);
0 ignored issues
show
Bug introduced by
The variable $updated does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
56
    }
57
58
    /**
59
     * Icons needed for dashboard.
60
     *
61
     * @return array
62
     */
63
    protected function dashboardIcons()
64
    {
65
        return [
66
            'down' => file_get_contents(public_path('vendor/larafolio/zondicons/arrow-thin-down.svg')),
67
            'up' => file_get_contents(public_path('vendor/larafolio/zondicons/arrow-thin-up.svg')),
68
            'hidden' => file_get_contents(public_path('vendor/larafolio/zondicons/view-hide.svg')),
69
            'visible' => file_get_contents(public_path('vendor/larafolio/zondicons/view-show.svg'))
70
        ];
71
    }
72
}
73