Completed
Pull Request — master (#2)
by
unknown
38:19
created

Queue::setConnectionName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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 $queue
61
     *
62
     * @return mixed
63
     */
64
    public function bulk(array $jobs, array $data = [], ?string $queue = null)
65
    {
66
        foreach ((array)$jobs as $job) {
67
            $this->push($job, $data, $queue);
68
        }
69
    }
70
71
    /**
72
     * Create a payload string from the given job and data.
73
     *
74
     * @param string $job
75
     * @param mixed $data
76
     *
77
     * @return string
78
     *
79
     * @throws InvalidPayloadException
80
     */
81
    protected function createPayload(string $job, array $data = [])
82
    {
83
        $payload = json_encode($this->createPayloadArray($job, $data));
84
85
        if (JSON_ERROR_NONE !== json_last_error()) {
86
            throw new InvalidPayloadException(
87
                'Unable to JSON encode payload. Error code: ' . json_last_error()
88
            );
89
        }
90
91
        return $payload;
92
    }
93
94
    /**
95
     * Create a payload array from the given job and data.
96
     *
97
     * @param string $job
98
     * @param array $data
99
     *
100
     * @return array
101
     */
102
    protected function createPayloadArray(string $job, array $data = []): array
103
    {
104
        return [
105
            'displayName' => explode('@', $job)[0],
106
            'job' => $job,
107
            'maxTries' => null,
108
            'timeout' => null,
109
            'data' => $data,
110
        ];
111
    }
112
113
    /**
114
     * Get the connection name for the queue.
115
     *
116
     * @return string
117
     */
118
    public function getConnectionName(): string
119
    {
120
        return $this->connectionName;
121
    }
122
123
    /**
124
     * Set the connection name for the queue.
125
     *
126
     * @param string $name
127
     *
128
     * @return $this
129
     */
130
    public function setConnectionName(string $name)
131
    {
132
        $this->connectionName = $name;
133
134
        return $this;
135
    }
136
}
137