Completed
Push — fix-data-subdir ( 6bac1e )
by Arnaud
03:01 queued 10s
created

DataLoad   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 8
dl 0
loc 72
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 6 2
A process() 0 45 3
A pathToArray() 0 10 2
1
<?php
2
/*
3
 * Copyright (c) Arnaud Ligny <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Cecil\Step;
10
11
use Symfony\Component\Finder\Finder;
12
use Symfony\Component\Yaml\Yaml;
13
14
/**
15
 * Load data files.
16
 */
17
class DataLoad extends AbstractStep
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function init($options)
23
    {
24
        if (is_dir($this->builder->getConfig()->getDataPath())) {
25
            $this->process = true;
26
        }
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function process()
33
    {
34
        call_user_func_array($this->builder->getMessageCb(), ['DATA', 'Loading data']);
35
36
        $data = Finder::create()
37
            ->files()
38
            ->in($this->builder->getConfig()->getDataPath())
39
            ->name('/\.('.implode('|', $this->builder->getConfig()->get('data.ext')).')$/')
40
            ->sortByName(true);
41
42
        $count = 0;
43
        $max = count($data);
44
45
        /* @var $file \Symfony\Component\Finder\SplFileInfo */
46
        foreach ($data as $file) {
47
            $count++;
48
            set_error_handler(
49
                function ($severity, $message, $file, $line) {
50
                    throw new \ErrorException($message, 0, $severity, $file, $line, null);
51
                }
52
            );
53
            $dataFile = $file->getContents();
54
            restore_error_handler();
55
            $dataArray = Yaml::parse($dataFile);
56
57
            $basename = $file->getBasename('.' . $file->getExtension());
58
            $subpath = \Cecil\Util::getFS()->makePathRelative(
59
                $file->getPath(),
60
                $this->builder->getConfig()->getDataPath()
61
            );
62
            $subpath = trim($subpath, "./");
63
            $array = [];
64
            $path = $subpath ? $subpath . '/' . $basename : $basename;
65
            $this->pathToArray($array, $path, $dataArray);
66
67
            $dataArray = array_merge_recursive(
68
                $this->builder->getData(),
69
                $array
70
            );
71
            $this->builder->setData($dataArray);
72
73
            $message = sprintf('"%s" loaded', $path);
74
            call_user_func_array($this->builder->getMessageCb(), ['DATA_PROGRESS', $message, $count, $max]);
75
        }
76
    }
77
78
    private function pathToArray(&$arr, $path, $value, $separator = '/')
79
    {
80
        $keys = explode($separator, $path);
81
82
        foreach ($keys as $key) {
83
            $arr = &$arr[$key];
84
        }
85
86
        $arr = $value;
87
    }
88
}
89