Passed
Branch master (f2d2e3)
by Michael
18:45
created

LaravelAdministrator::runIntegration()   B

Complexity

Conditions 10
Paths 33

Size

Total Lines 72
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 10
eloc 34
c 1
b 1
f 0
nc 33
nop 0
dl 0
loc 72
rs 7.6666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php namespace kcfinder\integration;
2
3
/** This file is part of KCFinder project
4
  *
5
  *      @desc Integration code: Laravel Administrator (https://github.com/FrozenNode/Laravel-Administrator)
6
  *   @package KCFinder
7
  *   @version 3.12
8
  *    @author Zsombor Franczia <[email protected]>
9
  * @copyright 2010-2014 KCFinder Project
10
  *   @license http://opensource.org/licenses/GPL-3.0 GPLv3
11
  *   @license http://opensource.org/licenses/LGPL-3.0 LGPLv3
12
  *      @link http://kcfinder.sunhater.com
13
  */
14
15
class LaravelAdministrator {
16
    protected static $authenticated = false;
17
    protected static $bootstrapAutoload = '/bootstrap/autoload.php';
18
    protected static $bootstrapStart = '/bootstrap/start.php';
19
20
    static function getLaravelPath() {
21
22
        //absolute path method
23
        if (!empty($_SERVER['SCRIPT_FILENAME'])) {
24
            $laravelPath = dirname(dirname(dirname(dirname(dirname(dirname(dirname(dirname($_SERVER['SCRIPT_FILENAME']))))))));
25
            if (!file_exists($laravelPath . self::$bootstrapAutoload)) {
26
                $laravelPath = dirname(dirname(dirname(dirname($_SERVER['SCRIPT_FILENAME']))));
27
                $depth = 3;
28
                do {
29
                    $laravelPath = dirname($laravelPath);
30
                    $depth++;
31
                    $autoloadFound = file_exists($laravelPath . self::$bootstrapAutoload);
32
                } while (!$autoloadFound && $depth < 10);
33
            }
34
            else {
35
                $autoloadFound = true;
36
            }
37
        }
38
39
        //relative path method
40
        if (!isset($autoloadFound) || !$autoloadFound) {
41
            $laravelPath = '../../../../../../..';
42
            if (!file_exists($laravelPath . self::$bootstrapAutoload)) {
43
                $laravelPath = '../../..';
44
                $depth = 3;
45
                do {
46
                    $laravelPath .= '/..';
47
                    $depth++;
48
                } while (!($autoloadFound = file_exists($laravelPath . self::$bootstrapAutoload)) && $depth < 10);
0 ignored issues
show
Unused Code introduced by
The assignment to $autoloadFound is dead and can be removed.
Loading history...
49
            }
50
            else {
51
                $autoloadFound = true;
52
            }
53
        }
54
55
        return $laravelPath;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $laravelPath does not seem to be defined for all execution paths leading up to this point.
Loading history...
56
    }
57
58
    static function runIntegration() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
59
60
        $laravelPath = self::getLaravelPath();
61
62
        if(file_exists($laravelPath . '/bootstrap/autoload.php')) {
63
            $currentCwd = getcwd();
64
65
            // Simulate being in the laravel root folder so we can share the session (is this really needed?)
66
            // chdir($laravelPath);
67
68
            // bootstrap
69
            require $laravelPath.'/bootstrap/autoload.php';
70
            $app = require_once $laravelPath.'/bootstrap/start.php';
71
            $app->boot(); //Boot the application's service providers. 
72
73
            //get the admin check closure that should be supplied in the admin config
74
            $permission = \Illuminate\Support\Facades\Config::get('administrator::administrator.permission');
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Facades\Config was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
75
            $hasPermission = $permission();
76
            self::$authenticated = $hasPermission;
77
78
            //start session if not started already 
79
            if (session_id() == "") {
80
                session_start();
81
                $iStartedTheSession = true;
82
            }
83
84
            if (!isset($_SESSION['KCFINDER'])) {
85
                $_SESSION['KCFINDER'] = array();
86
            }
87
88
            //if this is a simple true value, user is logged in
89
            if ($hasPermission == true) {
90
91
                $configFactory = \Illuminate\Support\Facades\App::make('admin_config_factory');
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Facades\App was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
92
                $modelName = \Illuminate\Support\Facades\Input::get('model');
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Facades\Input was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
93
                $fieldName = \Illuminate\Support\Facades\Input::get('field');
94
95
                if(!empty($modelName) && !empty($fieldName)) {
96
97
                    $modelConfig = $configFactory->make($modelName, true);
98
                    $modelConfigOptions = $modelConfig->getOption('edit_fields');
99
                    $kcfinderOptions = $modelConfigOptions[$fieldName]["kcfinder"];
100
101
                    //allow users to use an option called 'enabled' instead of 'disabled'
102
                    if(isset($kcfinderOptions["enabled"])) {
103
                        $kcfinderOptions["disabled"] = !$kcfinderOptions["enabled"];
104
                    }
105
106
                    //save all options to the session
107
                    foreach ($kcfinderOptions as $optKey => $optValue) {
108
                        $_SESSION['KCFINDER'][$optKey] = $optValue;
109
                    }
110
111
                    self::$authenticated = !$_SESSION['KCFINDER']['disabled'];
112
113
                }
114
            }
115
            else {
116
                //clean and reset the session variable
117
                $_SESSION['KCFINDER'] = array();
118
            }
119
120
            //close the session if I started it
121
            if (isset($iStartedTheSession)) {
122
                session_write_close();
123
            }
124
125
            // chdir($currentCwd);
126
            return self::$authenticated;
127
        }
128
        else {
129
            die("The integration service for 'Laravel Administrator' failed. Laravel root path not found!");
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
130
        }
131
132
    }
133
134
}
135
136
\kcfinder\integration\LaravelAdministrator::runIntegration();
137