Passed
Push — test ( 0895d9...6e4feb )
by Tom
02:26
created

Directories::getPipelineLocalDeploy()   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
use Ktomk\Pipelines\LibFs;
9
use Ktomk\Pipelines\Project;
10
use Ktomk\Pipelines\Utility\App;
11
12
/**
13
 * Pipelines directories.
14
 *
15
 * Provides common directories (as path-names) for the pipelines project,
16
 * making use of environment parameters like HOME and some from the
17
 * XDG Base Directory Specification (XDGBDS).
18
 *
19
 * @link https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
20
 * @link https://wiki.debian.org/XDGBaseDirectorySpecification
21
 * @link https://wiki.archlinux.org/index.php/XDG_Base_Directory
22
 *
23
 * @package Ktomk\Pipelines\Runner
24
 */
25
class Directories
26
{
27
    /**
28
     * @var array environment parameters
29
     */
30
    private $env;
31
32
    /**
33
     * @var Project
34
     */
35
    private $project;
36
37
    /**
38
     * @var string name of the utility to have directories for (XDG)
39
     */
40
    private $utility;
41
42
    /**
43
     * @var array|string[]
44
     */
45
    private static $baseDirectory = array(
46
        'XDG_DATA_HOME' => '$HOME/.local/share',
47
        'XDG_CACHE_HOME' => '$HOME/.cache',
48
        'XDG_CONFIG_HOME' => '$HOME/.config',
49
    );
50
51
    /**
52
     * Directories ctor
53
     *
54
     * @param array|string[] $env
55
     * @param Project $project project
56
     * @param string $utility [optional] name, defaults to "pipelines"
57
     *
58
     * @throws InvalidArgumentException
59
     *
60
     *@see Directories::getBaseDirectory
61
     */
62 11
    public function __construct(array $env, Project $project, $utility = null)
63
    {
64 11
        if (!isset($env['HOME']) || '' === $env['HOME']) {
65 1
            throw new InvalidArgumentException('$HOME unset or empty');
66
        }
67
68 10
        null === $utility && $utility = App::UTILITY_NAME;
69 10
        if (!LibFs::isPortableFilename($utility)) {
70 1
            throw new InvalidArgumentException(sprintf('Not a portable utility name: "%s"', $utility));
71
        }
72
73 9
        $this->env = $env;
74 9
        $this->project = $project;
75 9
        $this->utility = $utility;
76 9
    }
77
78
    /**
79
     * basename of the project directory
80
     *
81
     * @return string
82
     */
83 2
    public function getName()
84
    {
85 2
        return (string)$this->project;
86
    }
87
88
    /**
89
     * Get project directory.
90
     *
91
     * project is the base-folder (root-folder) of the project which
92
     * is run by pipelines.
93
     *
94
     * @return string
95
     */
96 1
    public function getProjectDirectory()
97
    {
98 1
        return $this->project->getPath();
99
    }
100
101
    /**
102
     * FIXME(tk) unused (only in tests!) - reason: yet undecided what / when
103
     * to put the local deploy stuff in there. needs XDGBDS reading and
104
     * some more considerations as more likely more data is needed to store
105
     * in the file-system. But better use XDGDBS here.
106
     *
107
     * @return string
108
     */
109 1
    public function getPipelineLocalDeploy()
110
    {
111 1
        return sprintf('%s/.%s/%s', $this->env['HOME'], $this->utility, $this->getName());
112
    }
113
114
    /**
115
     * Pipelines base directories
116
     *
117
     * In the style of the (here weakly referenced) XDG Base Directory specs.
118
     *
119
     * Examples:
120
     *
121
     *      XDG_DATA_HOME is $HOME/.local/share if unset or empty
122
     *      XDG_CACHE_HOME is $HOME/.cache if unset or empty
123
     *      XDG_CONFIG_HOME is $HOME/.config if unset or empty
124
     *
125
     * @link https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
126
     * @link https://wiki.debian.org/XDGBaseDirectorySpecification
127
     * @link https://wiki.archlinux.org/index.php/XDG_Base_Directory
128
     *
129
     * @param string $type name XDG_DATA_HOME / XDG_CACHE_HOME / XDG_CONFIG_HOME
130
     * @param null|string $suffix
131
     *
132
     * @return string
133
     */
134 5
    public function getBaseDirectory($type, $suffix = null)
135
    {
136 5
        $paths = self::$baseDirectory;
137
138 5
        $this->validateBaseDirectory($type);
139
140 4
        $buffer = null;
141 4
        if (isset($this->env[$type])) {
142 1
            $buffer = $this->env[$type];
143
        } else {
144 3
            $home = $this->env['HOME'];
145 3
            $buffer = substr_replace($paths[$type], $home, 0, strlen('$HOME'));
146
        }
147
148 4
        $buffer .= '/' . $this->utility;
149
150 4
        null === $suffix || $buffer .= '/' . $suffix;
151
152 4
        return $buffer;
153
    }
154
155
    /**
156
     * @param string $xdgName
157
     *
158
     * @return void
159
     */
160 5
    private function validateBaseDirectory($xdgName)
161
    {
162 5
        $paths = self::$baseDirectory;
163
164 5
        if (!isset($paths[$xdgName])) {
165 1
            throw new InvalidArgumentException(sprintf(
166 1
                'Not a base directory: %s. Known base directories are: "%s"',
167 1
                var_export($xdgName, true),
168 1
                implode('", "', array_keys($paths))
169
            ));
170
        }
171 4
    }
172
}
173