Passed
Push — test ( 33612f...4075b0 )
by Tom
12:36
created

Sf2Yaml::tryParseBuffer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
ccs 5
cts 5
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 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
     * @return array
24
     * @throws ParseException
25
     */
26 2
    public function parseFile($path)
27
    {
28 2
        return Yaml::fileDelegate($path, array($this, 'parseBuffer'));
29
    }
30
31
    /**
32
     * @param string $path
33
     *
34
     * @return null|array
35
     */
36 3
    public function tryParseFile($path)
37
    {
38 3
        return Yaml::fileDelegate($path, array($this, 'tryParseBuffer'));
39
    }
40
41
    /**
42
     * @param $buffer
43
     *
44
     * @return array
45
     * @throws ParseException
46
     */
47 3
    public function parseBuffer($buffer)
48
    {
49
        try {
50 3
            $result = SymfonyYaml::parse($buffer);
51 1
        } catch (SymfonyParseException $ex) {
52 1
            throw new ParseException($ex->getMessage(), 0, $ex);
53
        }
54
55
        # catch sf2 invalid yaml parsing
56 3
        if (array(':') === $result) {
57 1
            throw new ParseException('Sf2Yaml invalid YAML parsing', 0);
58
        }
59
60 2
        return $result;
61
    }
62
63
    /**
64
     * @param string $buffer
65
     *
66
     * @return null|array
67
     */
68 3
    public function tryParseBuffer($buffer)
69
    {
70
        try {
71 3
            $result = $this->parseBuffer($buffer);
72 2
        } catch (ParseException $ex) {
73 2
            return null;
74
        }
75
76 2
        return $result;
77
    }
78
}
79