Completed
Push — master ( aa918b...5cc05a )
by Luke
02:46
created

QueueWorkBatchCommand::fire()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 0
cts 9
cp 0
rs 9.4285
cc 3
eloc 9
nc 3
nop 0
crap 12
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\QueueManager;
17
use Illuminate\Queue\Worker;
18
use LukeWaite\LaravelQueueAwsBatch\Exceptions\JobNotFoundException;
19
use LukeWaite\LaravelQueueAwsBatch\Exceptions\UnsupportedException;
20
use LukeWaite\LaravelQueueAwsBatch\Queues\BatchQueue;
21
use Symfony\Component\Debug\Exception\FatalThrowableError;
22
23
class QueueWorkBatchCommand extends Command
24
{
25
    protected $name = 'queue:work-batch';
26
27
    protected $description = 'Run a Job for the AWS Batch queue';
28
29
    protected $signature = 'queue:work-batch {connection} {job_id} {--tries=}';
30
31
    protected $manager;
32
    protected $worker;
33
    protected $exceptions;
34
35
    public function __construct(QueueManager $manager, Worker $worker, Handler $exceptions)
36
    {
37
        $this->worker = $worker;
38
        $this->manager = $manager;
39
        $this->exceptions = $exceptions;
40
        parent::__construct();
41
    }
42
43
    public function fire()
44
    {
45
        try {
46
            $this->runJob();
47
        } catch (\Exception $e) {
48
            $this->exceptions->report($e);
49
            throw $e;
50
        } 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...
51
            $this->exceptions->report(new FatalThrowableError($e));
52
            throw $e;
53
        }
54
    }
55
56
    protected function runJob()
57
    {
58
        $maxTries = $this->option('tries');
59
        $delay = 0;
60
61
        $connectionName = $this->argument('connection');
62
        $jobId = $this->argument('job_id');
63
64
        /** @var BatchQueue $connection */
65
        $connection = $this->manager->connection($connectionName);
0 ignored issues
show
Bug introduced by
It seems like $connectionName defined by $this->argument('connection') on line 61 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...
66
67
        if (!$connection instanceof BatchQueue) {
68
            throw new UnsupportedException('queue:work-batch can only be run on batch queues');
69
        }
70
71
        $job = $connection->getJobById($jobId, $connectionName);
72
73
        // If we're able to pull a job off of the stack, we will process it and
74
        // then immediately return back out.
75
        if (!is_null($job)) {
76
            return $this->worker->process(
77
                $this->manager->getName($connectionName),
0 ignored issues
show
Bug introduced by
It seems like $connectionName defined by $this->argument('connection') on line 61 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...
78
                $job,
79
                $maxTries,
0 ignored issues
show
Documentation introduced by
$maxTries 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...
80
                $delay
81
            );
82
        }
83
84
        // If we hit this point, we haven't processed our job
85
        throw new JobNotFoundException('No job was returned');
86
    }
87
}
88