Completed
Push — master ( f89d9f...ca4d83 )
by Mahmoud
03:00
created

PortButler   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 22
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 178
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getContainersNamespace() 0 4 1
A getContainersNames() 0 10 2
A getContainersPaths() 0 19 1
A getClassObjectFromFile() 0 8 1
A getClassFullNameFromFile() 0 4 1
C getClassNamespaceFromFile() 0 31 8
B getClassNameFromFile() 0 20 5
A getLoginWebPageName() 0 10 2
A stringStartsWith() 0 4 1
1
<?php
2
3
namespace App\Port\Foundation\Portals;
4
5
use App\Port\Foundation\Exceptions\WrongConfigurationsException;
6
use Illuminate\Support\Facades\Config;
7
use Illuminate\Support\Facades\File;
8
9
/**
10
 * Class PortButler.
11
 *
12
 * NOTE: You can access this Class functions with the facade [ModuleConfig].
13
 *
14
 * @author Mahmoud Zalt <[email protected]>
15
 */
16
class PortButler
17
{
18
19
    /**
20
     * Get the containers namespace value from the containers config file
21
     *
22
     * @return  string
23
     */
24
    public function getContainersNamespace()
25
    {
26
        return Config::get('hello.containers.namespace');
27
    }
28
29
    /**
30
     * Get the containers names
31
     *
32
     * @return  array
33
     */
34
    public function getContainersNames()
35
    {
36
        $containersNames = [];
37
38
        foreach ($this->getContainersPaths() as $containersPath) {
39
            $containersNames[] = basename($containersPath);
40
        }
41
42
        return $containersNames;
43
    }
44
45
    /**
46
     * get containers directories paths
47
     *
48
     * @return  mixed
49
     */
50
    public function getContainersPaths()
51
    {
52
        $containersPaths = File::directories(app_path('Containers'));
53
54
        // TODO: preparing some code to implement the Ships concept to Porto
55
56
//        // Search for Ships "_SHIP" of Containers
0 ignored issues
show
Unused Code Comprehensibility introduced by
49% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
57
//        foreach ($containersPaths as $containersPath) {
58
//
59
//            if (preg_match('/_SHIP/',$containersPath)){
60
//
61
//                // append the Ship directories to the Containers directory, since it will contain containers
62
//                $shipContainers = File::directories($containersPath);
63
//                $containersPaths = array_merge($containersPaths, $shipContainers);
64
//            }
65
//        }
66
67
        return $containersPaths;
68
    }
69
70
    /**
71
     * build and return an object of a class from its file path
72
     *
73
     * @param $filePathName
74
     *
75
     * @return  mixed
76
     */
77
    public function getClassObjectFromFile($filePathName)
78
    {
79
        $classString = $this->getClassFullNameFromFile($filePathName);
80
81
        $object = new $classString;
82
83
        return $object;
84
    }
85
86
    /**
87
     * get the full name (name \ namespace) of a class from its file path
88
     * result example: (string) "I\Am\The\Namespace\Of\This\Class"
89
     *
90
     * @param $filePathName
91
     *
92
     * @return  string
93
     */
94
    public function getClassFullNameFromFile($filePathName)
95
    {
96
        return $this->getClassNamespaceFromFile($filePathName) . '\\' . $this->getClassNameFromFile($filePathName);
97
    }
98
99
    /**
100
     * get the class namespace form file path using token
101
     *
102
     * @param $filePathName
103
     *
104
     * @return  null|string
105
     */
106
    protected function getClassNamespaceFromFile($filePathName)
107
    {
108
        $src = file_get_contents($filePathName);
109
110
        $tokens = token_get_all($src);
111
        $count = count($tokens);
112
        $i = 0;
113
        $namespace = '';
114
        $namespace_ok = false;
115
        while ($i < $count) {
116
            $token = $tokens[$i];
117
            if (is_array($token) && $token[0] === T_NAMESPACE) {
118
                // Found namespace declaration
119
                while (++$i < $count) {
120
                    if ($tokens[$i] === ';') {
121
                        $namespace_ok = true;
122
                        $namespace = trim($namespace);
123
                        break;
124
                    }
125
                    $namespace .= is_array($tokens[$i]) ? $tokens[$i][1] : $tokens[$i];
126
                }
127
                break;
128
            }
129
            $i++;
130
        }
131
        if (!$namespace_ok) {
132
            return null;
133
        } else {
134
            return $namespace;
135
        }
136
    }
137
138
    /**
139
     * get the class name form file path using token
140
     *
141
     * @param $filePathName
142
     *
143
     * @return  mixed
144
     */
145
    protected function getClassNameFromFile($filePathName)
146
    {
147
        $php_code = file_get_contents($filePathName);
148
149
        $classes = array();
150
        $tokens = token_get_all($php_code);
151
        $count = count($tokens);
152
        for ($i = 2; $i < $count; $i++) {
153
            if ($tokens[$i - 2][0] == T_CLASS
154
                && $tokens[$i - 1][0] == T_WHITESPACE
155
                && $tokens[$i][0] == T_STRING
156
            ) {
157
158
                $class_name = $tokens[$i][1];
159
                $classes[] = $class_name;
160
            }
161
        }
162
163
        return $classes[0];
164
    }
165
166
    /**
167
     * @return  mixed
168
     */
169
    public function getLoginWebPageName()
170
    {
171
        $loginPage = Config::get('hello.containers.login-page-name');
172
173
        if (is_null($loginPage)) {
174
            throw new WrongConfigurationsException();
175
        }
176
177
        return $loginPage;
178
    }
179
180
    /**
181
     * check if a word starts with another word
182
     *
183
     * @param $word
184
     * @param $startsWith
185
     *
186
     * @return  bool
187
     */
188
    public function stringStartsWith($word, $startsWith)
189
    {
190
        return (substr($word, 0, strlen($startsWith)) === $startsWith);
191
    }
192
193
}
194