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

Task::queueJobsFromData()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
cc 4
nc 5
nop 4
1
<?php
2
3
namespace App\Queue\Factory;
4
5
use SilverStripe\Control\HTTPRequest;
6
use SilverStripe\Core\Injector\Injector;
7
use SilverStripe\Dev\BuildTask;
8
use SilverStripe\ORM\DataList;
9
use SilverStripe\ORM\ValidationException;
10
use Symbiote\QueuedJobs\Services\QueuedJobService;
11
12
/**
13
 * Class Task
14
 *
15
 * generic task which creates jobs from id list
16
 * each job contains a chunk of the ids
17
 * GET param options
18
 * size - (int) chunk size
19
 * limit - (int) limits the number of IDs processed, test only, defaults to 0 (no limit)
20
 * offset - (int) apply offset when using limit
21
 * jobs - (int) number of chunks per factory job, setting it 0 skips factory jobs step
22
 * used for long execution runs where more than 3000 specified jobs are created
23
 *
24
 * @package App\Queue\Factory
25
 */
26
abstract class Task extends BuildTask
27
{
28
29
    private const FACTORY_JOBS_BATCH_SIZE = 500;
30
31
    /**
32
     * @param HTTPRequest $request
33
     * @param DataList $list
34
     * @param string $jobClass
35
     * @param int $size
36
     * @throws ValidationException
37
     */
38
    protected function queueJobsFromList(HTTPRequest $request, DataList $list, string $jobClass, int $size): void
39
    {
40
        $limit = (int) $request->getVar('limit');
41
        $offset = (int) $request->getVar('offset');
42
43
        if ($limit > 0) {
44
            $list = $list->limit($limit, $offset);
45
        }
46
47
        $ids = $list->columnUnique('ID');
48
        $this->queueJobsFromIds($request, $ids, $jobClass, $size);
49
    }
50
51
    /**
52
     * @param HTTPRequest $request
53
     * @param array $ids
54
     * @param string $jobClass
55
     * @param int $size
56
     * @throws ValidationException
57
     */
58
    protected function queueJobsFromIds(HTTPRequest $request, array $ids, string $jobClass, int $size): void
59
    {
60
        $ids = $this->formatIds($ids);
61
        $this->queueJobsFromData($request, $ids, $jobClass, $size);
62
    }
63
64
    /**
65
     * @param HTTPRequest $request
66
     * @param array $data
67
     * @param string $jobClass
68
     * @param int $size
69
     * @throws ValidationException
70
     */
71
    protected function queueJobsFromData(HTTPRequest $request, array $data, string $jobClass, int $size): void
72
    {
73
        if (count($data) === 0) {
74
            return;
75
        }
76
77
        $jobs = $request->getVar('jobs') ?? self::FACTORY_JOBS_BATCH_SIZE;
78
        $jobs = (int) $jobs;
79
80
        $chunkSize = (int) $request->getVar('size');
81
        $chunkSize = $chunkSize > 0
82
            ? $chunkSize
83
            : $size;
84
85
        $chunks = array_chunk($data, $chunkSize);
86
87
        if ($jobs > 0) {
88
            $this->createFactoryJobs($chunks, $jobClass, $jobs);
89
90
            return;
91
        }
92
93
        $this->createSpecifiedJobs($chunks, $jobClass);
94
    }
95
96
    /**
97
     * @param array $chunks
98
     * @param string $jobClass
99
     * @throws ValidationException
100
     */
101
    private function createSpecifiedJobs(array $chunks, string $jobClass): void
102
    {
103
        $service = QueuedJobService::singleton();
104
105
        foreach ($chunks as $chunk) {
106
            $job = Injector::inst()->create($jobClass);
107
            $job->hydrate(array_values($chunk));
108
            $service->queueJob($job);
109
        }
110
    }
111
112
    /**
113
     * @param array $chunks
114
     * @param string $jobClass
115
     * @param int $chunkSize
116
     * @throws ValidationException
117
     */
118
    private function createFactoryJobs(array $chunks, string $jobClass, int $chunkSize): void
119
    {
120
        $service = QueuedJobService::singleton();
121
        $chunks = array_chunk($chunks, $chunkSize);
122
123
        foreach ($chunks as $chunk) {
124
            $job = new Job();
125
            $job->hydrate($jobClass, array_values($chunk));
126
            $service->queueJob($job);
127
        }
128
    }
129
130
    /**
131
     * Cast all IDs to int so we don't end up with type errors
132
     *
133
     * @param array $ids
134
     * @return array
135
     */
136
    private function formatIds(array $ids): array
137
    {
138
        $formatted = [];
139
140
        foreach ($ids as $id) {
141
            $formatted[] = (int) $id;
142
        }
143
144
        return $formatted;
145
    }
146
}
147