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 ( b25568...6a92d6 )
by Pedro
12:01
created

CrudController::initializeCrudController()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 12
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 function __construct()
28
    {
29
        // ---------------------------
30
        // Create the CrudPanel object
31
        // ---------------------------
32
        // Used by developers inside their ProductCrudControllers as
33
        // $this->crud or using the CRUD facade.
34
        //
35
        // It's done inside a middleware closure in order to have
36
        // the complete request inside the CrudPanel object.
37
        $this->middleware(function ($request, $next) {
38
            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

38
            if (! CrudManager::/** @scrutinizer ignore-call */ hasCrudController(get_class($this))) {
Loading history...
39
                $this->initializeCrudController($request);
40
41
                return $next($request);
42
            }
43
44
            $this->setupCrudController();
45
46
            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

46
            CrudManager::/** @scrutinizer ignore-call */ 
47
                         crud($this)->setRequest($request);
Loading history...
47
48
            return $next($request);
49
        });
50
    }
51
52
    public function initializeCrudController($request, $crudPanel = null): void
53
    {
54
        $crudPanel ??= CrudManager::crud($this);
55
        
56
        $crudPanel->initialize(get_class($this), $request);
57
58
        if(! $crudPanel->isInitialized()) {
59
            $crudPanel->initialized = true;
60
            $this->setupCrudController();
61
        }        
62
63
        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

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

161
            return CrudManager::/** @scrutinizer ignore-call */ getControllerCrud(get_class($this));
Loading history...
162
        }
163
164
        return $this->{$name};
165
    }
166
}
167