CommandLocatorStrategy   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 37
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A get() 0 11 3
1
<?php
2
namespace Nubs\Sensible\Strategy;
3
4
use Nubs\Which\Locator as CommandLocator;
5
6
/**
7
 * Uses a command locator to check a list of commands for one that exists.
8
 */
9
class CommandLocatorStrategy implements StrategyInterface
10
{
11
    /** @type array The list of commands to try locating. */
12
    private $_commands;
13
14
    /** @type \Nubs\Which\Locator The command locator. */
15
    private $_commandLocator;
16
17
    /**
18
     * Initialize the strategy to locate a suitable command from a list.
19
     *
20
     * @param string[] $commands A list of commands to look for.
21
     * @param \Nubs\Which\Locator The command locator.
22
     */
23
    public function __construct(array $commands, CommandLocator $commandLocator)
24
    {
25
        $this->_commands = $commands;
26
        $this->_commandLocator = $commandLocator;
27
    }
28
29
    /**
30
     * Returns the path to the first command found in the list.
31
     *
32
     * @return string|null The located command, or null if none could be found.
33
     */
34
    public function get()
35
    {
36
        foreach ($this->_commands as $command) {
37
            $location = $this->_commandLocator->locate(basename($command));
38
            if ($location !== null) {
39
                return $location;
40
            }
41
        }
42
43
        return null;
44
    }
45
}
46