ChainExitStrategy::shouldExit()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.4286
cc 3
eloc 6
nc 3
nop 1
crap 3
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 9
    public function __construct(array $exitStrategies = [])
21
    {
22 9
        foreach ($exitStrategies as $exitStrategy) {
23 4
            $this->addExitStrategy($exitStrategy);
24 9
        }
25 9
    }
26
27 8
    public function addExitStrategy(ExitStrategy $exitStrategy)
28
    {
29 8
        $this->exitStrategies[] = $exitStrategy;
30 8
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 7
    public function shouldExit($count)
36
    {
37 7
        foreach ($this->exitStrategies as $exitStrategy) {
38 7
            if ($exitStrategy->shouldExit($count)) {
39 6
                $this->reason = $exitStrategy->getReason();
40
41 6
                return true;
42
            }
43 4
        }
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