WorkerController::options()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace yrc\commands;
4
5
use Exception;
6
use yii\console\Controller;
7
use Yii;
8
9
class WorkerController extends Controller
10
{
11
    /**
12
     * @var string
13
     * The RPQ Job ID as passed by the RPQ CLI
14
     */
15
    public $jobId;
16
17
    /**
18
     * @var string
19
     * The name of the RPQ queue that this command should search for jobs in.
20
     */
21
    public $name = 'default';
22
23
    /**
24
     * Command line options
25
     * @param string $actionID
26
     * @return array
27
     */
28
    public function options($actionID)
29
    {
30
        return [
31
            'jobId',
32
            'name'
33
        ];
34
    }
35
36
    /**
37
     * Starts a worker in process mode
38
     * @return integer
39
     */
40
    public function actionProcess()
41
    {
42
        $hash = explode(':', $this->jobId);
43
        $jobId = $hash[count($hash) - 1];
44
45
        try {
46
            $job = Yii::$app->rpq->getQueue($this->name)->getJob($jobId);
0 ignored issues
show
Bug introduced by
The method getQueue() does not exist on null. ( Ignorable by Annotation )

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

46
            $job = Yii::$app->rpq->/** @scrutinizer ignore-call */ getQueue($this->name)->getJob($jobId);

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...
47
            $class = $job->getWorkerClass();
48
49
            if (!\class_exists($class)) {
50
                throw new Exception("Unable to find worker class {$class}");
51
            }
52
53
            if (!\is_subclass_of($class, '\RPQ\Server\AbstractJob')) {
54
                throw new Exception('Job does not implement RPQ\Server\AbstractJob');
55
            }
56
            
57
            $task = new $class(Yii::$app->rpq->getClient(), $job->getId());
58
            return $task->perform($job->getArgs());
59
        } catch (Exception $e) {
60
            // Log the error to the appropriate handler
61
            Yii::error([
62
                'message' => 'An error occured when executing the job',
63
                'jobId' => $job->getId(),
64
                'workerClass' => $job->getWorkerClass(),
65
                'queueName' => $this->name,
66
                'message' => $e->getMessage(),
67
                'code' => $e->getCode(),
68
                'file' => $e->getFile(),
69
                'line' => $e->getLine(),
70
            ], 'rpq');
71
            return -1;
72
        }
73
74
        return 0;
0 ignored issues
show
Unused Code introduced by
return 0 is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
75
    }
76
}
77