Passed
Pull Request — master (#464)
by Kirill
06:05
created

Decorator::pause()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Jobs\Queue;
6
7
use Spiral\Jobs\Options;
8
use Spiral\Jobs\QueueInterface;
9
use Spiral\RoadRunner\Jobs\Exception\JobsException;
10
use Spiral\RoadRunner\Jobs\JobsInterface;
11
use Spiral\RoadRunner\Jobs\OptionsInterface;
12
use Spiral\RoadRunner\Jobs\QueueInterface as RRQueueInterface;
13
use Spiral\RoadRunner\Jobs\Task\PreparedTaskInterface;
14
use Spiral\RoadRunner\Jobs\Task\QueuedTaskInterface;
15
16
class Decorator implements QueueInterface
17
{
18
    /**
19
     * @var RRQueueInterface
20
     */
21
    private RRQueueInterface $queue;
22
23
    /**
24
     * @var JobsInterface
25
     */
26
    private JobsInterface $jobs;
27
28
    /**
29
     * @param JobsInterface $jobs
30
     * @param RRQueueInterface $queue
31
     */
32
    public function __construct(JobsInterface $jobs, RRQueueInterface $queue)
33
    {
34
        $this->queue = $queue;
35
        $this->jobs = $jobs;
36
    }
37
38
    /**
39
     * {@inheritDoc}
40
     */
41
    public function getName(): string
42
    {
43
        return $this->queue->getName();
44
    }
45
46
    /**
47
     * {@inheritDoc}
48
     */
49
    public function getDefaultOptions(): OptionsInterface
50
    {
51
        return $this->queue->getDefaultOptions();
52
    }
53
54
    /**
55
     * {@inheritDoc}
56
     */
57
    public function withDefaultOptions(?OptionsInterface $options): RRQueueInterface
58
    {
59
        return new self($this->jobs, $this->queue->withDefaultOptions($options));
60
    }
61
62
    /**
63
     * {@inheritDoc}
64
     */
65
    public function create(string $name, array $payload = [], OptionsInterface $options = null): PreparedTaskInterface
66
    {
67
        return $this->getContext($options)
68
            ->create($name, $payload, $options)
69
        ;
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     */
75
    public function dispatch(PreparedTaskInterface $task): QueuedTaskInterface
76
    {
77
        return $this->queue->dispatch($task);
78
    }
79
80
    /**
81
     * {@inheritDoc}
82
     */
83
    public function dispatchMany(PreparedTaskInterface ...$tasks): iterable
84
    {
85
        return $this->queue->dispatchMany(...$tasks);
86
    }
87
88
    /**
89
     * {@inheritDoc}
90
     */
91
    public function pause(): void
92
    {
93
        $this->queue->pause();
94
    }
95
96
    /**
97
     * {@inheritDoc}
98
     */
99
    public function resume(): void
100
    {
101
        $this->queue->resume();
102
    }
103
104
    /**
105
     * {@inheritDoc}
106
     */
107
    public function isPaused(): bool
108
    {
109
        return $this->queue->isPaused();
110
    }
111
112
    /**
113
     * {@inheritDoc}
114
     * @throws JobsException
115
     */
116
    public function push(string $name, array $payload = [], OptionsInterface $options = null): string
117
    {
118
        $context = $this->getContext($options);
119
120
        $task = $context->dispatch($context->create($name, $payload, $options));
121
122
        return $task->getId();
123
    }
124
125
    /**
126
     * @param OptionsInterface|null $options
127
     * @return QueueInterface
128
     */
129
    private function getContext(?OptionsInterface $options): QueueInterface
130
    {
131
        if ($options instanceof Options && $options->getPipeline() !== null) {
132
            $original = $this->jobs->connect($options->getPipeline());
133
134
            return new self($this->jobs, $original);
135
        }
136
137
        return $this;
138
    }
139
}
140