Completed
Push — master ( 73fbcf...1ed8ca )
by Luke
10s
created

QueueWorkBatchCommand::gatherWorkerOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * Laravel Queue for AWS Batch.
4
 *
5
 * @author    Luke Waite <[email protected]>
6
 * @copyright 2017 Luke Waite
7
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
8
 *
9
 * @link      https://github.com/lukewaite/laravel-queue-aws-batch
10
 */
11
12
namespace LukeWaite\LaravelQueueAwsBatch\Console;
13
14
use Illuminate\Console\Command;
15
use Illuminate\Foundation\Exceptions\Handler;
16
use Illuminate\Queue\Console\WorkCommand;
17
use Illuminate\Queue\QueueManager;
18
use Illuminate\Queue\Worker;
19
use Illuminate\Queue\WorkerOptions;
20
use LukeWaite\LaravelQueueAwsBatch\Exceptions\JobNotFoundException;
21
use LukeWaite\LaravelQueueAwsBatch\Exceptions\UnsupportedException;
22
use LukeWaite\LaravelQueueAwsBatch\Queues\BatchQueue;
23
use Symfony\Component\Debug\Exception\FatalThrowableError;
24
25
class QueueWorkBatchCommand extends WorkCommand
26
{
27
    protected $name = 'queue:work-batch';
28
29
    protected $description = 'Run a Job for the AWS Batch queue';
30
31
    protected $signature = 'queue:work-batch
32
                            {job_id : The job id in the database}
33
                            {connection? : The name of the queue connection to work}
34
                            {--memory=128 : The memory limit in megabytes}
35
                            {--timeout=60 : The number of seconds a child process can run}
36
                            {--tries=0 : Number of times to attempt a job before logging it failed}';
37
38
39
    protected $manager;
40
    protected $exceptions;
41
42
    public function __construct(QueueManager $manager, Worker $worker, Handler $exceptions)
43
    {
44
        parent::__construct($worker);
45
        $this->manager = $manager;
46
        $this->exceptions = $exceptions;
47
    }
48
49
    public function fire()
50
    {
51
        $this->listenForEvents();
52
53
        try {
54
            $this->runJob();
55
        } catch (\Exception $e) {
56
            $this->exceptions->report($e);
57
            throw $e;
58
        } catch (\Throwable $e) {
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
59
            $this->exceptions->report(new FatalThrowableError($e));
60
            throw $e;
61
        }
62
    }
63
64
    // TOOD: Refactor out the logic here into an extension of the Worker class
65
    protected function runJob()
66
    {
67
        $connectionName = $this->argument('connection');
68
        $jobId = $this->argument('job_id');
69
70
        /** @var BatchQueue $connection */
71
        $connection = $this->manager->connection($connectionName);
0 ignored issues
show
Bug introduced by
It seems like $connectionName defined by $this->argument('connection') on line 67 can also be of type array; however, Illuminate\Queue\QueueManager::connection() does only seem to accept string|null, 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...
72
73
        if (!$connection instanceof BatchQueue) {
74
            throw new UnsupportedException('queue:work-batch can only be run on batch queues');
75
        }
76
77
        $job = $connection->getJobById($jobId, $connectionName);
0 ignored issues
show
Unused Code introduced by
The call to BatchQueue::getJobById() has too many arguments starting with $connectionName.

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
78
79
        // If we're able to pull a job off of the stack, we will process it and
80
        // then immediately return back out.
81
        if (!is_null($job)) {
82
            return $this->worker->process(
83
                $this->manager->getName($connectionName),
0 ignored issues
show
Bug introduced by
It seems like $connectionName defined by $this->argument('connection') on line 67 can also be of type array; however, Illuminate\Queue\QueueManager::getName() does only seem to accept string|null, 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...
84
                $job,
85
                $this->gatherWorkerOptions()
86
            );
87
        }
88
89
        // If we hit this point, we haven't processed our job
90
        throw new JobNotFoundException('No job was returned');
91
    }
92
93
    /**
94
     * Gather all of the queue worker options as a single object.
95
     *
96
     * @return \Illuminate\Queue\WorkerOptions
97
     */
98
    protected function gatherWorkerOptions()
99
    {
100
        return new WorkerOptions(
101
            0, $this->option('memory'),
0 ignored issues
show
Documentation introduced by
$this->option('memory') is of type string|array, but the function expects a integer.

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...
102
            $this->option('timeout'), 0,
0 ignored issues
show
Documentation introduced by
$this->option('timeout') is of type string|array, but the function expects a integer.

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...
103
            $this->option('tries'), false
0 ignored issues
show
Documentation introduced by
$this->option('tries') is of type string|array, but the function expects a integer.

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...
104
        );
105
    }
106
}
107