Completed
Pull Request — master (#3)
by ARCANEDEV
16:54
created

StatusesController::backup()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanesoft\Backups\Http\Controllers;
6
7
use Arcanesoft\Backups\Policies\StatusesPolicy;
8
use Arcanesoft\Backups\Services\BackupService;
9
use Arcanesoft\Foundation\Support\Traits\HasNotifications;
10
use Illuminate\Support\Facades\Log;
11
12
/**
13
 * Class     StatusesController
14
 *
15
 * @author   ARCANEDEV <[email protected]>
16
 */
17
class StatusesController extends Controller
18
{
19
    /* -----------------------------------------------------------------
20
     |  Traits
21
     | -----------------------------------------------------------------
22
     */
23
24
    use HasNotifications;
25
26
    /* -----------------------------------------------------------------
27
     |  Properties
28
     | -----------------------------------------------------------------
29
     */
30
31
    /** @var  \Arcanesoft\Backups\Services\BackupService */
32
    protected $backupService;
33
34
    /* -----------------------------------------------------------------
35
     |  Constructor
36
     | -----------------------------------------------------------------
37
     */
38
39
    /**
40
     * StatusesController constructor.
41
     */
42
    public function __construct(BackupService $backupService)
43
    {
44
        parent::__construct();
45
46
        $this->setCurrentSidebarItem('foundation::backups');
47
        $this->addBreadcrumbRoute(__('Backups'), 'admin::backups.statuses.index');
48
49
        $this->backupService = $backupService;
50
    }
51
52
    /* -----------------------------------------------------------------
53
     |  Main Methods
54
     | -----------------------------------------------------------------
55
     */
56
57
    public function index()
58
    {
59
        $this->authorize(StatusesPolicy::ability('index'));
0 ignored issues
show
Bug introduced by
The method authorize() does not seem to exist on object<Arcanesoft\Backup...ers\StatusesController>.

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...
60
61
        $statuses = $this->backupService->statuses();
62
63
        $this->setTitle($title = __('List of Monitor Statuses'));
0 ignored issues
show
Unused Code introduced by
The call to the method Arcanesoft\Backups\Http\...sController::setTitle() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
64
        $this->addBreadcrumb($title);
65
66
        return $this->view('statuses.index', compact('statuses'));
67
    }
68
69
    public function show($index)
70
    {
71
        $this->authorize(StatusesPolicy::ability('show'));
0 ignored issues
show
Bug introduced by
The method authorize() does not seem to exist on object<Arcanesoft\Backup...ers\StatusesController>.

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...
72
73
        $status = $this->backupService->getStatus($index);
74
75
        abort_if(is_null($status), 404);
76
77
        $this->setTitle($title = __('Monitor Status'));
0 ignored issues
show
Unused Code introduced by
The call to the method Arcanesoft\Backups\Http\...sController::setTitle() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
78
        $this->addBreadcrumb($title);
79
80
        return $this->view('statuses.show', compact('status'));
81
    }
82
83
    public function backup()
84
    {
85
        $this->authorize(StatusesPolicy::ability('create'));
0 ignored issues
show
Bug introduced by
The method authorize() does not seem to exist on object<Arcanesoft\Backup...ers\StatusesController>.

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...
86
87
        if ($this->backupService->runBackups()) {
88
            return static::jsonResponseSuccess([
89
                'message' => $this->transNotification('created'),
90
            ]);
91
        }
92
93
        return static::jsonResponseError([
94
            'message' => 'There is an error while running the backups.'
95
        ]);
96
    }
97
98
    public function clear()
99
    {
100
        $this->authorize(StatusesPolicy::ability('clean'));
0 ignored issues
show
Bug introduced by
The method authorize() does not seem to exist on object<Arcanesoft\Backup...ers\StatusesController>.

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...
101
102
        if ($this->backupService->clearBackups()) {
103
            return static::jsonResponseSuccess([
104
                'message' => $this->transNotification('cleared'),
105
            ]);
106
        }
107
108
        return static::jsonResponseError(['message' => 'There is an error while running the backups.']);
109
    }
110
111
    /* -----------------------------------------------------------------
112
     |  Other Methods
113
     | -----------------------------------------------------------------
114
     */
115
116
    /**
117
     * Notify with translation.
118
     *
119
     * @param  string  $action
120
     * @param  array   $replace
121
     * @param  array   $context
122
     *
123
     * @return string
124
     */
125
    protected function transNotification($action, array $replace = [], array $context = [])
126
    {
127
        $title   = trans("backups::statuses.messages.{$action}.title");
128
        $message = trans("backups::statuses.messages.{$action}.message", $replace);
129
130
        Log::info($message, $context);
131
132
        $this->notifySuccess($title, $message);
133
134
        return $message;
135
    }
136
}
137