Passed
Pull Request — master (#1107)
by Maxim
20:56
created

SyncDriver::push()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4.0072

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 18
ccs 12
cts 13
cp 0.9231
rs 9.9
c 0
b 0
f 0
cc 4
nc 2
nop 3
crap 4.0072
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Queue\Driver;
6
7
use Ramsey\Uuid\Uuid;
8
use Spiral\Queue\ExtendedOptionsInterface;
9
use Spiral\Queue\Interceptor\Consume\Handler;
10
use Spiral\Queue\OptionsInterface;
11
use Spiral\Queue\QueueInterface;
12
use Spiral\Queue\QueueTrait;
13
14
/**
15
 * Runs all the jobs in the same process.
16
 */
17
final class SyncDriver implements QueueInterface
18
{
19
    use QueueTrait;
20
21 7
    public function __construct(
22
        private readonly Handler $coreHandler
23
    ) {
24 7
    }
25
26
    /** @inheritdoc */
27 6
    public function push(string $name, mixed $payload = [], OptionsInterface $options = null): string
28
    {
29 6
        if ($options !== null && $options->getDelay()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $options->getDelay() of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
30
            \sleep($options->getDelay());
31
        }
32
33 6
        $id = (string)Uuid::uuid4();
34
35 6
        $this->coreHandler->handle(
36 6
            name: $name,
37 6
            driver: 'sync',
38 6
            queue: 'default',
39 6
            id: $id,
40 6
            payload: $payload,
41 6
            headers: $options instanceof ExtendedOptionsInterface ? $options->getHeaders() : [],
42 6
        );
43
44 6
        return $id;
45
    }
46
}
47