Completed
Push — master ( 944c9c...280a9a )
by Antonio Carlos
03:20 queued 10s
created

Helper::moveFilesWildcard()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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