Loader::getLoadStatus()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Codervio\Envmanager\Loader;
4
5
use Exception;
6
use RuntimeException;
7
8
class Loader
9
{
10
    protected $filepath;
11
    protected $isloaded = false;
12
    protected $extension;
13
    protected $strict = false;
14
15
    public function __construct($path, $extension)
16
    {
17
        $this->filepath = $path;
18
        $this->extension = $extension;
19
    }
20
21
    public function getLoadStatus()
22
    {
23
        return !empty($this->filepath);
24
    }
25
26
    public function getFile()
27
    {
28
        return $this->filepath;
29
    }
30
31
    public function run()
32
    {
33
        $this->isloaded = true;
34
35
        $content = null;
36
37
        $path = glob($this->filepath);
38
39
        if (!$path) {
40
            $this->isloaded = false;
41
            return false;
42
        }
43
44
        foreach ($path as $filemain)
45
        {
46
            $fileInfo = pathinfo($filemain);
47
48
            $pathInfo = $fileInfo['dirname'].DIRECTORY_SEPARATOR.$fileInfo['basename'];
49
50
            if (is_dir($pathInfo)) {
51
                $dirlist = @scandir($pathInfo);
52
53
                foreach ($dirlist as $file) {
54
55
                    $fileInfoFolder = pathinfo($file);
56
57
                    $pathInfoFolder = $fileInfoFolder['dirname'].DIRECTORY_SEPARATOR.$fileInfoFolder['basename'];
58
59
                    if ($this->isReadable($path)) {
60
61
                        if (in_array($fileInfoFolder['extension'], $this->extension)) {
62
63
                            $content .= file_get_contents($pathInfoFolder);
64
65
                        } else {
66
67
                            throw new Exception('Invalid extension');
68
69
                        }
70
71
                    }
72
73
                }
74
75
            } else {
76
77
                if (is_readable($pathInfo)) {
78
79
                    $content .= file_get_contents($pathInfo);
80
                }
81
82
            }
83
84
        }
85
86
        return $content;
87
    }
88
89
    private function isReadable($path)
90
    {
91
        if (is_file($path) || is_readable($path)) {
92
            return true;
93
        }
94
95
        throw new RuntimeException(sprintf('A file %s is not readable or not exists'. $path));
96
    }
97
}
98