Completed
Pull Request — master (#15)
by Alex
08:30
created

MessageHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Cmp\Queues\Infrastructure\AWS\v20121105\Queue;
4
5
use Cmp\Queues\Domain\Queue\Exception\ReaderException;
6
use Cmp\Queues\Domain\Queue\JSONMessageFactory;
7
8
class MessageHandler
9
{
10
    /**
11
     * @var JSONMessageFactory
12
     */
13
    private $jsonMessageFactory;
14
15
    /**
16
     * @var callable
17
     */
18
    private $callback;
19
20
    /**
21
     * @param JSONMessageFactory $jsonMessageFactory
22
     */
23
    public function __construct(JSONMessageFactory $jsonMessageFactory)
24
    {
25
        $this->jsonMessageFactory = $jsonMessageFactory;
26
    }
27
28
    /**
29
     * @param array $message
30
     *
31
     * @throws ReaderException
32
     */
33
    public function handleMessage(array $message)
34
    {
35
        if (!isset($this->callback)) {
36
            throw new ReaderException("Handling a message with no callback set");
37
        }
38
39
        $body = json_decode($message['Body'], true);
40
        $task = $this->jsonMessageFactory->create($body['Message']);
41
        call_user_func($this->callback, $task);
42
    }
43
44
    /**
45
     * @param callable $callback
46
     */
47
    public function setCallback(callable $callback)
48
    {
49
        $this->callback = $callback;
50
    }
51
}