Passed
Push — test ( 9a8848...0c4d14 )
by Tom
02:23
created

Directories::getProject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\Runner;
6
7
use InvalidArgumentException;
8
9
/**
10
 * Class Directories
11
 *
12
 * Provides common directories (as path-names) for the pipelines project
13
 *
14
 * @package Ktomk\Pipelines\Runner
15
 */
16
class Directories
17
{
18
    /**
19
     * @var string
20
     */
21
    private $project;
22
23
    /**
24
     * @var array
25
     */
26
    private $env;
27
28
    /**
29
     * Directories constructor.
30
     *
31
     * FIXME(tk) require $HOME in $env is kind of wrong, for the XDG_* family there are more fallback/s this class
32
     *           should adhere to - @see Directories::getBaseDirectory
33
     *
34
     * @param array $env
35
     * @param string $project directory
36
     */
37 11
    public function __construct(array $env, $project)
38
    {
39 11
        if (!basename($project)) {
40 1
            throw new InvalidArgumentException(sprintf('Invalid project directory "%s"', $project));
41
        }
42
43 10
        $this->project = $project;
44
45 10
        if (!isset($env['HOME'])) {
46
            /**
47
             * @throws \InvalidArgumentException
48
             */
49 1
            throw new InvalidArgumentException('No $HOME in environment');
50
        }
51
52 9
        $this->env = $env;
53 9
    }
54
55
    /**
56
     * basename of the project directory
57
     *
58
     * @return string
59
     */
60 2
    public function getName()
61
    {
62 2
        return basename($this->project);
63
    }
64
65
    /**
66
     * @return string
67
     */
68 1
    public function getProject()
69
    {
70 1
        return $this->project;
71
    }
72
73
    /**
74
     * FIXME(tk) unused (only in tests!)
75
     *
76
     * @return string
77
     */
78 1
    public function getPipelineLocalDeploy()
79
    {
80 1
        return sprintf('%s/.pipelines/%s', $this->env['HOME'], $this->getName());
81
    }
82
83
    /**
84
     * Pipelines base directories
85
     *
86
     * In the style of the (here weakly referenced) XDG Base Directory specs.
87
     *
88
     * example: XDG_CONFIG_HOME is $HOME/.config
89
     *
90
     * TODO(tk): System-wide fall-backs in case $HOME is undefined/empty @see Directories::__construct
91
     * @link https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
92
     *
93
     * @param string $type name .
94
     * @param null|string $suffix
95
     * @return string
96
     */
97 5
    public function getBaseDirectory($type, $suffix = null)
98
    {
99 5
        static $paths = array(
100
            'XDG_DATA_HOME' => '$HOME/.local/share',
101
            'XDG_CACHE_HOME' => '$HOME/.cache',
102
        );
103
104 5
        if (!isset($paths[$type])) {
105 1
            throw new \InvalidArgumentException(sprintf(
106 1
                'Not a base directory: %s. Known base directories are: "%s"',
107 1
                var_export($type, true),
108 1
                implode('", "', array_keys($paths))
109
            ));
110
        }
111
112 4
        $buffer = null;
113 4
        if (isset($this->env[$type])) {
114 1
            $buffer = $this->env[$type];
115
        } else {
116 3
            $home = $this->env['HOME'];
117 3
            $buffer = substr_replace($paths[$type], $home, 0, strlen('$HOME'));
118
        }
119
120 4
        null === $suffix || $buffer .= '/' . $suffix;
121
122 4
        return $buffer;
123
    }
124
}
125