Client::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of graze/queue.
5
 *
6
 * Copyright (c) 2015 Nature Delivered Ltd. <https://www.graze.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @license https://github.com/graze/queue/blob/master/LICENSE MIT
12
 *
13
 * @link    https://github.com/graze/queue
14
 */
15
16
namespace Graze\Queue;
17
18
use Graze\Queue\Adapter\AdapterInterface;
19
use Graze\Queue\Handler\BatchAcknowledgementHandler;
20
use Graze\Queue\Message\MessageFactory;
21
use Graze\Queue\Message\MessageFactoryInterface;
22
use Graze\Queue\Message\MessageInterface;
23
24
final class Client implements ConsumerInterface, DeleterInterface, ProducerInterface, PurgerInterface
25
{
26
    /** @var AdapterInterface */
27
    protected $adapter;
28
29
    /** @var MessageFactoryInterface */
30
    protected $factory;
31
32
    /** @var callable */
33
    protected $handler;
34
35
    /**
36
     * @param AdapterInterface $adapter
37
     * @param array            $config - handler <callable> Handler to apply a worker to a list of messages
38
     *                                 and determine when to send acknowledgement.
39
     *                                 - message_factory <MessageFactoryInterface> Factory used to create
40
     *                                 messages.
41
     */
42 22
    public function __construct(AdapterInterface $adapter, array $config = [])
43
    {
44 22
        $this->adapter = $adapter;
45
46 22
        $this->handler = isset($config['handler'])
47 22
            ? $config['handler']
48 22
            : $this->createDefaultHandler();
49
50 22
        $this->factory = isset($config['message_factory'])
51 22
            ? $config['message_factory']
52 22
            : $this->createDefaultMessageFactory();
53 22
    }
54
55
    /**
56
     * @param string $body
57
     * @param array  $options
58
     *
59
     * @return MessageInterface
60
     */
61 5
    public function create($body, array $options = [])
62
    {
63 5
        return $this->factory->createMessage($body, $options);
64
    }
65
66
    /**
67
     * @param callable $worker
68
     * @param int      $limit
69
     */
70 12
    public function receive(callable $worker, $limit = 1)
71
    {
72 12
        $messages = $this->adapter->dequeue($this->factory, $limit);
73
74 12
        call_user_func($this->handler, $messages, $this->adapter, $worker);
75 12
    }
76
77
    /**
78
     * @param MessageInterface[] $messages
79
     *
80
     * @return mixed
81
     */
82 5
    public function send(array $messages)
83
    {
84 5
        return $this->adapter->enqueue($messages);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->adapter->enqueue($messages) targeting Graze\Queue\Adapter\AdapterInterface::enqueue() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 3
    public function purge()
91
    {
92 3
        $this->adapter->purge();
93 3
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 3
    public function delete()
99
    {
100 3
        $this->adapter->delete();
101 3
    }
102
103
    /**
104
     * @return callable
105
     */
106 16
    protected function createDefaultHandler()
107
    {
108 16
        return new BatchAcknowledgementHandler();
109
    }
110
111
    /**
112
     * @return MessageFactoryInterface
113
     */
114 16
    protected function createDefaultMessageFactory()
115
    {
116 16
        return new MessageFactory();
117
    }
118
}
119