Completed
Push — master ( a3d07b...f35753 )
by Arjay
14:10
created

UtilitiesController::rebuildCategory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 6
rs 9.4285
1
<?php
2
3
namespace Yajra\CMS\Http\Controllers;
4
5
use App\Http\Requests;
6
use Collective\Html\HtmlBuilder;
7
use Illuminate\Contracts\Console\Kernel;
8
use Illuminate\Contracts\Logging\Log;
9
use Illuminate\Http\Request;
10
use Illuminate\Support\Facades\File;
11
use Rap2hpoutre\LaravelLogViewer\LaravelLogViewer;
12
use Yajra\CMS\Entities\Category;
13
use Yajra\CMS\Entities\Menu;
14
use Yajra\Datatables\Datatables;
15
16
class UtilitiesController extends Controller
17
{
18
    /**
19
     * @var \Illuminate\Contracts\Console\Kernel
20
     */
21
    protected $command;
22
23
    /**
24
     * @var \Collective\Html\HtmlBuilder
25
     */
26
    protected $html;
27
28
    /**
29
     * @var \Illuminate\Contracts\Logging\Log
30
     */
31
    protected $log;
32
33
    /**
34
     * Controller specific permission ability map.
35
     *
36
     * @var array
37
     */
38
    protected $customPermissionMap = [
39
        'backup' => 'view',
40
        'config' => 'view',
41
        'cache'  => 'view',
42
        'logs'   => 'view',
43
        'views'  => 'view',
44
        'index'  => 'view',
45
    ];
46
47
    /**
48
     * UtilitiesController constructor.
49
     *
50
     * @param \Illuminate\Contracts\Console\Kernel $command
51
     * @param \Illuminate\Contracts\Logging\Log $log
52
     * @param \Collective\Html\HtmlBuilder $html
53
     */
54
    public function __construct(Kernel $command, Log $log, HtmlBuilder $html)
55
    {
56
        $this->command = $command;
57
        $this->html    = $html;
58
        $this->log     = $log;
59
60
        $this->authorizePermissionResource('utilities');
61
    }
62
63
    /**
64
     * Display list of utilities.
65
     *
66
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
67
     */
68
    public function index()
69
    {
70
        return view('administrator.utilities.index');
71
    }
72
73
    /**
74
     * Execute back up manually.
75
     *
76
     * @param string $task
77
     * @return \Illuminate\Http\JsonResponse
78
     */
79 View Code Duplication
    public function backup($task = 'run')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
    {
81
        if (! in_array($task, ['backup', 'clean'])) {
82
            $message = trans('cms::utilities.backup.not_allowed',['task'=>$task]). trans('cms::utilities.field.executed_by',['name'=>$this->getCurrentUserName()]);
83
            $this->log->info($message);
84
85
            return $this->notifyError($message);
86
        }
87
88
        $this->command->call('backup:' . $task);
89
        $message = $task == 'clean' ? trans('cms::utilities.backup.cleanup_complete') : trans('cms::utilities.backup.complete');
90
        $this->log->info($message . trans('cms::utilities.field.executed_by',['name'=>$this->getCurrentUserName()]));
91
92
        return $this->notifySuccess($message);
93
    }
94
95
    /**
96
     * Get name of the current user.
97
     *
98
     * @return string
99
     */
100
    protected function getCurrentUserName()
101
    {
102
        return auth('administrator')->user()->present()->name();
0 ignored issues
show
Bug introduced by
The method user() does not seem to exist on object<Illuminate\Contracts\Auth\Factory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
103
    }
104
105
    /**
106
     * Clear cache manually.
107
     *
108
     * @return \Illuminate\Http\JsonResponse
109
     */
110 View Code Duplication
    public function cache()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
    {
112
        $this->command->call('cache:clear');
113
        $this->log->info(trans('cms::utilities.cache.success') . trans('cms::utilities.field.executed_by',['name'=>$this->getCurrentUserName()]));
114
115
        return $this->notifySuccess(trans('cms::utilities.cache.success'));
116
    }
117
118
    /**
119
     * Run config artisan command manually.
120
     *
121
     * @param string $task
122
     * @return \Illuminate\Http\JsonResponse
123
     */
124 View Code Duplication
    public function config($task)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
    {
126
        if (! in_array($task, ['cache', 'clear'])) {
127
            $message = trans('cms::utilities.config.not_allowed',['task'=>$task]). trans('cms::utilities.field.executed_by',['name'=>$this->getCurrentUserName()]);
128
            $this->log->info($message);
129
130
            return $this->notifyError($message);
131
        }
132
133
        $message = $task == 'cache' ? trans('cms::utilities.config.cache') : trans('cms::utilities.config.cache_cleared');
134
        $this->log->info($message . trans('cms::utilities.field.executed_by',['name'=>$this->getCurrentUserName()]));
135
        $this->command->call('config:' . $task);
136
137
        return $this->notifySuccess($message);
138
    }
139
140
    /**
141
     * Clear views manually.
142
     *
143
     * @return \Illuminate\Http\JsonResponse
144
     */
145 View Code Duplication
    public function views()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
146
    {
147
        $this->command->call('view:clear');
148
        $this->log->info(trans('cms::utilities.views.success').trans('cms::utilities.field.executed_by',['name'=>$this->getCurrentUserName()]));
149
150
        return $this->notifySuccess(trans('cms::utilities.views.success'));
151
    }
152
153
    /**
154
     * Log viewer.
155
     *
156
     * @param \Illuminate\Http\Request $request
157
     * @param \Yajra\Datatables\Datatables $datatables
158
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View|\Symfony\Component\HttpFoundation\BinaryFileResponse
159
     * @throws \Exception
160
     */
161
    public function logs(Request $request, Datatables $datatables)
162
    {
163
        if ($request->input('l')) {
164
            LaravelLogViewer::setFile(base64_decode($request->input('l')));
165
        }
166
167
        if ($request->input('dl')) {
168
            return response()->download(LaravelLogViewer::pathToLogFile(base64_decode($request->input('dl'))));
169
        } elseif ($request->has('del')) {
170
            File::delete(LaravelLogViewer::pathToLogFile(base64_decode($request->input('del'))));
171
172
            return redirect()->to($request->url());
173
        }
174
175
        $logs = LaravelLogViewer::all();
176
177
        if ($request->wantsJson()) {
178
            return $datatables->collection(collect($logs))
179
                              ->editColumn('stack', '{!! nl2br($stack) !!}')
180
                              ->editColumn('level', function ($log) {
181
                                  $content = $this->html->tag('span', '', [
182
                                      'class' => "glyphicon glyphicon-{$log['level_img']}-sign",
183
                                  ]);
184
185
                                  $content .= '&nbsp;' . $log['level'];
186
187
                                  return $this->html->tag('span', $content, ['class' => "text-{$log['level_class']}"]);
188
                              })
189
                              ->addColumn('content', function ($log) {
190
                                  $html = '';
191
                                  if ($log['stack']) {
192
                                      $html = '<a class="pull-right expand btn btn-default btn-xs"><span class="glyphicon glyphicon-search"></span></a>';
193
                                  }
194
195
                                  $html .= $log['text'];
196
197
                                  if (isset($log['in_file'])) {
198
                                      $html .= '<br>' . $log['in_file'];
199
                                  }
200
201
                                  return $html;
202
                              })
203
                              ->make(true);
204
        }
205
206
        return view('administrator.utilities.log', [
207
            'logs'         => $logs,
208
            'files'        => LaravelLogViewer::getFiles(true),
209
            'current_file' => LaravelLogViewer::getFileName(),
210
        ]);
211
    }
212
213
    /**
214
     * Rebuild menu entity nested set tree.
215
     */
216
    public function rebuildMenu()
217
    {
218
        Menu::rebuild();
219
220
        return $this->notifySuccess(trans('cms::utilities.menu.success'));
221
    }
222
223
    /**
224
     * Rebuild category entity nested set tree.
225
     */
226
    public function rebuildCategory()
227
    {
228
        Category::rebuild();
229
230
        return $this->notifySuccess(trans('cms::utilities.category.success'));
231
    }
232
}
233