Completed
Push — master ( b9dd45...d03d4d )
by Mahmoud
03:06
created

PortButler   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 172
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 22
lcom 1
cbo 1
dl 0
loc 172
rs 10
c 2
b 0
f 1

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getContainersNamespace() 0 4 1
A getLoginWebPageName() 0 10 2
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 stringStartsWith() 0 5 1
1
<?php
2
3
namespace App\Port\Butler\Portals;
4
5
use App\Port\Butler\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
     * @return  mixed
31
     */
32
    public function getLoginWebPageName()
33
    {
34
        $loginPage = Config::get('hello.containers.login-page-name');
35
36
        if (is_null($loginPage)) {
37
            throw new WrongConfigurationsException();
38
        }
39
40
        return $loginPage;
41
    }
42
43
    /**
44
     * Get the containers names
45
     *
46
     * @return  array
47
     */
48
    public function getContainersNames()
49
    {
50
        $containersNames = [];
51
52
        foreach ($this->getContainersPaths() as $containersPath) {
53
            $containersNames[] = basename($containersPath);
54
        }
55
56
        return $containersNames;
57
    }
58
59
    /**
60
     * get containers directories paths
61
     *
62
     * @return  mixed
63
     */
64
    public function getContainersPaths()
65
    {
66
        $containersPaths = File::directories(app_path('Containers'));
67
68
        // TODO: preparing some code to implement the Ships concept to Porto
69
70
//        // 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...
71
//        foreach ($containersPaths as $containersPath) {
72
//
73
//            if (preg_match('/_SHIP/',$containersPath)){
74
//
75
//                // append the Ship directories to the Containers directory, since it will contain containers
76
//                $shipContainers = File::directories($containersPath);
77
//                $containersPaths = array_merge($containersPaths, $shipContainers);
78
//            }
79
//        }
80
81
        return $containersPaths;
82
    }
83
84
    /**
85
     * build and return an object of a class from its file path
86
     *
87
     * @param $filePathName
88
     *
89
     * @return  mixed
90
     */
91
    public function getClassObjectFromFile($filePathName)
92
    {
93
        $classString = $this->getClassFullNameFromFile($filePathName);
94
95
        $object = new $classString;
96
97
        return $object;
98
    }
99
100
101
    /**
102
     * get the full name (name \ namespace) of a class from its file path
103
     * result example: (string) "I\Am\The\Namespace\Of\This\Class"
104
     *
105
     * @param $filePathName
106
     *
107
     * @return  string
108
     */
109
    public function getClassFullNameFromFile($filePathName)
110
    {
111
        return $this->getClassNamespaceFromFile($filePathName) . '\\' . $this->getClassNameFromFile($filePathName);
112
    }
113
114
    /**
115
     * get the class namespace form file path using token
116
     *
117
     * @param $filePathName
118
     *
119
     * @return  null|string
120
     */
121
    protected function getClassNamespaceFromFile($filePathName)
122
    {
123
        $src = file_get_contents($filePathName);
124
125
        $tokens = token_get_all($src);
126
        $count = count($tokens);
127
        $i = 0;
128
        $namespace = '';
129
        $namespace_ok = false;
130
        while ($i < $count) {
131
            $token = $tokens[$i];
132
            if (is_array($token) && $token[0] === T_NAMESPACE) {
133
                // Found namespace declaration
134
                while (++$i < $count) {
135
                    if ($tokens[$i] === ';') {
136
                        $namespace_ok = true;
137
                        $namespace = trim($namespace);
138
                        break;
139
                    }
140
                    $namespace .= is_array($tokens[$i]) ? $tokens[$i][1] : $tokens[$i];
141
                }
142
                break;
143
            }
144
            $i++;
145
        }
146
        if (!$namespace_ok) {
147
            return null;
148
        } else {
149
            return $namespace;
150
        }
151
    }
152
153
    /**
154
     * get the class name form file path using token
155
     *
156
     * @param $filePathName
157
     *
158
     * @return  mixed
159
     */
160
    protected function getClassNameFromFile($filePathName)
161
    {
162
        $php_code = file_get_contents($filePathName);
163
164
        $classes = array();
165
        $tokens = token_get_all($php_code);
166
        $count = count($tokens);
167
        for ($i = 2; $i < $count; $i++) {
168
            if ($tokens[$i - 2][0] == T_CLASS
169
                && $tokens[$i - 1][0] == T_WHITESPACE
170
                && $tokens[$i][0] == T_STRING
171
            ) {
172
173
                $class_name = $tokens[$i][1];
174
                $classes[] = $class_name;
175
            }
176
        }
177
178
        return $classes[0];
179
    }
180
181
    public function stringStartsWith($word, $needle)
182
    {
183
        $length = strlen($needle);
184
        return (substr($word, 0, $length) === $needle);
185
    }
186
187
}
188