PackageYamlFileReader   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 19
c 1
b 0
f 1
dl 0
loc 64
ccs 20
cts 20
cp 1
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A asPackageArray() 0 13 2
A resolveUri() 0 20 4
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\LibFsPath;
9
use Ktomk\Pipelines\Yaml\Yaml;
10
11
/**
12
 * Class BinaryPackageYmlReader
13
 *
14
 * Read binary package information from a YAML file
15
 */
16
class PackageYamlFileReader implements PackageInterface
17
{
18
    /**
19
     * @var string
20
     */
21
    private $file;
22
23
    /**
24
     * BinaryPackageYmlReader constructor.
25
     *
26
     * @param string $file path to package YAML file to read
27
     */
28 5
    public function __construct($file)
29
    {
30 5
        $this->file = LibFsPath::normalize($file);
31
    }
32
33
    /**
34
     * @throws \InvalidArgumentException
35
     *
36
     * @return array
37
     */
38 5
    public function asPackageArray()
39
    {
40 5
        $path = $this->file;
41 5
        if (!LibFs::isReadableFile($path)) {
42 1
            throw new \InvalidArgumentException(
43 1
                sprintf("not a readable file: '%s'", $path)
44
            );
45
        }
46 4
        $package = (array)Yaml::buffer(file_get_contents($path));
47
48 4
        $this->resolveUri($package['uri']);
49
50 4
        return $package;
51
    }
52
53
    /**
54
     * resolve local path URI relative to document location
55
     *
56
     * @param null|string $uri
57
     *
58
     * @return void
59
     */
60 4
    private function resolveUri(&$uri)
61
    {
62 4
        if (null === $uri) {
63 1
            return;
64
        }
65
66
        // http/s is the only remote transport considered to be remote so far
67
        // as it is the only one used
68 3
        if (1 === preg_match('(^https?://)i', $uri)) {
69 1
            return;
70
        }
71
72 2
        if (LibFsPath::isAbsolute($uri)) {
73 1
            return;
74
        }
75
76
        // TODO(tk): maybe this whole part as it's resolving?
77 1
        $baseDir = dirname($this->file);
78 1
        $buffer = $baseDir . '/' . $uri;
79 1
        $uri = LibFsPath::normalize($buffer);
80
    }
81
}
82