StatusesController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 2
1
<?php namespace Arcanesoft\Backups\Http\Controllers\Admin;
2
3
use Arcanedev\LaravelApiHelper\Traits\JsonResponses;
4
use Arcanesoft\Backups\Policies\StatusesPolicy;
5
use Arcanesoft\Backups\Services\BackupStatuses;
6
use Illuminate\Support\Facades\Log;
7
8
/**
9
 * Class     StatusesController
10
 *
11
 * @package  Arcanesoft\Backups\Http\Controllers\Admin
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
class StatusesController extends Controller
15
{
16
    /* -----------------------------------------------------------------
17
     |  Traits
18
     | -----------------------------------------------------------------
19
     */
20
21
    use JsonResponses;
22
23
    /* -----------------------------------------------------------------
24
     |  Constructor
25
     | -----------------------------------------------------------------
26
     */
27
28
    /**
29
     * StatusesController constructor.
30
     */
31
    public function __construct()
32
    {
33
        parent::__construct();
34
35
        $this->setCurrentPage('backups-statuses');
36
        $this->addBreadcrumbRoute(trans('backups::statuses.titles.backups'), 'admin::backups.statuses.index');
37
    }
38
39
    /* -----------------------------------------------------------------
40
     |  Main Methods
41
     | -----------------------------------------------------------------
42
     */
43
44
    public function index()
45
    {
46
        $this->authorize(StatusesPolicy::PERMISSION_LIST);
47
48
        $statuses = BackupStatuses::all();
49
50
        $this->setTitle($title = trans('backups::statuses.titles.monitor-statuses-list'));
51
        $this->addBreadcrumb($title);
52
53
        return $this->view('admin.statuses.index', compact('statuses'));
54
    }
55
56
    public function show($index)
57
    {
58
        $this->authorize(StatusesPolicy::PERMISSION_SHOW);
59
60
        if (is_null($status = BackupStatuses::getStatus($index)))
61
            self::pageNotFound();
62
63
        $backups = $status->backupDestination()->backups();
64
65
        $this->setTitle($title = trans('backups::statuses.titles.monitor-status'));
66
        $this->addBreadcrumb($title);
67
68
        return $this->view('admin.statuses.show', compact('status', 'backups'));
69
    }
70
71
    public function backup()
72
    {
73
        $this->authorize(StatusesPolicy::PERMISSION_CREATE);
74
75
        if (BackupStatuses::runBackups()) {
76
            return $this->jsonResponseSuccess([
77
                'message' => $this->transNotification('created'),
78
            ]);
79
        }
80
81
        return $this->jsonResponseError(['message' => 'There is an error while running the backups.']);
82
    }
83
84
    public function clear()
85
    {
86
        $this->authorize(StatusesPolicy::PERMISSION_DELETE);
87
88
        if (BackupStatuses::clearBackups()) {
89
            return $this->jsonResponseSuccess([
90
                'message' => $this->transNotification('cleared'),
91
            ]);
92
        }
93
94
        return $this->jsonResponseError(['message' => 'There is an error while running the backups.']);
95
    }
96
97
    /* -----------------------------------------------------------------
98
     |  Other Methods
99
     | -----------------------------------------------------------------
100
     */
101
102
    /**
103
     * Notify with translation.
104
     *
105
     * @param  string  $action
106
     * @param  array   $replace
107
     * @param  array   $context
108
     *
109
     * @return string
110
     */
111
    protected function transNotification($action, array $replace = [], array $context = [])
112
    {
113
        $title   = trans("backups::statuses.messages.{$action}.title");
114
        $message = trans("backups::statuses.messages.{$action}.message", $replace);
115
116
        Log::info($message, $context);
117
        $this->notifySuccess($message, $title);
118
119
        return $message;
120
    }
121
}
122