Test Failed
Branch test (ad29d6)
by Tom
02:27
created

PackageYamlFileReader::resolveUri()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 9
c 1
b 0
f 1
nc 4
nop 1
dl 0
loc 19
rs 9.9666
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\Runner\Docker\Binary;
6
7
use Ktomk\Pipelines\LibFs;
8
use Ktomk\Pipelines\Yaml\Yaml;
9
10
/**
11
 * Class BinaryPackageYmlReader
12
 *
13
 * Read binary package information from a YAML file
14
 */
15
class PackageYamlFileReader implements PackageInterface
16
{
17
    /**
18
     * @var string
19
     */
20
    private $file;
21
22
    /**
23
     * BinaryPackageYmlReader constructor.
24
     *
25
     * @param string $file
26
     */
27
    public function __construct($file)
28
    {
29
        $this->file = LibFs::normalizePathSegments($file);
30
    }
31
32
    /**
33
     * @return array
34
     */
35
    public function asPackageArray()
36
    {
37
        $path = $this->file;
38
        if (!LibFs::isReadableFile($path)) {
39
            throw new \InvalidArgumentException(
40
                sprintf("not a readable file: '%s'", $path)
41
            );
42
        }
43
        $package = Yaml::buffer(file_get_contents($path));
44
45
        $this->resolveUri($package['uri']);
46
47
        return $package;
48
    }
49
50
    /**
51
     * resolve local path URI relative to document location
52
     *
53
     * @param string $uri
54
     */
55
    private function resolveUri(&$uri)
56
    {
57
        if (null === $uri) {
0 ignored issues
show
introduced by
The condition null === $uri is always false.
Loading history...
58
            return;
59
        }
60
61
        // http/s is the only remote transport considered to be remote so far
62
        // as it is the only one used
63
        if (1 === preg_match('(^https?://)i', $uri)) {
64
            return;
65
        }
66
67
        if (LibFs::isAbsolutePath($uri)) {
68
            return;
69
        }
70
71
        $baseDir = dirname($this->file);
72
        $buffer = $baseDir . '/' . $uri;
73
        $uri = LibFs::normalizePathSegments($buffer);
74
    }
75
}
76