Completed
Push — master ( 7a1e6c...8794c3 )
by Arjay
11:28
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\DataTables\FileAssetsDataTable;
6
use Yajra\CMS\Entities\Configuration;
7
use App\Http\Requests;
8
use Illuminate\Http\Request;
9
10
/**
11
 * Class SiteConfigurationController
12
 *
13
 * @package Yajra\CMS\Controllers
14
 */
15
class SiteConfigurationController extends Controller
16
{
17
    /**
18
     * Site configuration setup page.
19
     *
20
     * @param \Yajra\CMS\Entities\Configuration $configuration
21
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
22
     */
23
    public function index(Configuration $configuration)
24
    {
25
        return view('administrator.configuration.index', compact('configuration'));
26
    }
27
28
    /**
29
     * Store submitted configurations.
30
     *
31
     * @param \Illuminate\Http\Request $request
32
     */
33
    public function store(Request $request)
34
    {
35
        $cleanArray = $request->all();
36
        $array      = ['config' => $cleanArray['config']] + $cleanArray; // set config array value to first.
37
38
        $this->storeConfiguration($array);
39
    }
40
41
    /**
42
     * Run submitted configurations.
43
     *
44
     * @param array $array
45
     */
46
    protected function storeConfiguration($array)
47
    {
48
        foreach ($array as $key => $value) {
49
            if ($key == 'config') {
50
                $setKey = $value;
51
            } else {
52
                $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...
53
                $this->removeByKey($cleanKey);
54
                Configuration::create(['key' => $cleanKey, 'value' => $value]);
55
            }
56
        }
57
    }
58
59
    /**
60
     * Remove configuration by key.
61
     *
62
     * @param string $key
63
     * @return Configuration
64
     */
65
    protected function removeByKey($key)
66
    {
67
        return Configuration::where('key', $key)->delete();
68
    }
69
70
    /**
71
     * Show all site assets.
72
     *
73
     * @param \Yajra\CMS\DataTables\FileAssetsDataTable $dataTable
74
     * @return \Illuminate\Http\JsonResponse
75
     */
76
    public function getAssets(FileAssetsDataTable $dataTable)
77
    {
78
        return $dataTable->ajax();
79
    }
80
}
81