WindowsLocatorFactory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
c 2
b 0
f 0
lcom 0
cbo 3
dl 0
loc 39
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 7 3
A createFromPath() 0 6 1
1
<?php
2
namespace Nubs\Which\LocatorFactory;
3
4
use Habitat\Environment\Environment;
5
use Nubs\Which\Locator;
6
use Nubs\Which\PathBuilder\WindowsPathBuilder;
7
8
/**
9
 * Locator factory for Windows systems.
10
 */
11
class WindowsLocatorFactory implements LocatorFactoryInterface
12
{
13
    /**
14
     * Create a locator from the PATH and PATHEXT environment variables.
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(
25
            $environment ? $environment->getenv('PATH') : getenv('PATH'),
26
            $environment ? $environment->getenv('PATHEXT') : getenv('PATHEXT')
27
        );
28
    }
29
30
    /**
31
     * Create a locator using semicolon-separated PATH and PATHEXT strings.
32
     *
33
     * The semicolon is a hard separator.  This means that command paths cannot
34
     * have a semicolon in the name.
35
     *
36
     * The current working directory is always prepended to the list of paths.
37
     *
38
     * @api
39
     * @param string $path The semicolon-separated PATH string.
40
     * @param string $pathext The semicolon-separated PATHEXT string.
41
     * @return \Nubs\Which\Locator The locator.
42
     */
43
    public function createFromPath($path, $pathext)
44
    {
45
        $paths = array_merge(['.'], array_filter(explode(';', $path)));
46
        $extensions = array_merge([''], array_filter(explode(';', $pathext)));
47
        return new Locator(new WindowsPathBuilder($paths, $extensions));
48
    }
49
}
50