Completed
Push — master ( 9a0903...3da072 )
by Krystian
02:07
created

FileUtility::locateLocalBinaryPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace SourceBroker\DeployerExtendedDatabase\Utility;
4
5
use function Deployer\runLocally;
6
7
/**
8
 * Class FileUtility
9
 * @package SourceBroker\DeployerExtendedDatabase\Utility
10
 */
11
class FileUtility
12
{
13
    /**
14
     * @param $filename
15
     * @return string
16
     */
17
    public function normalizeFilename($filename)
18
    {
19
        return preg_replace('/[^a-zA-Z0-9_]+/', '', $filename);
20
    }
21
22
    /**
23
     * @param $folder
24
     * @return string
25
     */
26
    public function normalizeFolder($folder)
27
    {
28
        return rtrim($folder, '/') . '/';
29
    }
30
31
    /**
32
     * @param $name
33
     * @return string
34
     */
35
    function locateLocalBinaryPath($name)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
36
    {
37
        $nameEscaped = escapeshellarg($name);
38
        // Try `command`, should cover all Bourne-like shells
39
        // Try `which`, should cover most other cases
40
        // Fallback to `type` command, if the rest fails
41
        $path = runLocally("command -v $nameEscaped || which $nameEscaped || type -p $nameEscaped");
42
        if ($path) {
43
            // Deal with issue when `type -p` outputs something like `type -ap` in some implementations
44
            return trim(str_replace("$name is", "", $path));
45
        }
46
        throw new \RuntimeException("Can't locate [$nameEscaped] on instance '" . get('default_stage') . "' - neither of [command|which|type] commands are available");
47
    }
48
}
49