Completed
Pull Request — master (#1066)
by
unknown
15:32
created

LfmController::applyIniOverrides()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 6
c 2
b 0
f 0
nc 4
nop 0
dl 0
loc 11
rs 8.8333
1
<?php
2
3
namespace UniSharp\LaravelFilemanager\Controllers;
4
5
use UniSharp\LaravelFilemanager\Lfm;
6
use UniSharp\LaravelFilemanager\LfmPath;
7
8
class LfmController extends Controller
9
{
10
    protected static $success_response = 'OK';
11
12
    public function __construct()
13
    {
14
        $this->applyIniOverrides();
15
    }
16
17
    /**
18
     * Set up needed functions.
19
     *
20
     * @return object|null
21
     */
22
    public function __get($var_name)
23
    {
24
        if ($var_name === 'lfm') {
25
            return app(LfmPath::class);
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

25
            return /** @scrutinizer ignore-call */ app(LfmPath::class);
Loading history...
26
        } elseif ($var_name === 'helper') {
27
            return app(Lfm::class);
28
        }
29
    }
30
31
    /**
32
     * Show the filemanager.
33
     *
34
     * @return mixed
35
     */
36
    public function show()
37
    {
38
        $key_auth_token = \config('lfm')['key_auth_token'];
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
        $key_auth_token = /** @scrutinizer ignore-call */ \config('lfm')['key_auth_token'];
Loading history...
39
        $no_authenticate_redirect_to = \config('lfm')['no_authenticate_token_redirect_to'];
40
41
        return view('laravel-filemanager::index', compact('key_auth_token', 'no_authenticate_redirect_to'))
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
        return /** @scrutinizer ignore-call */ view('laravel-filemanager::index', compact('key_auth_token', 'no_authenticate_redirect_to'))
Loading history...
42
            ->withHelper($this->helper);
0 ignored issues
show
Bug Best Practice introduced by
The property helper does not exist on UniSharp\LaravelFilemana...ntrollers\LfmController. Since you implemented __get, consider adding a @property annotation.
Loading history...
43
    }
44
45
    /**
46
     * Check if any extension or config is missing.
47
     *
48
     * @return array
49
     */
50
    public function getErrors()
51
    {
52
        $arr_errors = [];
53
54
        if (!extension_loaded('gd') && !extension_loaded('imagick')) {
55
            array_push($arr_errors, trans('laravel-filemanager::lfm.message-extension_not_found'));
0 ignored issues
show
Bug introduced by
The function trans was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
            array_push($arr_errors, /** @scrutinizer ignore-call */ trans('laravel-filemanager::lfm.message-extension_not_found'));
Loading history...
56
        }
57
58
        if (!extension_loaded('exif')) {
59
            array_push($arr_errors, 'EXIF extension not found.');
60
        }
61
62
        if (!extension_loaded('fileinfo')) {
63
            array_push($arr_errors, 'Fileinfo extension not found.');
64
        }
65
66
        $mine_config_key = 'lfm.folder_categories.'
67
            . $this->helper->currentLfmType()
0 ignored issues
show
Bug Best Practice introduced by
The property helper does not exist on UniSharp\LaravelFilemana...ntrollers\LfmController. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The method currentLfmType() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
            . $this->helper->/** @scrutinizer ignore-call */ currentLfmType()

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
68
            . '.valid_mime';
69
70
        if (!is_array(config($mine_config_key))) {
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
        if (!is_array(/** @scrutinizer ignore-call */ config($mine_config_key))) {
Loading history...
71
            array_push($arr_errors, 'Config : ' . $mine_config_key . ' is not a valid array.');
72
        }
73
74
        return $arr_errors;
75
    }
76
77
    public function error($error_type, $variables = [])
78
    {
79
        return $this->helper->error($error_type, $variables);
0 ignored issues
show
Bug Best Practice introduced by
The property helper does not exist on UniSharp\LaravelFilemana...ntrollers\LfmController. Since you implemented __get, consider adding a @property annotation.
Loading history...
80
    }
81
82
    /**
83
     * Overrides settings in php.ini.
84
     *
85
     * @return null
86
     */
87
    public function applyIniOverrides()
88
    {
89
        $overrides = config('lfm.php_ini_overrides', []);
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

89
        $overrides = /** @scrutinizer ignore-call */ config('lfm.php_ini_overrides', []);
Loading history...
90
91
        if ($overrides && is_array($overrides) && count($overrides) === 0) {
92
            return;
93
        }
94
95
        foreach ($overrides as $key => $value) {
96
            if ($value && $value != 'false') {
97
                ini_set($key, $value);
98
            }
99
        }
100
    }
101
102
103
    /**
104
     * If your use token authenticate, before show media manager call this api for checking authenticate
105
     * 
106
     * @return object|null
107
     * 
108
     */
109
    public function checkAuthenticate()
110
    {
111
        try {
112
            $guard_name = \config('lfm.guard_name');
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

112
            $guard_name = /** @scrutinizer ignore-call */ \config('lfm.guard_name');
Loading history...
113
114
            $auth = \Auth::guard($guard_name);
115
            if ($auth->check()) {
116
                $response = [
117
                    'message' => 'Authorization',
118
                    'errors' => [],
119
                    'data' => [
120
                        'authorization' => true,
121
                        'redirect_to' => null,
122
                    ]
123
                ];
124
                $status_code  = 200;
125
            } else {
126
                $response = [
127
                    'message' => 'No authorization',
128
                    'errors' => [],
129
                    'data' => [
130
                        'authorization' => false,
131
                        'redirect_to' => \config('lfm.no_authenticate_token_redirect_to'),
132
                    ]
133
                ];
134
                $status_code = 401;
135
            }
136
137
            return response($response, $status_code);
0 ignored issues
show
Bug introduced by
The function response was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

137
            return /** @scrutinizer ignore-call */ response($response, $status_code);
Loading history...
138
        } catch (\Exception $e) {
139
            return \response([
140
                'message' => 'Error machine',
141
                'errors' => [
142
                    'machine' => [$e->getMessage()],
143
                ],
144
                'data' => [],
145
            ], 500);
146
        }
147
    }
148
}
149