Completed
Pull Request — master (#25)
by Rémi
02:21
created

HandlerBuilder   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 92.5%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 7
dl 0
loc 143
ccs 37
cts 40
cp 0.925
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A autoAck() 0 6 1
A sync() 0 6 1
A async() 0 6 1
A doNotRequeueOnFailure() 0 6 1
A continueOnFailure() 0 6 1
A log() 0 6 1
A build() 0 23 4
1
<?php
2
3
namespace Burrow\Handler;
4
5
use Assert\Assertion;
6
use Assert\AssertionFailedException;
7
use Burrow\Driver;
8
use Burrow\QueueConsumer;
9
use Burrow\QueueHandler;
10
use Psr\Log\LoggerInterface;
11
use Psr\Log\NullLogger;
12
13
class HandlerBuilder
14
{
15
    /** @var Driver */
16
    private $driver;
17
18
    /** @var bool */
19
    private $autoAck;
20
21
    /** @var bool */
22
    private $sync;
23
24
    /** @var bool */
25
    private $requeueOnFailure;
26
27
    /** @var bool */
28
    private $stopOnFailure;
29
30
    /** @var LoggerInterface */
31
    private $logger;
32
33
    /**
34
     * HandlerBuilder constructor.
35
     *
36
     * @param Driver $driver
37
     */
38 9
    public function __construct(Driver $driver)
39
    {
40 9
        $this->driver = $driver;
41
42 9
        $this->autoAck = false;
43
44 9
        $this->sync = null;
45 9
        $this->requeueOnFailure = true;
46 9
        $this->stopOnFailure = true;
47
48 9
        $this->logger = new NullLogger();
49 9
    }
50
51
    /**
52
     * Keeps the autoAcking behavior
53
     */
54
    public function autoAck()
55
    {
56
        $this->autoAck = true;
57
58
        return $this->doNotRequeueOnFailure(); // cannot be re-queued if it's auto-acked
59
    }
60
61
    /**
62
     * Build a sync Handler.
63
     *
64
     * @return $this
65
     */
66 3
    public function sync()
67
    {
68 3
        $this->sync = true;
69
70 3
        return $this;
71
    }
72
73
    /**
74
     * Build an async Handler.
75
     *
76
     * @return $this
77
     */
78 3
    public function async()
79
    {
80 3
        $this->sync = false;
81
82 3
        return $this;
83
    }
84
85
    /**
86
     * Must the failed message be requeued.
87
     *
88
     * @return $this
89
     */
90 3
    public function doNotRequeueOnFailure()
91
    {
92 3
        $this->requeueOnFailure = false;
93
94 3
        return $this;
95
    }
96
97
    /**
98
     * Must the handler continue on failure
99
     *
100
     * @return $this
101
     */
102 3
    public function continueOnFailure()
103
    {
104 3
        $this->stopOnFailure = false;
105
106 3
        return $this;
107
    }
108
109
    /**
110
     * Set a logger.
111
     *
112
     * @param LoggerInterface $logger
113
     *
114
     * @return $this
115
     */
116 6
    public function log(LoggerInterface $logger)
117
    {
118 6
        $this->logger = $logger;
119
120 6
        return $this;
121
    }
122
123
    /**
124
     * Build the Handler.
125
     *
126
     * @param QueueConsumer $consumer
127
     *
128
     * @return QueueHandler
129
     *
130
     * @throws AssertionFailedException
131
     */
132 9
    public function build(QueueConsumer $consumer)
133
    {
134 9
        Assertion::notNull($this->sync, 'You must specify if the handler must be sync or async');
135
136
        // Sync
137 6
        $handler = $this->sync ?
138 4
            new SyncConsumerHandler($consumer, $this->driver) :
139 6
            new AsyncConsumerHandler($consumer);
140 6
        $handler->setLogger($this->logger);
141
142
        // Ack
143 6
        $handler = $this->autoAck ?
144 2
            $handler :
145 6
            new AckHandler($handler, $this->driver, $this->requeueOnFailure);
146
147
        // Stop / Continue
148 6
        $handler = $this->stopOnFailure ?
149 4
            new StopOnExceptionHandler($handler) :
150 6
            new ContinueOnExceptionHandler($handler);
151 6
        $handler->setLogger($this->logger);
152
153 6
        return $handler;
154
    }
155
}
156