Queue::createPayloadArray()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace SfCod\QueueBundle\Queue;
4
5
use SfCod\QueueBundle\Base\InteractWithTimeTrait;
6
use SfCod\QueueBundle\Exception\InvalidPayloadException;
7
8
/**
9
 * Class Queue
10
 *
11
 * @author Virchenko Maksim <[email protected]>
12
 *
13
 * @package SfCod\QueueBundle\Queue
14
 */
15
abstract class Queue implements QueueInterface
16
{
17
    use InteractWithTimeTrait;
18
19
    /**
20
     * The connection name for the queue.
21
     *
22
     * @var string
23
     */
24
    protected $connectionName;
25
26
    /**
27
     * Push a new job onto the queue.
28
     *
29
     * @param string $queue
30
     * @param string $job
31
     * @param array $data
32
     *
33
     * @return mixed
34
     */
35
    public function pushOn(string $queue, string $job, array $data = [])
36
    {
37
        return $this->push($job, $data, $queue);
38
    }
39
40
    /**
41
     * Push a new job onto the queue after a delay.
42
     *
43
     * @param string $queue
44
     * @param \DateTimeInterface|\DateInterval|int $delay
45
     * @param string $job
46
     * @param array $data
47
     *
48
     * @return mixed
49
     */
50
    public function laterOn(string $queue, $delay, string $job, array $data = [])
51
    {
52
        return $this->later($delay, $job, $data, $queue);
53
    }
54
55
    /**
56
     * Push an array of jobs onto the queue.
57
     *
58
     * @param array $jobs
59
     * @param array $data
60
     * @param string|null $queue
61
     */
62
    public function bulk(array $jobs, array $data = [], ?string $queue = null)
63
    {
64
        foreach ($jobs as $job) {
65
            $this->push($job, $data, $queue);
66
        }
67
    }
68
69
    /**
70
     * Create a payload string from the given job and data.
71
     *
72
     * @param string $job
73
     * @param mixed $data
74
     *
75
     * @return string
76
     *
77
     * @throws InvalidPayloadException
78
     */
79
    protected function createPayload(string $job, array $data = [])
80
    {
81
        $payload = json_encode($this->createPayloadArray($job, $data));
82
83
        if (JSON_ERROR_NONE !== json_last_error()) {
84
            throw new InvalidPayloadException('Unable to JSON encode payload. Error code: ' . json_last_error());
85
        }
86
87
        return $payload;
88
    }
89
90
    /**
91
     * Create a payload array from the given job and data.
92
     *
93
     * @param string $job
94
     * @param array $data
95
     *
96
     * @return array
97
     */
98
    protected function createPayloadArray(string $job, array $data = []): array
99
    {
100
        return [
101
            'displayName' => explode('@', $job)[0],
102
            'job' => $job,
103
            'maxTries' => null,
104
            'timeout' => null,
105
            'data' => $data,
106
        ];
107
    }
108
109
    /**
110
     * Get the connection name for the queue.
111
     *
112
     * @return string
113
     */
114
    public function getConnectionName(): string
115
    {
116
        return $this->connectionName;
117
    }
118
119
    /**
120
     * Set the connection name for the queue.
121
     *
122
     * @param string $name
123
     *
124
     * @return $this
125
     */
126
    public function setConnectionName(string $name): QueueInterface
127
    {
128
        $this->connectionName = $name;
129
130
        return $this;
131
    }
132
}
133