Passed
Push — master ( 139939...b4b8e8 )
by Arthur
21:54 queued 17s
created

Resource   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 182
rs 10
c 0
b 0
f 0
wmc 29

17 Methods

Rating   Name   Duplication   Size   Complexity  
A getClasses() 0 11 4
A getSubPath() 0 3 1
A getName() 0 3 1
A amount() 0 3 1
A getNamespace() 0 3 1
A getModule() 0 3 1
A getPath() 0 3 1
A boot() 0 3 1
A hasPhpExtension() 0 3 2
A getAllPhpFileNamesWithoutExtension() 0 9 3
A getBaseClass() 0 3 1
A setFiles() 0 5 2
A getAllPhpFileNames() 0 9 3
A getClassByName() 0 7 3
A __construct() 0 7 1
A getFiles() 0 3 1
A getAllFileNames() 0 8 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: arthur
5
 * Date: 09.03.19
6
 * Time: 21:51
7
 */
8
9
namespace Foundation\Core;
10
11
use Foundation\Exceptions\Exception;
12
13
final class Resource
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $name;
19
20
    /**
21
     * @var string
22
     */
23
    protected $subPath;
24
25
    /**
26
     * @var Module $module
27
     */
28
    protected $module;
29
30
    /**
31
     * @var string|null
32
     */
33
    protected $baseClass;
34
35
    /**
36
     * @var File[] $files
37
     */
38
    protected $files = [];
39
40
    /**
41
     * LarapiModule constructor.
42
     * @param $name
43
     */
44
    public function __construct(string $name, string $subPath, Module $module, ?string $baseClass = null)
45
    {
46
        $this->name = $name;
47
        $this->subPath = $subPath;
48
        $this->module = $module;
49
        $this->baseClass = $baseClass;
50
        $this->boot();
51
    }
52
53
    protected function boot()
54
    {
55
        $this->setFiles();
56
    }
57
58
    protected function setFiles()
59
    {
60
        $files = $this->getAllPhpFileNamesWithoutExtension();
61
        foreach ($files as $file) {
62
            $this->files[] = new File($file, $this->getPath() . '/' . $file . '.php', $this);
63
        }
64
    }
65
66
    /**
67
     * @return string
68
     */
69
    public function getName(): string
70
    {
71
        return $this->name;
72
    }
73
74
    /**
75
     * @return string
76
     */
77
    public function getSubPath(): string
78
    {
79
        return $this->subPath;
80
    }
81
82
    /**
83
     * @return string
84
     */
85
    public function getPath(): string
86
    {
87
        return $this->module->getPath() . $this->subPath;
88
    }
89
90
    /**
91
     * @return string
92
     */
93
    public function getNamespace(): string
94
    {
95
        return $this->module->getNamespace() . str_replace('/', '\\', $this->getSubPath());
96
    }
97
98
    /**
99
     * @return int
100
     */
101
    public function amount(): int
102
    {
103
        return sizeof($this->getClasses());
104
    }
105
106
    /**
107
     * @return string[]
108
     */
109
    public function getClasses(): array
110
    {
111
        $classes = [];
112
        foreach ($this->getAllPhpFileNames() as $file) {
113
            $shortClassName = str_replace('.php', '', $file);
114
            $class = $this->getNamespace() . '\\' . $shortClassName;
115
116
            if ($this->baseClass === null || instance_without_constructor($class) instanceof $this->baseClass)
117
                $classes[] = $class;
118
        }
119
        return $classes;
120
    }
121
122
    /**
123
     * @return File[]
124
     */
125
    public function getFiles(): array
126
    {
127
        return $this->files;
128
    }
129
130
    public function getAllFileNames()
131
    {
132
        try {
133
            $fileNames = array_diff(scandir($this->getPath()), ['..', '.']);
134
        } Catch(\ErrorException $e){
135
            $fileNames = [];
136
        }
137
        return $fileNames;
138
    }
139
140
    public function getAllPhpFileNames()
141
    {
142
        $files = [];
143
        foreach ($this->getAllFileNames() as $file) {
144
            if ($this->hasPhpExtension($file)) {
145
                $files[] = $file;
146
            }
147
        }
148
        return $files;
149
    }
150
151
    public function getAllPhpFileNamesWithoutExtension()
152
    {
153
        $files = [];
154
        foreach ($this->getAllFileNames() as $file) {
155
            if ($this->hasPhpExtension($file)) {
156
                $files[] = str_replace('.php', '', $file);
157
            }
158
        }
159
        return $files;
160
    }
161
162
    /**
163
     * @param string $fileName
164
     *
165
     * @return bool
166
     */
167
    private function hasPhpExtension(string $fileName): bool
168
    {
169
        return strlen($fileName) > 4 && '.php' === ($fileName[-4] . $fileName[-3] . $fileName[-2] . $fileName[-1]);
170
    }
171
172
    /**
173
     * @return Module
174
     */
175
    public function getModule(): Module
176
    {
177
        return $this->module;
178
    }
179
180
    /**
181
     * @return string|null
182
     */
183
    public function getBaseClass(): ?string
184
    {
185
        return $this->baseClass;
186
    }
187
188
    public function getClassByName(string $className): ?string
189
    {
190
        foreach ($this->getClasses() as $class) {
191
            if (strtolower((get_short_class_name($class)) === strtolower($className)))
192
                return $class;
193
        }
194
        return null;
195
    }
196
197
198
}
199