Completed
Push — master ( 8794c3...3fe63c )
by Arjay
15:51
created

SiteConfigurationController::getAssets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
cc 1
eloc 2
nc 1
nop 1
rs 10
1
<?php
2
3
namespace Yajra\CMS\Http\Controllers;
4
5
use Yajra\CMS\Entities\Configuration;
6
use App\Http\Requests;
7
use Illuminate\Http\Request;
8
9
/**
10
 * Class SiteConfigurationController
11
 *
12
 * @package Yajra\CMS\Controllers
13
 */
14
class SiteConfigurationController extends Controller
15
{
16
    /**
17
     * Site configuration setup page.
18
     *
19
     * @param \Yajra\CMS\Entities\Configuration $configuration
20
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
21
     */
22
    public function index(Configuration $configuration)
23
    {
24
        return view('administrator.configuration.index', compact('configuration'));
25
    }
26
27
    /**
28
     * Store submitted configurations.
29
     *
30
     * @param \Illuminate\Http\Request $request
31
     */
32
    public function store(Request $request)
33
    {
34
        $cleanArray = $request->all();
35
        $array      = ['config' => $cleanArray['config']] + $cleanArray; // set config array value to first.
36
37
        $this->storeConfiguration($array);
38
    }
39
40
    /**
41
     * Run submitted configurations.
42
     *
43
     * @param array $array
44
     */
45
    protected function storeConfiguration($array)
46
    {
47
        foreach ($array as $key => $value) {
48
            if ($key == 'config') {
49
                $setKey = $value;
50
            } else {
51
                $cleanKey = $setKey . '.' . str_replace('AAA', '.', $key); // remove js replacement string.
0 ignored issues
show
Bug introduced by
The variable $setKey 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...
52
                $this->removeByKey($cleanKey);
53
                Configuration::create(['key' => $cleanKey, 'value' => $value]);
54
            }
55
        }
56
    }
57
58
    /**
59
     * Remove configuration by key.
60
     *
61
     * @param string $key
62
     * @return Configuration
63
     */
64
    protected function removeByKey($key)
65
    {
66
        return Configuration::where('key', $key)->delete();
67
    }
68
}
69