Completed
Push — master ( 395fb2...b0764c )
by he
10:48 queued 10s
created

DataProvider::getInputFiles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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