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

Sf2Yaml::tryParseFile()   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 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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 Symfony\Component\Yaml\Exception\ParseException as SymfonyParseException;
8
use Symfony\Component\Yaml\Yaml as SymfonyYaml;
9
10
class Sf2Yaml implements ParserInterface
11
{
12
    /**
13
     * @return bool
14
     */
15 1
    public static function isAvailable()
16
    {
17 1
        return true;
18
    }
19
20
    /**
21
     * @param $path
22
     *
23
     * @throws ParseException
24
     *
25
     * @return array
26
     */
27 2
    public function parseFile($path)
28
    {
29 2
        return Yaml::fileDelegate($path, array($this, 'parseBuffer'));
30
    }
31
32
    /**
33
     * @param string $path
34
     *
35
     * @return null|array
36
     */
37 3
    public function tryParseFile($path)
38
    {
39 3
        return Yaml::fileDelegate($path, array($this, 'tryParseBuffer'));
40
    }
41
42
    /**
43
     * @param $buffer
44
     *
45
     * @throws ParseException
46
     *
47
     * @return array
48
     */
49 3
    public function parseBuffer($buffer)
50
    {
51
        try {
52 3
            $result = SymfonyYaml::parse($buffer);
53 1
        } catch (SymfonyParseException $ex) {
54 1
            throw new ParseException($ex->getMessage(), 0, $ex);
55
        }
56
57
        # catch sf2 invalid yaml parsing
58 3
        if (array(':') === $result) {
59 1
            throw new ParseException('Sf2Yaml invalid YAML parsing', 0);
60
        }
61
62 2
        return $result;
63
    }
64
65
    /**
66
     * @param string $buffer
67
     *
68
     * @return null|array
69
     */
70 3
    public function tryParseBuffer($buffer)
71
    {
72
        try {
73 3
            $result = $this->parseBuffer($buffer);
74 2
        } catch (ParseException $ex) {
75 2
            return null;
76
        }
77
78 2
        return $result;
79
    }
80
}
81