Completed
Pull Request — master (#14)
by he
12:12
created

DataProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 3
Bugs 2 Features 1
Metric Value
eloc 27
c 3
b 2
f 1
dl 0
loc 74
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getOutputFiles() 0 5 1
A getData() 0 20 3
A getDataPath() 0 3 1
A getInputFiles() 0 5 1
A getDataFiles() 0 11 2
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
                throw new LogicException($message);
24
            }
25
            $outContent = $dataOutput[$name];
26
            $data[] = [
27
                'input'  => $content,
28
                'output' => $outContent,
29
            ];
30
        }
31
32
        return $data;
33
    }
34
35
    /**
36
     * @param $id
37
     *
38
     * @return array
39
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
40
     */
41
    public function getInputFiles($id)
42
    {
43
        $pattern = $this->getDataPath($id).'*'.self::TYPE_IN;
44
45
        return $this->getDataFiles($pattern);
46
    }
47
48
    /**
49
     * @param $id
50
     *
51
     * @return string
52
     */
53
    public function getDataPath($id)
54
    {
55
        return config('hustoj.data_path').'/'.$id.'/';
56
    }
57
58
    /**
59
     * @param $id
60
     *
61
     * @return array
62
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
63
     */
64
    public function getOutputFiles($id)
65
    {
66
        $pattern = $this->getDataPath($id).'*'.self::TYPE_OUT;
67
68
        return $this->getDataFiles($pattern);
69
    }
70
71
    public function getDataFiles($pattern)
72
    {
73
        $fs = new Filesystem();
74
        $files = $fs->glob($pattern);
75
76
        $data = [];
77
        foreach ($files as $file) {
78
            $data[$fs->name($file)] = $fs->get($file);
79
        }
80
81
        return $data;
82
    }
83
}
84