Passed
Push — test ( d48a8e...8face6 )
by Tom
02:59
created

PackageYamlFileReader::__construct()   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 1
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\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 path to package YAML file to read
26
     */
27 5
    public function __construct($file)
28
    {
29 5
        $this->file = LibFs::normalizePath($file);
30 5
    }
31
32
    /**
33
     * @throws \InvalidArgumentException
34
     * @return array
35
     */
36 5
    public function asPackageArray()
37
    {
38 5
        $path = $this->file;
39 5
        if (!LibFs::isReadableFile($path)) {
40 1
            throw new \InvalidArgumentException(
41 1
                sprintf("not a readable file: '%s'", $path)
42
            );
43
        }
44 4
        $package = Yaml::buffer(file_get_contents($path));
45
46 4
        $this->resolveUri($package['uri']);
47
48 4
        return $package;
49
    }
50
51
    /**
52
     * resolve local path URI relative to document location
53
     *
54
     * @param null|string $uri
55
     */
56 4
    private function resolveUri(&$uri)
57
    {
58 4
        if (null === $uri) {
59 1
            return;
60
        }
61
62
        // http/s is the only remote transport considered to be remote so far
63
        // as it is the only one used
64 3
        if (1 === preg_match('(^https?://)i', $uri)) {
65 1
            return;
66
        }
67
68 2
        if (LibFs::isAbsolutePath($uri)) {
69 1
            return;
70
        }
71
72
        // TODO(tk): maybe this whole part as it's resolving?
73 1
        $baseDir = dirname($this->file);
74 1
        $buffer = $baseDir . '/' . $uri;
75 1
        $uri = LibFs::normalizePath($buffer);
76 1
    }
77
}
78