RuntimeHelper::setupHome()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 8.8571
cc 5
eloc 8
nc 5
nop 1
1
<?php
2
3
/**
4
 * This file is part of tenside/core.
5
 *
6
 * (c) Christian Schiffler <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * This project is provided in good faith and hope to be usable by anyone.
12
 *
13
 * @package    tenside/core
14
 * @author     Christian Schiffler <[email protected]>
15
 * @copyright  2015 Christian Schiffler <[email protected]>
16
 * @license    https://github.com/tenside/core/blob/master/LICENSE MIT
17
 * @link       https://github.com/tenside/core
18
 * @filesource
19
 */
20
21
namespace Tenside\Core\Util;
22
23
/**
24
 * Generic helper method collection for being able to provide an web entry point and an cli entry point.
25
 */
26
class RuntimeHelper
27
{
28
    /**
29
     * Detect the correct tenside home dir and set the environment variable.
30
     *
31
     * @param string $home The home directory.
32
     *
33
     * @return void
34
     *
35
     * @throws \InvalidArgumentException For empty value of $home.
36
     */
37
    public static function setupHome($home)
38
    {
39
        if (empty($home)) {
40
            throw new \InvalidArgumentException('Empty home directory encountered.');
41
        }
42
43
        if (false === getenv('COMPOSER')) {
44
            putenv('COMPOSER=' . $home . DIRECTORY_SEPARATOR . 'composer.json');
45
        }
46
        chdir($home);
47
48
        // Ensure at least one of the environment variables is available.
49
        if (!getenv('COMPOSER_HOME') && !getenv('HOME')) {
50
            putenv('COMPOSER_HOME=' . $home);
51
        }
52
    }
53
}
54