Passed
Push — master ( dca89f...91c2eb )
by Anton
02:26
created

JobQueue::push()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 6
c 1
b 1
f 0
dl 0
loc 12
rs 10
cc 4
nc 4
nop 3
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
use Spiral\Goridge\RPC;
15
16
/**
17
 * Provides the ability to automatically specify the job pipeline.
18
 */
19
final class JobQueue implements QueueInterface
20
{
21
    /** @var Queue */
22
    private $queue;
23
24
    /** @var JobRegistry */
25
    private $registry;
26
27
    /**
28
     * @param RPC $rpc
29
     * @param JobRegistry $registry
30
     */
31
    public function __construct(RPC $rpc, JobRegistry $registry)
32
    {
33
        $this->queue = new Queue($rpc, $registry);
34
        $this->registry = $registry;
35
    }
36
37
    /**
38
     * @param string $jobType
39
     * @param array $payload
40
     * @param Options|null $options
41
     * @return string
42
     */
43
    public function push(string $jobType, array $payload = [], Options $options = null): string
44
    {
45
        if ($options === null) {
46
            $options = new Options();
47
        }
48
49
        $pipeline = $this->registry->getPipeline($jobType);
50
        if ($pipeline !== null && $options->getPipeline() === null) {
51
            $options = $options->withPipeline($pipeline);
52
        }
53
54
        return $this->queue->push($jobType, $payload, $options);
55
    }
56
}
57