Completed
Push — master ( 613d98...fa04c9 )
by Rémi
20:40
created

HandlerBuilder::async()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Burrow\Handler;
4
5
use Assert\Assertion;
6
use Burrow\Driver;
7
use Burrow\QueueConsumer;
8
use Burrow\QueueHandler;
9
use Psr\Log\LoggerInterface;
10
use Psr\Log\NullLogger;
11
12
class HandlerBuilder
13
{
14
    /** @var Driver */
15
    private $driver;
16
17
    /** @var bool */
18
    private $sync;
19
20
    /** @var bool */
21
    private $requeueOnFailure;
22
23
    /** @var bool */
24
    private $stopOnFailure;
25
26
    /** @var LoggerInterface */
27
    private $logger;
28
29
    /**
30
     * HandlerBuilder constructor.
31
     *
32
     * @param Driver $driver
33
     */
34
    public function __construct(Driver $driver)
35
    {
36
        $this->driver = $driver;
37
        $this->consumer = null;
0 ignored issues
show
Bug introduced by
The property consumer does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
38
39
        $this->sync = null;
40
        $this->requeueOnFailure = true;
41
        $this->stopOnFailure = true;
42
43
        $this->logger = new NullLogger();
44
    }
45
46
    /**
47
     * Build a sync Handler.
48
     *
49
     * @return $this
50
     */
51
    public function sync()
52
    {
53
        $this->sync = true;
54
55
        return $this;
56
    }
57
58
    /**
59
     * Build an async Handler.
60
     *
61
     * @return $this
62
     */
63
    public function async()
64
    {
65
        $this->sync = false;
66
67
        return $this;
68
    }
69
70
    /**
71
     * Must the failed message be requeued.
72
     *
73
     * @return $this
74
     */
75
    public function doNotRequeueOnFailure()
76
    {
77
        $this->requeueOnFailure = false;
78
79
        return $this;
80
    }
81
82
    /**
83
     * Must the handler continue on failure
84
     *
85
     * @return $this
86
     */
87
    public function continueOnFailure()
88
    {
89
        $this->stopOnFailure = false;
90
91
        return $this;
92
    }
93
94
    /**
95
     * Set a logger.
96
     *
97
     * @param LoggerInterface $logger
98
     *
99
     * @return $this
100
     */
101
    public function log(LoggerInterface $logger)
102
    {
103
        $this->logger = $logger;
104
105
        return $this;
106
    }
107
108
    /**
109
     * Build the Handler.
110
     *
111
     * @param QueueConsumer $consumer
112
     *
113
     * @return QueueHandler
114
     */
115
    public function build(QueueConsumer $consumer)
116
    {
117
        Assertion::notNull($this->sync, 'You must specify if the handler must be sync or async');
118
119
        $syncAsync = ($this->sync) ?
120
            new SyncConsumerHandler($consumer, $this->driver) :
121
            new AsyncConsumerHandler($consumer);
122
123
        $ackHandler = new AckHandler($syncAsync, $this->driver, $this->requeueOnFailure);
124
125
        $handler = ($this->stopOnFailure) ?
126
            new StopOnExceptionHandler($ackHandler) :
127
            new ContinueOnExceptionHandler($ackHandler);
128
129
        $syncAsync->setLogger($this->logger);
130
        $handler->setLogger($this->logger);
131
132
        return $handler;
133
    }
134
}
135