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.

DoormanRunner::runQueue()   B
last analyzed

Complexity

Conditions 9
Paths 17

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 7.5135
c 0
b 0
f 0
cc 9
nc 17
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Symbiote\QueuedJobs\Tasks\Engines;
4
5
use AsyncPHP\Doorman\Manager\ProcessManager;
6
use SilverStripe\Core\Environment;
7
use SilverStripe\Core\Injector\Injector;
8
use SilverStripe\ORM\FieldType\DBDatetime;
9
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor;
10
use Symbiote\QueuedJobs\Jobs\DoormanQueuedJobTask;
11
use Symbiote\QueuedJobs\Services\QueuedJob;
12
use Symbiote\QueuedJobs\Services\QueuedJobService;
13
14
/**
15
 * Runs all jobs through the doorman engine
16
 */
17
class DoormanRunner extends BaseRunner implements TaskRunnerEngine
18
{
19
    /**
20
     * @var string[]
21
     */
22
    protected $defaultRules = [];
23
24
    /**
25
     * Assign default rules for this task
26
     *
27
     * @param array $rules
28
     * @return $this
29
     */
30
    public function setDefaultRules($rules)
31
    {
32
        $this->defaultRules = $rules;
33
        return $this;
34
    }
35
36
    /**
37
     * @return array List of rules
38
     */
39
    public function getDefaultRules()
40
    {
41
        return $this->defaultRules;
42
    }
43
44
    /**
45
     * Run tasks on the given queue
46
     *
47
     * @param string $queue
48
     */
49
    public function runQueue($queue)
50
    {
51
        // check if queue can be processed
52
        $service = QueuedJobService::singleton();
53
        if ($service->isAtMaxJobs()) {
54
            $service->getLogger()->info('Not processing queue as jobs are at max initialisation limit.');
55
            return;
56
        }
57
58
        // split jobs out into multiple tasks...
59
60
        /** @var ProcessManager $manager */
61
        $manager = Injector::inst()->create(ProcessManager::class);
62
        $manager->setWorker(
63
            BASE_PATH . "/vendor/silverstripe/framework/cli-script.php dev/tasks/ProcessJobQueueChildTask"
64
        );
65
        $logPath = Environment::getEnv('SS_DOORMAN_LOGPATH');
0 ignored issues
show
Bug introduced by
The method getEnv() does not seem to exist on object<SilverStripe\Core\Environment>.

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...
66
        if ($logPath) {
67
            $manager->setLogPath($logPath);
68
        }
69
70
        // Assign default rules
71
        $defaultRules = $this->getDefaultRules();
72
        if ($defaultRules) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $defaultRules of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
73
            foreach ($defaultRules as $rule) {
74
                $manager->addRule($rule);
0 ignored issues
show
Documentation introduced by
$rule is of type string, but the function expects a object<AsyncPHP\Doorman\Rule>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
75
            }
76
        }
77
78
        $descriptor = $this->getNextJobDescriptorWithoutMutex($queue);
79
80
        while ($manager->tick() || $descriptor) {
81
            if (QueuedJobService::singleton()->isMaintenanceLockActive()) {
82
                $service->getLogger()->info('Skipped queued job descriptor since maintenance log is active.');
83
                return;
84
            }
85
86
            $this->logDescriptorStatus($descriptor, $queue);
0 ignored issues
show
Bug introduced by
It seems like $descriptor can also be of type object<SilverStripe\ORM\DataObject>; however, Symbiote\QueuedJobs\Task...::logDescriptorStatus() does only seem to accept boolean|null|object<Symb...ts\QueuedJobDescriptor>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
87
88
            if ($descriptor instanceof QueuedJobDescriptor) {
89
                $descriptor->JobStatus = QueuedJob::STATUS_INIT;
90
                $descriptor->write();
91
92
                $manager->addTask(new DoormanQueuedJobTask($descriptor));
93
            }
94
95
            sleep(1);
96
97
            $descriptor = $this->getNextJobDescriptorWithoutMutex($queue);
98
        }
99
    }
100
101
    /**
102
     * @param string $queue
103
     * @return null|QueuedJobDescriptor
104
     */
105
    protected function getNextJobDescriptorWithoutMutex($queue)
106
    {
107
        $list = QueuedJobDescriptor::get()
108
            ->filter('JobType', $queue)
109
            ->sort('ID', 'ASC');
110
111
        $descriptor = $list
112
            ->filter('JobStatus', QueuedJob::STATUS_WAIT)
113
            ->first();
114
115
        if ($descriptor) {
116
            return $descriptor;
117
        }
118
119
        return $list
120
            ->filter('JobStatus', QueuedJob::STATUS_NEW)
121
            ->where(sprintf(
122
                '"StartAfter" < \'%s\' OR "StartAfter" IS NULL',
123
                DBDatetime::now()->getValue()
124
            ))
125
            ->first();
126
    }
127
}
128