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

PrefetchMessageCache::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
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