Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Push — datatable-single-component ( 863f17...49cc78 )
by Pedro
13:04
created

CrudController::setupCrudController()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\app\Http\Controllers;
4
5
use Backpack\CRUD\app\Http\Controllers\Contracts\CrudControllerContract;
6
use Backpack\CRUD\app\Library\Attributes\DeprecatedIgnoreOnRuntime;
7
use Backpack\CRUD\app\Library\CrudPanel\CrudPanel;
8
use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facades\LifecycleHook;
9
use Backpack\CRUD\CrudManager;
10
use Illuminate\Foundation\Bus\DispatchesJobs;
11
use Illuminate\Foundation\Validation\ValidatesRequests;
12
use Illuminate\Routing\Controller;
13
use Illuminate\Support\Str;
14
15
/**
16
 * Class CrudController.
17
 *
18
 * @property-read CrudPanel $crud
19
 * @property array $data
20
 */
21
class CrudController extends Controller implements CrudControllerContract
22
{
23
    use DispatchesJobs, ValidatesRequests;
24
25
    public $data = [];
26
27
    public $initialized = false;
28
29
    public function __construct()
30
    {
31
        // ---------------------------
32
        // Create the CrudPanel object
33
        // ---------------------------
34
        // Used by developers inside their ProductCrudControllers as
35
        // $this->crud or using the CRUD facade.
36
        //
37
        // It's done inside a middleware closure in order to have
38
        // the complete request inside the CrudPanel object.
39
        $this->middleware(function ($request, $next) {
40
            if (! CrudManager::hasCrudController(get_class($this))) {
0 ignored issues
show
Bug introduced by
The method hasCrudController() does not exist on Backpack\CRUD\CrudManager. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

40
            if (! CrudManager::/** @scrutinizer ignore-call */ hasCrudController(get_class($this))) {
Loading history...
41
                $this->initializeCrud($request);
42
43
                return $next($request);
44
            }
45
46
            $this->setupCrudController();
47
48
            $this->initialized = true;
49
50
            CrudManager::crud($this)->setRequest($request);
0 ignored issues
show
Bug introduced by
The method crud() does not exist on Backpack\CRUD\CrudManager. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

50
            CrudManager::/** @scrutinizer ignore-call */ 
51
                         crud($this)->setRequest($request);
Loading history...
51
52
            return $next($request);
53
        });
54
    }
55
56
    public function initializeCrud($request, $crudPanel = null, $operation = null): void
57
    {
58
        $crudPanel ??= CrudManager::crud($this);
59
        \Log::debug('Initializing CrudPanel', [
60
            'controller' => get_class($this),
61
            'operation' => $operation,
62
        ]);
63
        if ($crudPanel->isInitialized()) {
64
            \Log::debug('CrudPanel is already initialized. Skipping initialization.', [
65
                'controller' => get_class($this),
66
                'operation' => $operation,
67
            ]);
68
            $crudPanel->setRequest($request);
69
            $crudPanel->setCrudController(get_class($this));
70
            CrudManager::setControllerCrud(get_class($this), $crudPanel);
0 ignored issues
show
Bug introduced by
The method setControllerCrud() does not exist on Backpack\CRUD\CrudManager. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

70
            CrudManager::/** @scrutinizer ignore-call */ 
71
                         setControllerCrud(get_class($this), $crudPanel);
Loading history...
71
        }
72
73
        $crudPanel->initialized = true;
74
        $crudPanel->setRequest($request);
75
        $crudPanel->setCrudController(get_class($this));
76
77
        $this->setupCrudController();
78
79
        CrudManager::setControllerCrud(get_class($this), $crudPanel);
80
    }
81
82
    private function setupCrudController()
83
    {
84
        LifecycleHook::trigger('crud:before_setup_defaults', [$this]);
85
        $this->setupDefaults();
86
        LifecycleHook::trigger('crud:after_setup_defaults', [$this]);
87
88
        LifecycleHook::trigger('crud:before_setup', [$this]);
89
        $this->setup();
90
        LifecycleHook::trigger('crud:after_setup', [$this]);
91
92
        $this->setupConfigurationForCurrentOperation();
93
    }
94
95
    /**
96
     * Allow developers to set their configuration options for a CrudPanel.
97
     */
98
    public function setup()
99
    {
100
    }
101
102
    /**
103
     * Load routes for all operations.
104
     * Allow developers to load extra routes by creating a method that looks like setupOperationNameRoutes.
105
     *
106
     * @param  string  $segment  Name of the current entity (singular).
107
     * @param  string  $routeName  Route name prefix (ends with .).
108
     * @param  string  $controller  Name of the current controller.
109
     */
110
    #[DeprecatedIgnoreOnRuntime('we dont call this method anymore unless you had it overwritten in your CrudController')]
111
    public function setupRoutes($segment, $routeName, $controller)
112
    {
113
        preg_match_all('/(?<=^|;)setup([^;]+?)Routes(;|$)/', implode(';', get_class_methods($this)), $matches);
114
115
        if (count($matches[1])) {
116
            foreach ($matches[1] as $methodName) {
117
                $this->{'setup'.$methodName.'Routes'}($segment, $routeName, $controller);
118
            }
119
        }
120
    }
121
122
    /**
123
     * Load defaults for all operations.
124
     * Allow developers to insert default settings by creating a method
125
     * that looks like setupOperationNameDefaults.
126
     */
127
    protected function setupDefaults()
128
    {
129
        preg_match_all('/(?<=^|;)setup([^;]+?)Defaults(;|$)/', implode(';', get_class_methods($this)), $matches);
130
        if (count($matches[1])) {
131
            foreach ($matches[1] as $methodName) {
132
                $this->{'setup'.$methodName.'Defaults'}();
133
            }
134
        }
135
    }
136
137
    /**
138
     * Load configurations for the current operation.
139
     *
140
     * Allow developers to insert default settings by creating a method
141
     * that looks like setupOperationNameOperation (aka setupXxxOperation).
142
     */
143
    protected function setupConfigurationForCurrentOperation(?string $operation = null)
144
    {
145
        $operationName = $operation ?? $this->crud->getCurrentOperation();
146
        if (! $operationName) {
147
            return;
148
        }
149
        $setupClassName = 'setup'.Str::studly($operationName).'Operation';
150
151
        /*
152
         * FIRST, run all Operation Closures for this operation.
153
         *
154
         * It's preferred for this to run closures first, because
155
         * (1) setup() is usually higher in a controller than any other method, so it's more intuitive,
156
         * since the first thing you write is the first thing that is being run;
157
         * (2) operations use operation closures themselves, inside their setupXxxDefaults(), and
158
         * you'd like the defaults to be applied before anything you write. That way, anything you
159
         * write is done after the default, so you can remove default settings, etc;
160
         */
161
        LifecycleHook::trigger($operationName.':before_setup', [$this]);
162
163
        $this->crud->applyConfigurationFromSettings($operationName);
164
        /*
165
         * THEN, run the corresponding setupXxxOperation if it exists.
166
         */
167
        if (method_exists($this, $setupClassName)) {
168
            $this->{$setupClassName}();
169
        }
170
171
        LifecycleHook::trigger($operationName.':after_setup', [$this]);
172
    }
173
174
    public function __get($name)
175
    {
176
        if ($name === 'crud') {
177
            return CrudManager::getControllerCrud(get_class($this));
0 ignored issues
show
Bug introduced by
The method getControllerCrud() does not exist on Backpack\CRUD\CrudManager. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

177
            return CrudManager::/** @scrutinizer ignore-call */ getControllerCrud(get_class($this));
Loading history...
178
        }
179
180
        return $this->{$name};
181
    }
182
}
183