| Total Complexity | 40 |
| Total Lines | 256 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Manager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Manager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class Manager |
||
| 23 | { |
||
| 24 | public function webRoutes() |
||
| 25 | { |
||
| 26 | require __DIR__ . '/../routes/web.php'; |
||
| 27 | } |
||
| 28 | |||
| 29 | public function apiRoutes() |
||
| 30 | { |
||
| 31 | require __DIR__ . '/../routes/api.php'; |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Load assests |
||
| 36 | * |
||
| 37 | * @param string $path |
||
| 38 | * |
||
| 39 | * @return \Illuminate\Http\Response |
||
| 40 | */ |
||
| 41 | public function assets($path) |
||
| 42 | { |
||
| 43 | $file = base_path(trim(config('dbm.resources_path'), '/') . "/" . urldecode($path)); |
||
| 44 | |||
| 45 | if (File::exists($file)) { |
||
| 46 | |||
| 47 | switch ($extension = pathinfo($file, PATHINFO_EXTENSION)) { |
||
| 48 | case 'js': |
||
| 49 | $mimeType = 'text/javascript'; |
||
| 50 | break; |
||
| 51 | case 'css': |
||
| 52 | $mimeType = 'text/css'; |
||
| 53 | break; |
||
| 54 | default: |
||
| 55 | $mimeType = File::mimeType($file); |
||
| 56 | break; |
||
| 57 | } |
||
| 58 | |||
| 59 | $response = Response::make(File::get($file), 200); |
||
| 60 | $response->header('Content-Type', $mimeType); |
||
| 61 | $response->setSharedMaxAge(31536000); |
||
| 62 | $response->setMaxAge(31536000); |
||
| 63 | $response->setExpires(new \DateTime('+1 year')); |
||
| 64 | |||
| 65 | return $response; |
||
| 66 | } |
||
| 67 | |||
| 68 | return response('', 404); |
||
| 69 | } |
||
| 70 | |||
| 71 | public function getModelNamespace() |
||
| 72 | { |
||
| 73 | return trim(config('dbm.modal_namespace', app()->getNamespace()), '\\'); |
||
| 74 | } |
||
| 75 | |||
| 76 | public function makeModel($model, $table) |
||
| 77 | { |
||
| 78 | try { |
||
| 79 | |||
| 80 | $partials = explode("\\", $model); |
||
| 81 | |||
| 82 | $className = array_pop($partials); |
||
| 83 | $namespace = implode("\\", $partials); |
||
| 84 | |||
| 85 | $contents = "<?php\n\n"; |
||
| 86 | $contents .= "namespace " . $namespace . ";\n\n"; |
||
| 87 | if (Driver::isMongoDB()) { |
||
| 88 | $contents .= "use Jenssegers\Mongodb\Eloquent\Model;\n\n"; |
||
| 89 | } else { |
||
| 90 | $contents .= "use Illuminate\Database\Eloquent\Model;\n\n"; |
||
| 91 | } |
||
| 92 | $contents .= "class " . $className . " extends Model\n"; |
||
| 93 | $contents .= "{\n\n"; |
||
| 94 | if (Driver::isMongoDB()) { |
||
| 95 | $contents .= "\tprotected \$collection = '" . $table . "';\n"; |
||
| 96 | } else { |
||
| 97 | $contents .= "\tprotected \$table = '" . $table . "';\n"; |
||
| 98 | } |
||
| 99 | |||
| 100 | // $content .= "\tpublic \$timestamps = false;\n"; |
||
| 101 | $contents .= "}\n"; |
||
| 102 | |||
| 103 | $filesystem = new Filesystem; |
||
| 104 | $filesystem->put(base_path($model . ".php"), $contents); |
||
| 105 | } catch (\Exception $e) { |
||
| 106 | throw new \Exception("There has an error when create model. The error is :" . $e->getMessage(), 1); |
||
| 107 | |||
| 108 | } |
||
| 109 | |||
| 110 | } |
||
| 111 | |||
| 112 | public function makeController($controller) |
||
| 113 | { |
||
| 114 | try { |
||
| 115 | Artisan::call('make:controller', [ |
||
| 116 | 'name' => $controller, |
||
| 117 | ]); |
||
| 118 | } catch (\Exception $e) { |
||
| 119 | throw new \Exception("There has an error when create Controller. The error is :" . $e->getMessage(), 1); |
||
| 120 | |||
| 121 | } |
||
| 122 | |||
| 123 | } |
||
| 124 | |||
| 125 | public function model($model, $table = null) |
||
| 126 | { |
||
| 127 | if ($table == null) { |
||
| 128 | return new $model; |
||
| 129 | } |
||
| 130 | |||
| 131 | return (new $model)->setTable($table); |
||
| 132 | } |
||
| 133 | |||
| 134 | public function Object() |
||
| 135 | { |
||
| 136 | if (Driver::isMongoDB()) { |
||
| 137 | return new DBM_MongoObject(); |
||
| 138 | } |
||
| 139 | |||
| 140 | return new DBM_Object; |
||
| 141 | } |
||
| 142 | |||
| 143 | public function Field() |
||
| 144 | { |
||
| 145 | if (Driver::isMongoDB()) { |
||
| 146 | return new DBM_MongoField; |
||
| 147 | } |
||
| 148 | |||
| 149 | return new DBM_Field; |
||
| 150 | } |
||
| 151 | |||
| 152 | public function Permission() |
||
| 153 | { |
||
| 154 | if (Driver::isMongoDB()) { |
||
| 155 | return new DBM_MongoPermission; |
||
| 156 | } |
||
| 157 | |||
| 158 | return new DBM_Permission; |
||
| 159 | } |
||
| 160 | |||
| 161 | public function Template() |
||
| 162 | { |
||
| 163 | if (Driver::isMongoDB()) { |
||
| 164 | return new DBM_MongoTemplate; |
||
| 165 | } |
||
| 166 | |||
| 167 | return new DBM_Template; |
||
| 168 | } |
||
| 169 | |||
| 170 | public function templates() |
||
| 171 | { |
||
| 172 | $templates = static::Template()->get(); |
||
| 173 | $newTemplates = []; |
||
| 174 | |||
| 175 | foreach ($templates as $template) { |
||
| 176 | $newTemplates[] = (object) [ |
||
| 177 | "name" => $template->name, |
||
| 178 | "oldName" => $template->old_name, |
||
| 179 | "type" => [ |
||
| 180 | "name" => $template->type, |
||
| 181 | ], |
||
| 182 | "notnull" => $template->notnull, |
||
| 183 | "unsigned" => $template->unsigned, |
||
| 184 | "autoincrement" => $template->auto_increment, |
||
| 185 | "default" => $template->default, |
||
| 186 | "length" => $template->length, |
||
| 187 | "index" => ($template->index != null) ? $template->index : "", |
||
| 188 | ]; |
||
| 189 | } |
||
| 190 | |||
| 191 | return $newTemplates; |
||
| 192 | } |
||
| 193 | |||
| 194 | /* |
||
| 195 | * File System |
||
| 196 | */ |
||
| 197 | |||
| 198 | public function getPathPrefix($driver = 'local') |
||
| 199 | { |
||
| 200 | return trim(Storage::disk($driver)->getDriver()->getAdapter()->getPathPrefix(), DIRECTORY_SEPARATOR); |
||
| 201 | } |
||
| 202 | |||
| 203 | /* |
||
| 204 | * Permission |
||
| 205 | */ |
||
| 206 | |||
| 207 | public function userPermissions() |
||
| 208 | { |
||
| 209 | $user = Auth::user(); |
||
| 210 | |||
| 211 | return self::Object() |
||
| 212 | ->setManyToManyRelation( |
||
| 213 | $user, |
||
| 214 | static::Permission(), |
||
| 215 | 'dbm_user_permissions', |
||
| 216 | 'user_id', |
||
| 217 | 'dbm_permission_id' |
||
| 218 | ) |
||
| 219 | ->belongs_to_many; |
||
| 220 | } |
||
| 221 | |||
| 222 | public function isLoggedIn() |
||
| 223 | { |
||
| 224 | if (Auth::guest()) { |
||
| 225 | return Route::has('login') ? redirect(route('login')) : abort(404); |
||
| 226 | } |
||
| 227 | |||
| 228 | return true; |
||
| 229 | } |
||
| 230 | |||
| 231 | public function checkPermission($prefix, $slug) |
||
| 232 | { |
||
| 233 | if (Auth::guest()) { |
||
| 234 | return 'not_logged_in'; |
||
| 235 | } |
||
| 236 | |||
| 237 | $user_model = config('dbm.user.model'); |
||
| 238 | $user_table = config('dbm.user.table'); |
||
| 239 | $user_local_key = config('dbm.user.local_key'); |
||
| 240 | $user_display_name = config('dbm.user.display_name'); |
||
| 241 | |||
| 242 | $permissions = $this->userPermissions(); |
||
| 243 | |||
| 244 | foreach ($permissions as $permission) { |
||
| 245 | if ($permission->prefix == $prefix && $permission->slug == $slug) { |
||
| 246 | return 'authorized'; |
||
| 247 | } |
||
| 248 | } |
||
| 249 | |||
| 250 | return 'not_authorized'; |
||
| 251 | |||
| 252 | } |
||
| 253 | |||
| 254 | public function authorize($permission) |
||
| 278 | } |
||
| 279 | } |
||
| 280 | |||
| 281 | } |
||
| 282 |