GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#290)
by
unknown
01:34
created

Extension::getJobStatuses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace App\Queue\Admin;
4
5
use SilverStripe\Core\Extension as BaseExtension;
6
use SilverStripe\Forms\DropdownField;
7
use SilverStripe\Forms\Form;
8
use SilverStripe\Forms\GridField\GridField;
9
use SilverStripe\Forms\GridField\GridFieldConfig;
10
use SilverStripe\Forms\GridField\GridFieldDataColumns;
11
use SilverStripe\Forms\GridField\GridFieldFilterHeader;
12
use SilverStripe\Forms\GridField\GridFieldPaginator;
13
use SilverStripe\ORM\DataList;
14
use SilverStripe\ORM\FieldType\DBDatetime;
15
use Symbiote\QueuedJobs\Controllers\QueuedJobsAdmin;
16
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor;
17
use Terraformers\RichFilterHeader\Form\GridField\RichFilterHeader;
18
19
/**
20
 * Class Extension
21
 *
22
 * @property QueuedJobsAdmin|$this $owner
23
 * @package App\Queue\Admin
24
 */
25
class Extension extends BaseExtension
26
{
27
28
    private const SCHEDULED_FILTER_FUTURE = 'future';
29
    private const SCHEDULED_FILTER_PAST = 'past';
30
31
    /**
32
     * @param Form $form
33
     */
34
    public function updateEditForm(Form $form): void
35
    {
36
        $fields = $form->Fields();
37
38
        // there are multiple fields that need to be updated
39
        $fieldNames = [
40
            'QueuedJobDescriptor',
41
            $this->encodeClassName(QueuedJobDescriptor::class),
42
        ];
43
44
        foreach ($fieldNames as $fieldName) {
45
            /** @var GridField $gridField */
46
            $gridField = $fields->fieldByName($fieldName);
47
48
            if (!$gridField) {
49
                continue;
50
            }
51
52
            $config = $gridField->getConfig();
53
54
            // apply custom filters
55
            $this->customiseFilters($config);
56
        }
57
    }
58
59
    /**
60
     * Customise queued jobs filters UI
61
     *
62
     * @param GridFieldConfig $config
63
     */
64
    private function customiseFilters(GridFieldConfig $config): void
65
    {
66
        /** @var GridFieldDataColumns $gridFieldColumns */
67
        $gridFieldColumns = $config->getComponentByType(GridFieldDataColumns::class);
68
69
        $gridFieldColumns->setDisplayFields([
70
            'getImplementationSummary' => 'Type',
71
            'JobTypeString' => 'Queue',
72
            'JobStatus' => 'Status',
73
            'JobTitle' => 'Description',
74
            'Created' => 'Added',
75
            'StartAfter' => 'Scheduled',
76
            'JobFinished' => 'Finished',
77
        ]);
78
79
        $config->removeComponentsByType(GridFieldFilterHeader::class);
80
81
        $filter = new RichFilterHeader();
82
        $filter
83
            ->setFilterConfig([
84
                'getImplementationSummary' => 'Implementation',
85
                'Description' => 'JobTitle',
86
                'Status' => [
87
                    'title' => 'JobStatus',
88
                    'filter' => 'ExactMatchFilter',
89
                ],
90
                'JobTypeString' => [
91
                    'title' => 'JobType',
92
                    'filter' => 'ExactMatchFilter',
93
                ],
94
                'Created' => 'Added',
95
                'StartAfter' => 'Scheduled',
96
            ])
97
            ->setFilterFields([
98
                'JobType' => $queueType = DropdownField::create(
99
                    '',
100
                    '',
101
                    $this->getQueueTypes()
102
                ),
103
                'JobStatus' => $jobStatus = DropdownField::create(
104
                    '',
105
                    '',
106
                    $this->getJobStatuses()
107
                ),
108
                'Added' => $added = DropdownField::create(
109
                    '',
110
                    '',
111
                    $this->getAddedDates()
112
                ),
113
                'Scheduled' => $scheduled = DropdownField::create(
114
                    '',
115
                    '',
116
                    [
117
                        self::SCHEDULED_FILTER_FUTURE => self::SCHEDULED_FILTER_FUTURE,
118
                        self::SCHEDULED_FILTER_PAST => self::SCHEDULED_FILTER_PAST,
119
                    ]
120
                ),
121
            ])
122
            ->setFilterMethods([
123
                'Added' => static function (DataList $list, $name, $value): DataList {
124
                    if ($value) {
125
                        $added = DBDatetime::now()->modify($value);
126
127
                        return $list->filter(['Created:LessThanOrEqual' => $added->Rfc2822()]);
128
                    }
129
130
                    return $list;
131
                },
132
                'Scheduled' => static function (DataList $list, $name, $value): DataList {
133
                    if ($value === static::SCHEDULED_FILTER_FUTURE) {
134
                        return $list->filter([
135
                            'StartAfter:GreaterThan' => DBDatetime::now()->Rfc2822(),
136
                        ]);
137
                    }
138
139
                    if ($value === static::SCHEDULED_FILTER_PAST) {
140
                        return $list->filter([
141
                            'StartAfter:LessThanOrEqual' => DBDatetime::now()->Rfc2822(),
142
                        ]);
143
                    }
144
145
                    return $list;
146
                },
147
            ]);
148
149
        foreach ([$jobStatus, $queueType, $added, $scheduled] as $dropDownField) {
150
            /** @var DropdownField $dropDownField */
151
            $dropDownField->setEmptyString('-- select --');
152
        }
153
154
        $config->addComponent($filter, GridFieldPaginator::class);
155
    }
156
157
    /**
158
     * Queue types options for drop down field
159
     *
160
     * @return array
161
     */
162
    private function getQueueTypes(): array
163
    {
164
        /** @var QueuedJobDescriptor $job */
165
        $job = QueuedJobDescriptor::singleton();
166
        $map = $job->getJobTypeValues();
167
        $values = array_values($map);
168
        $keys = [];
169
170
        foreach (array_keys($map) as $key) {
171
            $keys[] = (int) $key;
172
        }
173
174
        return array_combine($keys, $values);
175
    }
176
177
    /**
178
     * All possible job statuses (this list is not exposed by the module)
179
     * intended to be used in a drop down field
180
     *
181
     * @return array
182
     */
183
    private function getJobStatuses(): array
184
    {
185
        /** @var QueuedJobDescriptor $job */
186
        $job = QueuedJobDescriptor::singleton();
187
        $statuses = $job->getJobStatusValues();
0 ignored issues
show
Documentation Bug introduced by
The method getJobStatusValues does not exist on object<Symbiote\QueuedJo...ts\QueuedJobDescriptor>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
188
189
        sort($statuses, SORT_STRING);
190
191
        $statuses = array_combine($statuses, $statuses);
192
193
        return $statuses;
194
    }
195
196
    /**
197
     * Encode class name to match the matching CMS field name
198
     *
199
     * @param string $className
200
     * @return string
201
     */
202
    private function encodeClassName(string $className): string
203
    {
204
        return str_replace('\\', '-', $className);
205
    }
206
207
    /**
208
     * Date options for added dates drop down field
209
     *
210
     * @return array
211
     */
212
    private function getAddedDates(): array
213
    {
214
        return [
215
            '-1 day' => '1 day or older',
216
            '-3 day' => '3 days or older',
217
            '-7 day' => '7 days or older',
218
            '-14 day' => '14 days or older',
219
            '-1 month' => '1 month or older',
220
        ];
221
    }
222
}
223