Passed
Push — Assets/Image ( bd550f...7a290d )
by Arnaud
09:48 queued 04:01
created

DataLoad::process()   B

Complexity

Conditions 9
Paths 13

Size

Total Lines 76
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 53
nc 13
nop 0
dl 0
loc 76
rs 7.4698
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Serializer\Encoder\CsvEncoder;
13
use Symfony\Component\Serializer\Encoder\JsonEncoder;
14
use Symfony\Component\Serializer\Encoder\XmlEncoder;
15
use Symfony\Component\Serializer\Encoder\YamlEncoder;
16
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
17
use Symfony\Component\Serializer\Serializer;
18
19
/**
20
 * Load data files.
21
 */
22
class DataLoad extends AbstractStep
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function init($options)
28
    {
29
        if (is_dir($this->builder->getConfig()->getDataPath())) {
30
            $this->process = true;
31
        }
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function process()
38
    {
39
        call_user_func_array($this->builder->getMessageCb(), ['DATA', 'Loading data']);
40
41
        $files = Finder::create()
42
            ->files()
43
            ->in($this->builder->getConfig()->getDataPath())
44
            ->name('/\.('.implode('|', $this->builder->getConfig()->get('data.ext')).')$/')
0 ignored issues
show
Bug introduced by
It seems like $this->builder->getConfig()->get('data.ext') can also be of type null; however, parameter $pieces of implode() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
            ->name('/\.('.implode('|', /** @scrutinizer ignore-type */ $this->builder->getConfig()->get('data.ext')).')$/')
Loading history...
45
            ->sortByName(true);
46
        $max = count($files);
47
48
        if ($max <= 0) {
49
            $message = 'No files';
50
            call_user_func_array($this->builder->getMessageCb(), ['DATA_PROGRESS', $message]);
51
52
            return;
53
        }
54
55
        // YAML
56
        $serializerYaml = new Serializer([new ObjectNormalizer()], [new YamlEncoder()]);
57
        // JSON
58
        $serializerJson = new Serializer([new ObjectNormalizer()], [new JsonEncoder()]);
59
        // CSV
60
        $serializerCsv = new Serializer([new ObjectNormalizer()], [new CsvEncoder()]);
61
        // XML
62
        $serializerXml = new Serializer([new ObjectNormalizer()], [new XmlEncoder()]);
63
64
        $count = 0;
65
66
        /* @var $file \Symfony\Component\Finder\SplFileInfo */
67
        foreach ($files as $file) {
68
            $count++;
69
            set_error_handler(
70
                function ($severity, $message, $file, $line) {
71
                    throw new \ErrorException($message, 0, $severity, $file, $line, null);
72
                }
73
            );
74
            $data = $file->getContents();
75
            restore_error_handler();
76
77
            switch ($file->getExtension()) {
78
                case 'yml':
79
                case 'yaml':
80
                    $dataArray = $serializerYaml->decode($data, 'yaml');
81
                    break;
82
                case 'json':
83
                    $dataArray = $serializerJson->decode($data, 'json');
84
                    break;
85
                case 'csv':
86
                    $dataArray = $serializerCsv->decode($data, 'csv');
87
                    break;
88
                case 'xml':
89
                    $dataArray = $serializerXml->decode($data, 'xml');
90
                    break;
91
                default:
92
                    return;
93
            }
94
95
            $basename = $file->getBasename('.'.$file->getExtension());
96
            $subpath = \Cecil\Util::getFS()->makePathRelative(
97
                $file->getPath(),
98
                $this->builder->getConfig()->getDataPath()
99
            );
100
            $subpath = trim($subpath, './');
101
            $array = [];
102
            $path = $subpath ? $subpath.'/'.$basename : $basename;
103
            $this->pathToArray($array, $path, $dataArray);
104
105
            $dataArray = array_merge_recursive(
106
                $this->builder->getData(),
107
                $array
108
            );
109
            $this->builder->setData($dataArray);
110
111
            $message = sprintf('"%s" loaded', $path);
112
            call_user_func_array($this->builder->getMessageCb(), ['DATA_PROGRESS', $message, $count, $max]);
113
        }
114
    }
115
116
    private function pathToArray(&$arr, $path, $value, $separator = '/')
117
    {
118
        $keys = explode($separator, $path);
119
120
        foreach ($keys as $key) {
121
            $arr = &$arr[$key];
122
        }
123
124
        $arr = $value;
125
    }
126
}
127