Passed
Pull Request — master (#464)
by Kirill
12:51
created

ShortCircuit   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 45
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A push() 0 12 3
A serialize() 0 3 1
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Jobs;
13
14
/**
15
 * Runs all the jobs in the same process.
16
 */
17
final class ShortCircuit implements QueueInterface
18
{
19
    /** @var int */
20
    private $id = 0;
21
22
    /** @var HandlerRegistryInterface */
23
    private $registry;
24
25
    /** @var SerializerRegistryInterface */
26
    private $serializerRegistry;
27
28
    /**
29
     * @param HandlerRegistryInterface $registry
30
     */
31
    public function __construct(HandlerRegistryInterface $registry, SerializerRegistryInterface $serializerRegistry)
32
    {
33
        $this->registry = $registry;
34
        $this->serializerRegistry = $serializerRegistry;
35
    }
36
37
    /**
38
     * @inheritdoc
39
     */
40
    public function push(string $jobType, array $payload = [], Options $options = null): string
41
    {
42
        $payloadBody = $this->serialize($jobType, $payload);
43
        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...
44
            sleep($options->getDelay());
45
        }
46
47
        $id = (string)(++$this->id);
48
49
        $this->registry->getHandler($jobType)->handle($jobType, $id, $payloadBody);
50
51
        return $id;
52
    }
53
54
    /**
55
     * @param string $jobType
56
     * @param array  $payload
57
     * @return string
58
     */
59
    private function serialize(string $jobType, array $payload): string
60
    {
61
        return $this->serializerRegistry->getSerializer($jobType)->serialize($jobType, $payload);
62
    }
63
}
64