summaryrefslogtreecommitdiff
path: root/vendor/fguillot/simple-queue/src/Adapter/MemoryQueueAdapter.php
blob: 36375d5b156fb830784becb0e8fa45ac58657e30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php

namespace SimpleQueue\Adapter;

use DateTime;
use Exception;
use SimpleQueue\Exception\NotSupportedException;
use SimpleQueue\QueueAdapterInterface;
use SimpleQueue\Job;
use SplQueue;

/**
 * Class MemoryAdapter
 *
 * @package SimpleQueue\Adapter
 */
class MemoryQueueAdapter implements QueueAdapterInterface
{
    /**
     * @var SplQueue
     */
    protected $queue;

    /**
     * MemoryAdapter constructor.
     */
    public function __construct()
    {
        $this->queue = new SplQueue();
    }

    /**
     * Send a job
     *
     * @access public
     * @param  Job $job
     * @return $this
     */
    public function push(Job $job)
    {
        $this->queue->enqueue($job->serialize());
        return $this;
    }

    /**
     * Schedule a job in the future
     *
     * @access public
     * @param  Job      $job
     * @param  DateTime $dateTime
     * @return bool
     * @throws NotSupportedException
     */
    public function schedule(Job $job, DateTime $dateTime)
    {
        throw new NotSupportedException('Job delay is not supported by MemoryQueue');
    }

    /**
     * Wait and get job from a queue
     *
     * @access public
     * @return Job|null
     */
    public function pull()
    {
        try {
            $job = new Job();
            $payload = $this->queue->dequeue();
            return $job->unserialize($payload);
        } catch (Exception $e) {
            return null;
        }
    }

    /**
     * Acknowledge a job
     *
     * @access public
     * @param  Job $job
     * @return $this
     */
    public function completed(Job $job)
    {
        return $this;
    }

    /**
     * Mark a job as failed
     *
     * @access public
     * @param  Job $job
     * @return $this
     */
    public function failed(Job $job)
    {
        $this->queue->enqueue($job->serialize());
        return $this;
    }
}