Test Failed
Push — master ( a58c94...1bcec0 )
by Brent
03:26
created

YamlParser::parse()   B

Complexity

Conditions 5
Paths 14

Size

Total Lines 28
Code Lines 18

Duplication

Lines 6
Ratio 21.43 %

Importance

Changes 0
Metric Value
cc 5
eloc 18
nc 14
nop 1
dl 6
loc 28
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace Brendt\Stitcher\Parser;
4
5
use Brendt\Stitcher\Exception\ParserException;
6
use Symfony\Component\Finder\Finder;
7
use Symfony\Component\Yaml\Exception\ParseException;
8
use Symfony\Component\Yaml\Yaml;
9
use Brendt\Stitcher\Config;
10
11
/**
12
 * The YamlParser take a path to one or more YAML files, and parses the content into an array.
13
 *
14
 * @see \Brendt\Stitcher\Parser\AbstractArrayParser::parseArrayData()
15
 */
16
class YamlParser extends AbstractArrayParser {
17
18
    /**
19
     * @param string $path
20
     *
21
     * @return mixed
22
     * @throws ParserException
23
     */
24
    public function parse($path = '*.yml') {
25
        if (!strpos($path, '.yml')) {
26
            $path .= '.yml';
27
        }
28
29
        $root = Config::get('directories.src');
30
        $files = Finder::create()->files()->in($root)->path($path);
31
        $yamlData = [];
32
33
        foreach ($files as $file) {
34
            try {
35
                $parsed = Yaml::parse($file->getContents());
36
37 View Code Duplication
                if (isset($parsed['entries'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
                    $yamlData += $parsed['entries'];
39
                } else {
40
                    $id = str_replace(".{$file->getExtension()}", '', $file->getFilename());
41
                    $yamlData[$id] = $parsed;
42
                }
43
            } catch (ParseException $e) {
44
                throw new ParserException("{$file->getRelativePathname()}: {$e->getMessage()}");
45
            }
46
        }
47
48
        $parsedEntries = $this->parseArrayData($yamlData);
49
50
        return $parsedEntries;
51
    }
52
53
}
54