Completed
Pull Request — master (#3)
by Kevin
03:40
created

ChainExitStrategy::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.1481

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 2
cts 3
cp 0.6667
rs 9.4286
cc 2
eloc 3
nc 2
nop 1
crap 2.1481
1
<?php
2
3
namespace Zenstruck\Queue\Subscriber\ExitStrategy;
4
5
use Zenstruck\Queue\Subscriber\ExitStrategy;
6
7
/**
8
 * @author Kevin Bond <[email protected]>
9
 */
10
class ChainExitStrategy implements ExitStrategy
11
{
12
    private $reason;
13
14
    /** @var array|ExitStrategy[] */
15
    private $exitStrategies = [];
16
17
    /**
18
     * @param array|ExitStrategy[] $exitStrategies
19
     */
20 4
    public function __construct(array $exitStrategies = [])
21
    {
22
        foreach ($exitStrategies as $exitStrategy) {
23
            $this->addExitStrategy($exitStrategy);
24 4
        }
25
    }
26
27 3
    public function addExitStrategy(ExitStrategy $exitStrategy)
28
    {
29 3
        $this->exitStrategies[] = $exitStrategy;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 7
    public function shouldExit($count)
36
    {
37 7
        foreach ($this->exitStrategies as $exitStrategy) {
38
            if ($exitStrategy->shouldExit($count)) {
39
                $this->reason = $exitStrategy->getReason();
40
41 6
                return true;
42
            }
43 3
        }
44
45 1
        return false;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 6
    public function getReason()
52
    {
53 6
        return $this->reason;
54
    }
55
}
56