PosixLocatorFactory::create()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 1
nop 1
1
<?php
2
namespace Nubs\Which\LocatorFactory;
3
4
use Habitat\Environment\Environment;
5
use Nubs\Which\Locator;
6
use Nubs\Which\PathBuilder\PosixPathBuilder;
7
8
/**
9
 * Locator factory for POSIXy systems (e.g. Linux, OSX, BSD).
10
 */
11
class PosixLocatorFactory implements LocatorFactoryInterface
12
{
13
    /**
14
     * Create a locator from the PATH environment variable.
15
     *
16
     * @api
17
     * @param \Habitat\Environment\Environment $environment The environment
18
     *     variable wrapper.  Defaults to null which just uses PHP's built-in
19
     *     getenv.
20
     * @return \Nubs\Which\Locator The locator.
21
     */
22
    public function create(Environment $environment = null)
23
    {
24
        return $this->createFromPath($environment ? $environment->getenv('PATH') : getenv('PATH'));
25
    }
26
27
    /**
28
     * Create a locator using a colon-separated PATH string.
29
     *
30
     * The colon is a hard separator.  This means that command paths cannot have
31
     * a colon in the name.
32
     *
33
     * @api
34
     * @param string $path The colon-separated PATH string.
35
     * @return \Nubs\Which\Locator The locator.
36
     */
37
    public function createFromPath($path)
38
    {
39
        return new Locator(new PosixPathBuilder(array_filter(explode(':', $path))));
40
    }
41
}
42