Completed
Push — master ( 3da775...1e6bd3 )
by Robbie
9s
created

GridFieldRefreshButton::handleRefresh()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace BringYourOwnIdeas\Maintenance\Forms;
4
5
use SilverStripe\ORM\DataList;
6
use SilverStripe\View\Requirements;
7
use SilverStripe\Forms\GridField\GridField_FormAction;
8
use SilverStripe\View\ArrayData;
9
use SilverStripe\Forms\GridField\GridField;
10
use SilverStripe\Core\Convert;
11
use SilverStripe\Core\Injector\Injector;
12
use Symbiote\QueuedJobs\Services\QueuedJobService;
13
use Symbiote\QueuedJobs\Services\QueuedJob;
14
use BringYourOwnIdeas\Maintenance\Jobs\CheckForUpdatesJob;
15
use SilverStripe\Forms\GridField\GridField_HTMLProvider;
16
use SilverStripe\Forms\GridField\GridField_ActionProvider;
17
use SilverStripe\Forms\GridField\GridField_URLHandler;
18
19
/**
20
 * Adds a "Refresh" button to the bottom or top of a GridField.
21
 *
22
 * @package forms
23
 * @subpackage fields-gridfield
24
 */
25
26
class GridFieldRefreshButton implements GridField_HTMLProvider, GridField_ActionProvider, GridField_URLHandler
27
{
28
    /**
29
     * @var array
30
     * @config
31
     */
32
    private static $allowed_actions = ["check"];
0 ignored issues
show
introduced by
The private property $allowed_actions is not used, and could be removed.
Loading history...
33
34
    /**
35
     * Fragment to write the button to.
36
     * @var string
37
     */
38
    protected $targetFragment;
39
40
    /**
41
     * @param string $targetFragment The HTML fragment to write the button into
42
     */
43
    public function __construct($targetFragment)
44
    {
45
        $this->targetFragment = $targetFragment;
46
    }
47
48
    /**
49
     * @param GridField $gridField
50
     * @return array
51
     */
52
    public function getHTMLFragments($gridField)
53
    {
54
        Requirements::javascript('bringyourownideas/silverstripe-maintenance: javascript/CheckForUpdates.js');
55
56
        $button = GridField_FormAction::create(
57
            $gridField,
58
            'refresh',
59
            _t('GridFieldRefreshButton.REFRESH', 'Check for updates'),
60
            'refresh',
61
            null
62
        );
63
64
        $button->addExtraClass('btn btn-secondary font-icon-sync');
65
66
        $button->setAttribute('data-check', $gridField->Link('check'));
67
        $button->setAttribute(
68
            'data-message',
69
            _t(
70
                'GridFieldRefreshButton.MESSAGE',
71
                'Updating this list may take 2-3 minutes. You can continue to use the CMS while we run the update.'
72
            )
73
        );
74
75
        if ($this->hasActiveJob()) {
76
            $button->setTitle(_t('GridFieldRefreshButton.UPDATE', 'Updating...'));
77
            $button->setDisabled(true);
78
        }
79
80
        return [
81
            $this->targetFragment => ArrayData::create(['Button' => $button->Field()])
82
                ->renderWith(__CLASS__)
83
        ];
84
    }
85
86
    /**
87
     * Refresh is an action button.
88
     *
89
     * @param GridField $gridField
90
     *
91
     * @return array
92
     */
93
    public function getActions($gridField)
94
    {
95
        return ['refresh'];
96
    }
97
98
    /**
99
     * Handle the refresh action.
100
     *
101
     * @param GridField $gridField
102
     * @param string $actionName
103
     * @param array $arguments
104
     * @param array $data
105
     *
106
     * @return null
107
     */
108
    public function handleAction(GridField $gridField, $actionName, $arguments, $data)
109
    {
110
        if ($actionName == 'refresh') {
111
            return $this->handleRefresh($gridField);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->handleRefresh($gridField) targeting BringYourOwnIdeas\Mainte...Button::handleRefresh() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Unused Code introduced by
The call to BringYourOwnIdeas\Mainte...Button::handleRefresh() has too many arguments starting with $gridField. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

111
            return $this->/** @scrutinizer ignore-call */ handleRefresh($gridField);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
112
        }
113
    }
114
115
    /**
116
     * Refresh is accessible via the url
117
     *
118
     * @param GridField $gridField
119
     * @return array
120
     */
121
    public function getURLHandlers($gridField)
122
    {
123
        return [
124
            'check' => 'handleCheck'
125
        ];
126
    }
127
128
    /**
129
     * @see hasActiveJob
130
     * @return string JSON boolean
131
     */
132
    public function handleCheck()
133
    {
134
        $isRunning = $this->hasActiveJob();
135
        return Convert::raw2json($isRunning);
136
    }
137
138
    /**
139
     * Check the queue for refresh jobs that are not 'done'
140
     * in one manner or another (e.g. stalled or cancelled)
141
     *
142
     * @return boolean
143
     */
144
    public function hasActiveJob()
145
    {
146
        /** @var DataList $jobList */
147
        $jobList = Injector::inst()
148
            ->get(QueuedJobService::class)
149
            ->getJobList(QueuedJob::QUEUED)
150
            ->filter([
151
                'Implementation' => CheckForUpdatesJob::class
152
            ])
153
            ->exclude([
154
                'JobStatus' => [
155
                    QueuedJob::STATUS_COMPLETE,
156
                    QueuedJob::STATUS_CANCELLED,
157
                    QueuedJob::STATUS_BROKEN
158
                ]
159
            ]);
160
161
        return $jobList->exists();
162
    }
163
164
    /**
165
     * Handle the refresh, for both the action button and the URL
166
     */
167
    public function handleRefresh()
168
    {
169
        if (!$this->hasActiveJob()) {
170
            $injector = Injector::inst();
171
            $injector->get(QueuedJobService::class)->queueJob($injector->create(CheckForUpdatesJob::class));
172
        }
173
    }
174
}
175