Completed
Push — master ( 79bbce...abd19f )
by Antonio Carlos
07:57 queued 05:51
created

Helper::loadFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace PragmaRX\Countries\Package\Services;
4
5
class Helper
6
{
7
    /**
8
     * @var object
9
     */
10
    protected $config;
11
12
    /**
13
     * Rinvex constructor.
14
     *
15
     * @param object $config
16
     */
17 34
    public function __construct($config)
18
    {
19 34
        $this->config = $config;
20 34
    }
21
22
    /**
23
     * Load a file from disk.
24
     *
25
     * @param $file
26
     * @return null|string
27
     */
28 34
    public function loadFile($file)
29
    {
30 34
        if (file_exists($file)) {
31 34
            return $this->sanitizeFile(file_get_contents($file));
32
        }
33 9
    }
34
35
    /**
36
     * Loads a json file.
37
     *
38
     * @param $file
39
     * @param string $dir
40
     * @return \PragmaRX\Coollection\Package\Coollection
41
     * @throws Exception
42
     */
43 34
    public function loadJson($file, $dir = null)
44
    {
45 34
        if (empty($file)) {
46
            throw new Exception('loadJson Error: File name not set');
47
        }
48
49 34
        if (! file_exists($file) && ! file_exists($file = $this->dataDir("/$dir/".strtolower($file).'.json'))) {
50 9
            return coollect();
51
        }
52
53 34
        $decoded = json5_decode($this->loadFile($file), true);
54
55 34
        if (is_null($decoded)) {
56
            throw new Exception("Error decoding json file: $file");
57
        }
58
59 34
        return coollect($decoded);
60
    }
61
62
    /**
63
     * Load json files from dir.
64
     *
65
     * @param $dir
66
     * @return \PragmaRX\Coollection\Package\Coollection
67
     */
68 View Code Duplication
    public function loadJsonFiles($dir)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
    {
70 34
        return coollect(glob("$dir/*.json*"))->mapWithKeys(function ($file) {
71 34
            $key = str_replace('.json', '', str_replace('.json5', '', basename($file)));
72
73 34
            return [$key => $this->loadJson($file)];
74 34
        });
75
    }
76
77
    /**
78
     * Move files using wildcard filter.
79
     *
80
     * @param $from
81
     * @param $to
82
     */
83
    public function moveFilesWildcard($from, $to)
84
    {
85
        coollect(glob($this->dataDir($from)))->each(function ($from) use ($to) {
86
            $this->mkDir($dir = $this->dataDir($to));
0 ignored issues
show
Bug introduced by
The method mkDir() does not seem to exist on object<PragmaRX\Countrie...ackage\Services\Helper>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
87
88
            rename($from, $dir.'/'.basename($from));
89
        });
90
    }
91
92
    /**
93
     * Get data directory.
94
     *
95
     * @param $path
96
     * @return string
97
     */
98 34
    public function dataDir($path = '')
99
    {
100 34
        $path = (empty($path) || starts_with($path, DIRECTORY_SEPARATOR)) ? $path : "/{$path}";
101
102 34
        return __COUNTRIES_DIR__.$this->toDir("/src/data$path");
103
    }
104
105
    /**
106
     * @param $contents
107
     * @return string
108
     */
109 34
    public function sanitizeFile($contents)
110
    {
111 34
        return str_replace('\n', '', $contents);
112
    }
113
114
    /**
115
     * Check if array is multidimensional.
116
     *
117
     * @param $string
118
     * @return string
119
     */
120 34
    public function toDir($string)
121
    {
122 34
        return str_replace('/', DIRECTORY_SEPARATOR, $string);
123
    }
124
}
125