Completed
Push — master ( dd8ca6...ed1ab1 )
by Antonio Carlos
06:33 queued 01:44
created

Helper::loadFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PragmaRX\Countries\Package\Services;
4
5
use Exception;
6
use IlluminateAgnostic\Str\Support\Str;
7
8
class Helper
9
{
10
    /**
11
     * @var object
12
     */
13
    protected $config;
14
15
    /**
16
     * Rinvex constructor.
17
     *
18
     * @param object $config
19
     */
20 34
    public function __construct($config)
21
    {
22 34
        $this->config = $config;
23 34
    }
24
25
    /**
26
     * Load a file from disk.
27
     *
28
     * @param $file
29
     * @return null|string
30
     */
31 34
    public function loadFile($file)
32
    {
33 34
        if (file_exists($file)) {
34 34
            return $this->sanitizeFile(file_get_contents($file));
35
        }
36 9
    }
37
38
    /**
39
     * Loads a json file.
40
     *
41
     * @param $file
42
     * @param string $dir
43
     * @return \PragmaRX\Coollection\Package\Coollection
44
     * @throws Exception
45
     */
46 34
    public function loadJson($file, $dir = null)
47
    {
48 34
        if (empty($file)) {
49
            throw new Exception('loadJson Error: File name not set');
50
        }
51
52 34
        if (! file_exists($file) && ! file_exists($file = $this->dataDir("/$dir/".strtolower($file).'.json'))) {
53 9
            return coollect();
54
        }
55
56 34
        $decoded = json5_decode($this->loadFile($file), true);
57
58 34
        if (\is_null($decoded)) {
59
            throw new Exception("Error decoding json file: $file");
60
        }
61
62 34
        return coollect($decoded);
63
    }
64
65
    /**
66
     * Load json files from dir.
67
     *
68
     * @param $dir
69
     * @return \PragmaRX\Coollection\Package\Coollection
70
     */
71 34 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...
72
    {
73
        return coollect(glob("$dir/*.json*"))->mapWithKeys(function ($file) {
74 34
            $key = str_replace(['.json5', '.json'], '', basename($file));
75
76 34
            return [$key => $this->loadJson($file)];
77 34
        });
78
    }
79
80
    /**
81
     * Move files using wildcard filter.
82
     *
83
     * @param $from
84
     * @param $to
85
     */
86
    public function moveFilesWildcard($from, $to)
87
    {
88
        coollect(glob($this->dataDir($from)))->each(function ($from) use ($to) {
89
            $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...
90
91
            rename($from, $dir.'/'.basename($from));
92
        });
93
    }
94
95
    /**
96
     * Get data directory.
97
     *
98
     * @param $path
99
     * @return string
100
     */
101 34
    public function dataDir($path = '')
102
    {
103 34
        $path = (empty($path) || Str::startsWith($path, DIRECTORY_SEPARATOR)) ? $path : "/{$path}";
104
105 34
        return __COUNTRIES_DIR__.$this->toDir("/src/data$path");
106
    }
107
108
    /**
109
     * @param $contents
110
     * @return string
111
     */
112 34
    public function sanitizeFile($contents)
113
    {
114 34
        return str_replace('\n', '', $contents);
115
    }
116
117
    /**
118
     * Check if array is multidimensional.
119
     *
120
     * @param $string
121
     * @return string
122
     */
123 34
    public function toDir($string)
124
    {
125 34
        return str_replace('/', DIRECTORY_SEPARATOR, $string);
126
    }
127
}
128