Passed
Pull Request — main (#42)
by Luke
09:39
created

QueueWorkBatchCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 6
Bugs 2 Features 0
Metric Value
wmc 8
eloc 35
c 6
b 2
f 0
dl 0
loc 82
ccs 0
cts 30
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A gatherWorkerOptions() 0 9 1
A __construct() 0 5 1
A fire() 0 12 3
A runJob() 0 26 3
1
<?php
2
3
/**
4
 * Laravel Queue for AWS Batch.
5
 *
6
 * @author    Luke Waite <[email protected]>
7
 * @copyright 2017 Luke Waite
8
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
9
 *
10
 * @link      https://github.com/lukewaite/laravel-queue-aws-batch
11
 */
12
13
namespace LukeWaite\LaravelQueueAwsBatch\Console;
14
15
use Illuminate\Console\Command;
16
use Illuminate\Foundation\Exceptions\Handler;
17
use Illuminate\Queue\Console\WorkCommand;
18
use Illuminate\Queue\QueueManager;
19
use Illuminate\Queue\Worker;
20
use Illuminate\Queue\WorkerOptions;
21
use LukeWaite\LaravelQueueAwsBatch\Exceptions\JobNotFoundException;
22
use LukeWaite\LaravelQueueAwsBatch\Exceptions\UnsupportedException;
23
use LukeWaite\LaravelQueueAwsBatch\Queues\BatchQueue;
24
use Symfony\Component\Debug\Exception\FatalThrowableError;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Debug\...ion\FatalThrowableError was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
26
class QueueWorkBatchCommand extends WorkCommand
27
{
28
    protected $name = 'queue:work-batch';
29
30
    protected $description = 'Run a Job for the AWS Batch queue';
31
32
    protected $signature = 'queue:work-batch
33
                            {job_id : The job id in the database}
34
                            {connection? : The name of the queue connection to work}
35
                            {--memory=128 : The memory limit in megabytes}
36
                            {--timeout=60 : The number of seconds a child process can run}
37
                            {--tries=0 : Number of times to attempt a job before logging it failed}';
38
39
40
    protected $manager;
41
    protected $exceptions;
42
43
    public function __construct(QueueManager $manager, Worker $worker, Handler $exceptions)
44
    {
45
        parent::__construct($worker);
0 ignored issues
show
Bug introduced by
The call to Illuminate\Queue\Console...kCommand::__construct() has too few arguments starting with cache. ( Ignorable by Annotation )

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

45
        parent::/** @scrutinizer ignore-call */ 
46
                __construct($worker);

This check compares calls to functions or methods with their respective definitions. If the call has less 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. Please note the @ignore annotation hint above.

Loading history...
46
        $this->manager = $manager;
47
        $this->exceptions = $exceptions;
48
    }
49
50
    public function fire()
51
    {
52
        $this->listenForEvents();
53
54
        try {
55
            $this->runJob();
56
        } catch (\Exception $e) {
57
            $this->exceptions->report($e);
58
            throw $e;
59
        } catch (\Throwable $e) {
60
            $this->exceptions->report(new FatalThrowableError($e));
61
            throw $e;
62
        }
63
    }
64
65
    // TOOD: Refactor out the logic here into an extension of the Worker class
66
    protected function runJob()
67
    {
68
        $connectionName = $this->argument('connection');
69
        $jobId = $this->argument('job_id');
70
71
        /** @var BatchQueue $connection */
72
        $connection = $this->manager->connection($connectionName);
0 ignored issues
show
Bug introduced by
It seems like $connectionName can also be of type array; however, parameter $name of Illuminate\Queue\QueueManager::connection() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

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

72
        $connection = $this->manager->connection(/** @scrutinizer ignore-type */ $connectionName);
Loading history...
73
74
        if (!$connection instanceof BatchQueue) {
0 ignored issues
show
introduced by
$connection is always a sub-type of LukeWaite\LaravelQueueAwsBatch\Queues\BatchQueue.
Loading history...
75
            throw new UnsupportedException('queue:work-batch can only be run on batch queues');
76
        }
77
78
        $job = $connection->getJobById($jobId, $connectionName);
0 ignored issues
show
Unused Code introduced by
The call to LukeWaite\LaravelQueueAw...atchQueue::getJobById() has too many arguments starting with $connectionName. ( Ignorable by Annotation )

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

78
        /** @scrutinizer ignore-call */ 
79
        $job = $connection->getJobById($jobId, $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. Please note the @ignore annotation hint above.

Loading history...
79
80
        // If we're able to pull a job off of the stack, we will process it and
81
        // then immediately return back out.
82
        if (!is_null($job)) {
83
            return $this->worker->process(
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->worker->process($...>gatherWorkerOptions()) targeting Illuminate\Queue\Worker::process() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
84
                $this->manager->getName($connectionName),
0 ignored issues
show
Bug introduced by
It seems like $connectionName can also be of type array; however, parameter $connection of Illuminate\Queue\QueueManager::getName() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

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

84
                $this->manager->getName(/** @scrutinizer ignore-type */ $connectionName),
Loading history...
85
                $job,
86
                $this->gatherWorkerOptions()
87
            );
88
        }
89
90
        // If we hit this point, we haven't processed our job
91
        throw new JobNotFoundException('No job was returned');
92
    }
93
94
    /**
95
     * Gather all of the queue worker options as a single object.
96
     *
97
     * @return \Illuminate\Queue\WorkerOptions
98
     */
99
    protected function gatherWorkerOptions()
100
    {
101
        return new WorkerOptions(
102
            0,
103
            $this->option('memory'),
104
            $this->option('timeout'),
105
            0,
106
            $this->option('tries'),
107
            false
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type integer expected by parameter $maxTries of Illuminate\Queue\WorkerOptions::__construct(). ( Ignorable by Annotation )

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

107
            /** @scrutinizer ignore-type */ false
Loading history...
108
        );
109
    }
110
}
111