Passed
Push — master ( 3f0369...26e86a )
by Tom
04:24
created

Yaml::tryFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\Yaml;
6
7
use Ktomk\Pipelines\LibFsStream;
8
9
class Yaml
10
{
11
    /**
12
     * @var array of Yaml parser classes
13
     *
14
     * @see ParserInterface
15
     * @see Yaml::parser()
16
     */
17
    public static $classes = array();
18
19
    /**
20
     * @param string $file
21
     *
22
     * @throws ParseException
23
     * @throws \InvalidArgumentException
24
     *
25
     * @return array
26
     */
27 3
    public static function file($file)
28
    {
29 3
        $path = '-' === $file ? 'php://stdin' : $file;
30
31 3
        $path = LibFsStream::fdToPhp($path);
32
33 3
        if (!LibFsStream::isReadable($path)) {
34 1
            throw new \InvalidArgumentException(
35 1
                sprintf("not a readable file: '%s'", $file)
36
            );
37
        }
38
39 2
        return self::parser()->parseFile($path);
40
    }
41
42
    /**
43
     * @param string $file
44
     *
45
     * @throws \InvalidArgumentException
46
     *
47
     * @return null|array
48
     */
49 1
    public static function tryFile($file)
50
    {
51
        try {
52 1
            return self::file($file);
53 1
        } catch (ParseException $ex) {
54 1
            return null;
55
        }
56
    }
57
58
    /**
59
     * error-free (suppressed) file_get_contents()
60
     *
61
     * available for implementations aligned with {@see Yaml::file()}
62
     * that already checks readability before getting the contents.
63
     *
64
     * fall-back on file-reading error is null.
65
     *
66
     * @param string $path
67
     * @param callable $parseBuffer function(string $buffer): array|null
68
     *
69
     * @return null|array
70
     */
71 4
    public static function fileDelegate($path, $parseBuffer)
72
    {
73 4
        $buffer = @file_get_contents($path);
74
75 4
        return false === $buffer ? null : call_user_func($parseBuffer, $buffer);
76
    }
77
78
    /**
79
     * @param string $buffer
80
     *
81
     * @return null|array
82
     */
83 2
    public static function buffer($buffer)
84
    {
85 2
        return self::parser()->tryParseBuffer($buffer);
86
    }
87
88
    /**
89
     * @return ParserInterface
90
     */
91 4
    private static function parser()
92
    {
93 4
        $classes = self::$classes ?: array(
94 3
            'Ktomk\Pipelines\Yaml\LibYaml',
95
            'Ktomk\Pipelines\Yaml\Sf2Yaml',
96
        );
97
98 4
        foreach ($classes as $class) {
99
            if (
100 4
                class_exists($class)
101 4
                && is_subclass_of($class, 'Ktomk\Pipelines\Yaml\ParserInterface')
102 4
                && $class::isAvailable()
103
            ) {
104 3
                return new $class();
105
            }
106
        }
107
108 1
        throw new \BadMethodCallException(sprintf('No YAML parser available'));
109
    }
110
}
111