Sf2Yaml::parseBuffer()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 7
c 1
b 0
f 1
nc 3
nop 1
dl 0
loc 14
ccs 7
cts 7
cp 1
crap 3
rs 10
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\Yaml;
6
7
use Ktomk\Symfony\Component\Yaml\Exception\ParseException as SymfonyParseException;
8
use Ktomk\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 null|array
26
     */
27 4
    public function parseFile($path)
28
    {
29 4
        return Yaml::fileDelegate($path, array($this, 'parseBuffer'));
30
    }
31
32
    /**
33
     * @param string $path
34
     *
35
     * @return null|array
36
     */
37 5
    public function tryParseFile($path)
38
    {
39 5
        return Yaml::fileDelegate($path, array($this, 'tryParseBuffer'));
40
    }
41
42
    /**
43
     * @param $buffer
44
     *
45
     * @throws ParseException
46
     *
47
     * @return array
48
     */
49 5
    public function parseBuffer($buffer)
50
    {
51
        try {
52 5
            $result = SymfonyYaml::parse($buffer);
53 3
        } catch (SymfonyParseException $ex) {
54 3
            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 5
    public function tryParseBuffer($buffer)
71
    {
72
        try {
73 5
            $result = $this->parseBuffer($buffer);
74 4
        } catch (ParseException $ex) {
75 4
            return null;
76
        }
77
78 2
        return $result;
79
    }
80
}
81