Passed
Pull Request — master (#85)
by Robbie
01:48
created

GridFieldRefreshButton::hasPendingJob()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace BringYourOwnIdeas\Maintenance\Forms;
4
5
use BringYourOwnIdeas\Maintenance\Reports\SiteSummary;
6
use SilverStripe\ORM\DataList;
0 ignored issues
show
Bug introduced by
The type SilverStripe\ORM\DataList was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use SilverStripe\View\Requirements;
0 ignored issues
show
Bug introduced by
The type SilverStripe\View\Requirements was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use SilverStripe\Forms\GridField\GridField_FormAction;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Forms\GridField\GridField_FormAction was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use SilverStripe\View\ArrayData;
0 ignored issues
show
Bug introduced by
The type SilverStripe\View\ArrayData was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use SilverStripe\Forms\GridField\GridField;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Forms\GridField\GridField was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use SilverStripe\Core\Convert;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Core\Convert was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use SilverStripe\Core\Injector\Injector;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Core\Injector\Injector was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor;
0 ignored issues
show
Bug introduced by
The type Symbiote\QueuedJobs\Data...cts\QueuedJobDescriptor was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Symbiote\QueuedJobs\Services\QueuedJobService;
0 ignored issues
show
Bug introduced by
The type Symbiote\QueuedJobs\Services\QueuedJobService was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Symbiote\QueuedJobs\Services\QueuedJob;
0 ignored issues
show
Bug introduced by
The type Symbiote\QueuedJobs\Services\QueuedJob was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use BringYourOwnIdeas\Maintenance\Jobs\CheckForUpdatesJob;
17
use SilverStripe\Forms\GridField\GridField_HTMLProvider;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Forms\GridF...\GridField_HTMLProvider was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use SilverStripe\Forms\GridField\GridField_ActionProvider;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Forms\GridF...ridField_ActionProvider was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use SilverStripe\Forms\GridField\GridField_URLHandler;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Forms\GridField\GridField_URLHandler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
21
/**
22
 * Adds a "Refresh" button to the bottom or top of a GridField.
23
 *
24
 * @package forms
25
 * @subpackage fields-gridfield
26
 */
27
28
class GridFieldRefreshButton implements GridField_HTMLProvider, GridField_ActionProvider, GridField_URLHandler
29
{
30
    /**
31
     * @var array
32
     * @config
33
     */
34
    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...
35
36
    /**
37
     * Fragment to write the button to.
38
     * @var string
39
     */
40
    protected $targetFragment;
41
42
    /**
43
     * @param string $targetFragment The HTML fragment to write the button into
44
     */
45
    public function __construct($targetFragment)
46
    {
47
        $this->targetFragment = $targetFragment;
48
    }
49
50
    /**
51
     * @param GridField $gridField
52
     * @return array
53
     */
54
    public function getHTMLFragments($gridField)
55
    {
56
        Requirements::javascript('bringyourownideas/silverstripe-maintenance: client/dist/js/bundle.js');
57
58
        $button = GridField_FormAction::create(
59
            $gridField,
60
            'refresh',
61
            _t(__CLASS__ . '.REFRESH', 'Check for updates'),
0 ignored issues
show
Bug introduced by
The function _t was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

61
            /** @scrutinizer ignore-call */ 
62
            _t(__CLASS__ . '.REFRESH', 'Check for updates'),
Loading history...
62
            'refresh',
63
            null
64
        );
65
66
        $button->addExtraClass('btn btn-secondary font-icon-sync');
67
68
        $button->setAttribute('data-check', $gridField->Link('check'));
69
        $button->setAttribute(
70
            'data-message',
71
            _t(
72
                __CLASS__ . '.MESSAGE',
73
                'Updating this list may take 2-3 minutes. You can continue to use the CMS while we run the update.'
74
            )
75
        );
76
77
        if ($this->hasPendingJob()) {
78
            $button->setTitle(_t(__CLASS__ . '.UPDATE', 'Updating...'));
79
            $button->setDisabled(true);
80
        }
81
82
        return [
83
            $this->targetFragment => ArrayData::create(['Button' => $button->Field()])
84
                ->renderWith(__CLASS__)
85
        ];
86
    }
87
88
    /**
89
     * Refresh is an action button.
90
     *
91
     * @param GridField $gridField
92
     *
93
     * @return array
94
     */
95
    public function getActions($gridField)
96
    {
97
        return ['refresh'];
98
    }
99
100
    /**
101
     * Handle the refresh action.
102
     *
103
     * @param GridField $gridField
104
     * @param string $actionName
105
     * @param array $arguments
106
     * @param array $data
107
     *
108
     * @return null
109
     */
110
    public function handleAction(GridField $gridField, $actionName, $arguments, $data)
111
    {
112
        if ($actionName == 'refresh') {
113
            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

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