ListStrategy::get()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 0
1
<?php
2
namespace Nubs\Sensible\Strategy;
3
4
/**
5
 * Executes each strategy in turn until one of them returns a non-null result.
6
 */
7
class ListStrategy implements StrategyInterface
8
{
9
    /** @type \Nubs\Sensible\StrategyInterface[] The strategies to use. */
10
    private $_strategies;
11
12
    /**
13
     * Initialize the strategy to wrap a list of alternate strategies.
14
     *
15
     * @param \Nubs\Sensible\StrategyInterface[] $strategies The strategies to
16
     *     use.
17
     */
18
    public function __construct(array $strategies)
19
    {
20
        $this->_strategies = $strategies;
21
    }
22
23
    /**
24
     * Returns the result of the first successful strategy.
25
     *
26
     * @return string|null The command as located by a strategy or null if no
27
     *     strategies found a command to use.
28
     */
29
    public function get()
30
    {
31
        foreach ($this->_strategies as $strategy) {
32
            $result = $strategy->get();
33
            if ($result !== null) {
34
                return $result;
35
            }
36
        }
37
38
        return null;
39
    }
40
}
41