ResultAcknowledgementHandler   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
dl 0
loc 82
ccs 12
cts 18
cp 0.6667
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 1
A acknowledge() 0 9 2
A flush() 0 3 1
A extend() 0 6 1
A reject() 0 6 1
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\Handler;
17
18
use Graze\Queue\Adapter\AdapterInterface;
19
use Graze\Queue\Message\MessageInterface;
20
21
class ResultAcknowledgementHandler extends AbstractAcknowledgementHandler
22
{
23
    /** @var callable */
24
    private $validator;
25
26
    /** @var AbstractAcknowledgementHandler */
27
    private $handler;
28
29
    /**
30
     * ResultAcknowledgementHandler constructor.
31
     *
32
     * @param callable                       $validator
33
     * @param AbstractAcknowledgementHandler $handler
34
     */
35 3
    public function __construct(callable $validator, AbstractAcknowledgementHandler $handler)
36
    {
37
        /**
38
         * This callable should accept the mixed result returned by a worker
39
         * and return a boolean value.
40
         *
41
         * @var callable
42
         */
43 3
        $this->validator = $validator;
44
45
        /**
46
         * The handler to call `acknowledge` or `reject` on if {@see $validator} returns a
47
         * truthy value for the given result.
48
         *
49
         * @var AbstractAcknowledgementHandler
50
         */
51 3
        $this->handler = $handler;
52 3
    }
53
54
    /**
55
     * @param MessageInterface $message
56
     * @param AdapterInterface $adapter
57
     * @param mixed            $result
58
     */
59 3
    protected function acknowledge(
60
        MessageInterface $message,
61
        AdapterInterface $adapter,
62
        $result = null
63
    ) {
64 3
        if (call_user_func($this->validator, $result) === true) {
65 2
            $this->handler->acknowledge($message, $adapter, $result);
66 2
        } else {
67 1
            $this->handler->reject($message, $adapter, $result);
68
        }
69 3
    }
70
71
    /**
72
     * @param MessageInterface $message
73
     * @param AdapterInterface $adapter
74
     * @param int              $duration Number of seconds to ensure that this message is not seen by any other clients
75
     */
76
    protected function extend(
77
        MessageInterface $message,
78
        AdapterInterface $adapter,
79
        $duration
80
    ) {
81
        $this->handler->extend($message, $adapter, $duration);
82
    }
83
84
    /**
85
     * @param MessageInterface $message
86
     * @param AdapterInterface $adapter
87
     * @param mixed            $result
88
     */
89
    protected function reject(
90
        MessageInterface $message,
91
        AdapterInterface $adapter,
92
        $result = null
93
    ) {
94
        $this->handler->reject($message, $adapter, $result);
95
    }
96
97
    /**
98
     * @param AdapterInterface $adapter
99
     */
100 3
    protected function flush(AdapterInterface $adapter)
101
    {
102 3
        $this->handler->flush($adapter);
103 3
    }
104
}
105