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

Sf2Yaml   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 16
dl 0
loc 67
ccs 18
cts 18
cp 1
rs 10
c 1
b 0
f 1
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isAvailable() 0 3 1
A parseFile() 0 3 1
A tryParseBuffer() 0 9 2
A parseBuffer() 0 14 3
A tryParseFile() 0 3 1
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