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

FolderParser::parse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 2
nop 1
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
1
<?php
2
3
namespace Brendt\Stitcher\Parser;
4
5
use Brendt\Stitcher\Config;
6
use Brendt\Stitcher\Factory\ParserFactory;
7
use Symfony\Component\Finder\Finder;
8
use Symfony\Component\Finder\SplFileInfo;
9
10
/**
11
 * The FolderParser take the path to a folder and will read all files in that folder, parsing each of the files
12
 * individually.
13
 */
14
class FolderParser implements Parser {
15
16
    /**
17
     * @var ParserFactory
18
     */
19
    private $parserFactory;
20
21
    /**
22
     * FolderParser constructor
23
     */
24
    public function __construct() {
25
        $this->parserFactory = Config::getDependency('factory.parser');
26
    }
27
28
    /**
29
     * @param $path
30
     *
31
     * @return array
32
     */
33
    public function parse($path) {
34
        $path = trim($path, '/');
35
        $root = Config::get('directories.src');
36
        $files = Finder::create()->files()->in("{$root}/data/{$path}")->name('*.*')->sort(function(SplFileInfo $a, SplFileInfo $b) {
37
            return strcmp($b->getRelativePath(), $a->getRelativePath());
38
        });
39
        $data = [];
40
41
        foreach ($files as $file) {
42
            $parser = $this->parserFactory->getParser($file->getFilename());
43
44
            $id = str_replace(".{$file->getExtension()}", '', $file->getFilename());
45
46
            $data[$id] = [
47
                'id'      => $id,
48
                'content' => $parser->parse($file->getRelativePathname()),
49
            ];
50
        }
51
52
        return $data;
53
    }
54
}
55