Test Setup Failed
Pull Request — master (#14)
by he
07:42
created

DataProvider::getDataFiles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 11
rs 10
1
<?php
2
3
namespace App\Services;
4
5
use Illuminate\Filesystem\Filesystem;
6
use LogicException;
7
8
class DataProvider
9
{
10
    const TYPE_IN = '.in';
11
    const TYPE_OUT = '.out';
12
13
    public function getData($id)
14
    {
15
        $dataInput = $this->getInputFiles($id);
16
        $dataOutput = $this->getOutputFiles($id);
17
18
        $data = [];
19
        foreach ($dataInput as $name => $content) {
20
            if (!array_key_exists($name, $dataOutput)) {
21
                $message = 'Problem Data is not match!';
22
                app('log')->error($message, ['pid' => $id]);
23
24
                throw new LogicException($message);
25
            }
26
            $outContent = $dataOutput[$name];
27
            $data[] = [
28
                'input'  => $content,
29
                'output' => $outContent,
30
            ];
31
        }
32
33
        return $data;
34
    }
35
36
    /**
37
     * @param $id
38
     *
39
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
40
     *
41
     * @return array
42
     */
43
    public function getInputFiles($id)
44
    {
45
        $pattern = $this->getDataPath($id).'*'.self::TYPE_IN;
46
47
        return $this->getDataFiles($pattern);
48
    }
49
50
    /**
51
     * @param $id
52
     *
53
     * @return string
54
     */
55
    public function getDataPath($id)
56
    {
57
        return config('hustoj.data_path').'/'.$id.'/';
58
    }
59
60
    /**
61
     * @param $id
62
     *
63
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
64
     *
65
     * @return array
66
     */
67
    public function getOutputFiles($id)
68
    {
69
        $pattern = $this->getDataPath($id).'*'.self::TYPE_OUT;
70
71
        return $this->getDataFiles($pattern);
72
    }
73
74
    public function getDataFiles($pattern)
75
    {
76
        $fs = new Filesystem();
77
        $files = $fs->glob($pattern);
78
79
        $data = [];
80
        foreach ($files as $file) {
81
            $data[$fs->name($file)] = $fs->get($file);
82
        }
83
84
        return $data;
85
    }
86
}
87