Completed
Push — master ( 69b104...4c9995 )
by Arjay
13:44
created

ConfigurationsController::show()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Yajra\CMS\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use Yajra\CMS\Entities\Configuration;
7
8
/**
9
 * Class SiteConfigurationController
10
 *
11
 * @package Yajra\CMS\Controllers
12
 */
13
class ConfigurationsController extends Controller
14
{
15
    /**
16
     * Limit the config results based on given key.
17
     *
18
     * @var array
19
     */
20
    protected $limits = [
21
        'site'     => ['name', 'version', 'keywords', 'author', 'description', 'admin_theme', 'registration'],
22
        'app'      => ['name', 'debug', 'debugbar', 'env', 'locale', 'log', 'timezone', 'url'],
23
        'database' => ['default', 'connections', 'migrations', 'redis'],
24
    ];
25
26
    /**
27
     * Site configuration setup page.
28
     *
29
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
30
     */
31
    public function index()
32
    {
33
        $config = $this->setupConfig();
34
35
        return view('administrator.configuration.index', compact('config'));
36
    }
37
38
    /**
39
     * Get configuration setup.
40
     *
41
     * @return \Illuminate\Support\Collection
42
     */
43
    protected function setupConfig()
44
    {
45
        return collect([
46
            [
47
                'title' => 'Site Management',
48
                'key'   => 'site',
49
                'icon'  => 'fa-cog',
50
            ],
51
            [
52
                'title' => 'Application Environment',
53
                'key'   => 'app',
54
                'icon'  => 'fa-laptop',
55
            ],
56
            [
57
                'title' => 'Database Connection',
58
                'key'   => 'database',
59
                'icon'  => 'fa-database',
60
            ],
61
            [
62
                'title' => 'Mail Driver',
63
                'key'   => 'mail',
64
                'icon'  => 'fa-envelope',
65
            ],
66
            [
67
                'title' => 'Cache Store',
68
                'key'   => 'cache',
69
                'icon'  => 'fa-cloud-download',
70
            ],
71
            [
72
                'title' => 'Session Driver',
73
                'key'   => 'session',
74
                'icon'  => 'fa-comment-o',
75
            ],
76
            [
77
                'title' => 'File System',
78
                'key'   => 'filesystems',
79
                'icon'  => 'fa-briefcase',
80
            ],
81
        ]);
82
    }
83
84
    /**
85
     * Store submitted configurations.
86
     *
87
     * @param \Illuminate\Http\Request $request
88
     */
89
    public function store(Request $request)
90
    {
91
        $config = $request->input('config');
92
93
        foreach (array_dot($request->input('data')) as $key => $value) {
0 ignored issues
show
Bug introduced by
It seems like $request->input('data') targeting Illuminate\Http\Request::input() can also be of type string; however, array_dot() does only seem to accept 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...
94
            $path = $config . '.' . $key;
95
            $this->destroy($path);
96
            if (is_array($value)) {
97
                $value = implode(',', $value);
98
            }
99
            Configuration::create(['key' => $path, 'value' => $value]);
100
        }
101
102
        return $this->notifySuccess("Configuation ($config) successfully saved! You may need to refresh the page to reflect some changes.");
103
    }
104
105
    /**
106
     * Remove configuration by key.
107
     *
108
     * @param string $key
109
     * @return Configuration
110
     */
111
    public function destroy($key)
112
    {
113
        return Configuration::where('key', $key)->delete();
114
    }
115
116
    /**
117
     * Show configuration by key.
118
     *
119
     * @param string $key
120
     * @return array|mixed
121
     */
122
    public function show($key)
123
    {
124
        $config = config($key) ?? [];
125
126
        if (! isset($this->limits[$key])) {
127
            return $config;
128
        }
129
130
        return response()->json(array_only($config, $this->limits[$key]));
131
    }
132
}
133