Completed
Push — master ( 2e8a4c...06111f )
by Olivier
17s queued 12s
created

src/Swarrot/Driver/PrefetchMessageCache.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Swarrot\Driver;
4
5
use Swarrot\Broker\Message;
6
7
/**
8
 * Class PrefetchMessageCache.
9
 */
10
class PrefetchMessageCache implements MessageCacheInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Swarrot\Driver\MessageCacheInterface has been deprecated.

This class, trait or interface has been deprecated.

Loading history...
11
{
12
    protected $caches = [];
13
14
    public function __construct()
15
    {
16
        @trigger_error(sprintf('"%s" have been deprecated since Swarrot 3.7', __CLASS__), E_USER_DEPRECATED);
17
    }
18
19
    /**
20
     * Pushes a message to the end of the cache.
21
     *
22
     * @param string $queueName
23
     */
24
    public function push($queueName, Message $message)
25
    {
26
        $cache = $this->get($queueName);
27
        $cache->enqueue($message);
28
    }
29
30
    /**
31
     * Get the next message in line. Or nothing if there is no more
32
     * in the cache.
33
     *
34
     * @param string $queueName
35
     *
36
     * @return Message|null
37
     */
38
    public function pop($queueName)
39
    {
40
        $cache = $this->get($queueName);
41
42
        if (!$cache->isEmpty()) {
43
            return $cache->dequeue();
44
        }
45
    }
46
47
    /**
48
     * Create the queue cache internally if it doesn't yet exists.
49
     *
50
     * @param string $queueName
51
     *
52
     * @return \SplQueue
53
     */
54
    protected function get($queueName)
55
    {
56
        if (isset($this->caches[$queueName])) {
57
            return $this->caches[$queueName];
58
        }
59
60
        return $this->caches[$queueName] = new \SplQueue();
61
    }
62
}
63