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

CrudPanelManager::getControllerCrud()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD;
4
5
use Backpack\CRUD\app\Http\Controllers\Contracts\CrudControllerContract;
6
use Backpack\CRUD\app\Library\CrudPanel\CrudPanel;
7
use Illuminate\Support\Facades\Facade;
8
9
final class CrudPanelManager
10
{
11
    private array $cruds = [];
12
13
    private ?string $currentlyActiveCrudController = null;
14
15
    private CrudPanel $crudPanelInstance;
16
17
    private $requestController = null;
18
19
    public function __construct()
20
    {
21
        $this->crudPanelInstance = new CrudPanel();
22
    }
23
24
    public function getCrudPanelInstance(): CrudPanel
25
    {
26
        return $this->crudPanelInstance;
27
    }
28
29
    public function crud(CrudControllerContract $controller): CrudPanel
30
    {
31
        $controllerClass = get_class($controller);
32
33
        $this->requestController = $controllerClass;
34
35
        if (isset($this->cruds[$controllerClass])) {
36
            return $this->cruds[$controllerClass];
37
        }
38
39
        $instance = new CrudPanel();
40
41
        $this->cruds[$controllerClass] = $instance;
42
43
        return $this->cruds[$controllerClass];
44
    }
45
46
    public function crudFromController(string $controller, ?string $operation = null): CrudPanel
47
    {
48
        $controller = new $controller();
49
50
        $crud = $this->crud($controller);
51
52
        // Use provided operation or default to 'list'
53
        $operation = $operation ?? 'list';
54
        $crud->setOperation($operation);
55
56
        $primaryControllerRequest = $this->cruds[array_key_first($this->cruds)]->getRequest();
57
        if (! $crud->isInitialized()) {
58
            $controller->initializeCrud($primaryControllerRequest, $crud, $operation);
59
        }
60
61
        return $crud;
62
    }
63
64
    public function setControllerCrud(string $controller, CrudPanel $crud): void
65
    {
66
        $this->cruds[$controller] = $crud;
67
    }
68
69
    public function hasCrudController(string $controller): bool
70
    {
71
        return isset($this->cruds[$controller]);
72
    }
73
74
    public function getControllerCrud(string $controller): CrudPanel
75
    {
76
        if (! isset($this->cruds[$controller])) {
77
            return $this->crudFromController($this->getActiveController() ?? $this->requestController ?? $controller);
78
        }
79
80
        return $this->cruds[$controller];
81
    }
82
83
    public function getRequestController(): ?string
84
    {
85
        return $this->requestController;
86
    }
87
88
    public function setActiveController(string $controller): void
89
    {
90
        Facade::clearResolvedInstance('crud');
91
        $this->currentlyActiveCrudController = $controller;
92
    }
93
94
    public function getActiveController(): ?string
95
    {
96
        return $this->currentlyActiveCrudController;
97
    }
98
99
    public function unsetActiveController(): void
100
    {
101
        $this->currentlyActiveCrudController = null;
102
    }
103
104
    public static function getCrudPanel(): CrudPanel
105
    {
106
        if (self::getActiveController()) {
0 ignored issues
show
Bug Best Practice introduced by
The method Backpack\CRUD\CrudPanelM...::getActiveController() is not static, but was called statically. ( Ignorable by Annotation )

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

106
        if (self::/** @scrutinizer ignore-call */ getActiveController()) {
Loading history...
107
            return self::crudFromController(self::getActiveController());
0 ignored issues
show
Bug introduced by
It seems like self::getActiveController() can also be of type null; however, parameter $controller of Backpack\CRUD\CrudPanelM...r::crudFromController() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

107
            return self::crudFromController(/** @scrutinizer ignore-type */ self::getActiveController());
Loading history...
Bug Best Practice introduced by
The method Backpack\CRUD\CrudPanelM...r::crudFromController() is not static, but was called statically. ( Ignorable by Annotation )

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

107
            return self::/** @scrutinizer ignore-call */ crudFromController(self::getActiveController());
Loading history...
108
        }
109
110
        // Prioritize explicit controller context
111
        $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
112
        $controller = null;
113
114
        foreach ($trace as $step) {
115
            if (isset($step['class']) &&
116
                is_a($step['class'], CrudControllerContract::class, true)) {
117
                $controller = (string) $step['class'];
118
                break;
119
            }
120
        }
121
122
        if ($controller) {
123
            $crudPanel = self::getControllerCrud($controller);
0 ignored issues
show
Bug Best Practice introduced by
The method Backpack\CRUD\CrudPanelM...er::getControllerCrud() is not static, but was called statically. ( Ignorable by Annotation )

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

123
            /** @scrutinizer ignore-call */ 
124
            $crudPanel = self::getControllerCrud($controller);
Loading history...
124
125
            return $crudPanel;
126
        }
127
128
        $cruds = self::getCruds();
0 ignored issues
show
Bug Best Practice introduced by
The method Backpack\CRUD\CrudPanelManager::getCruds() is not static, but was called statically. ( Ignorable by Annotation )

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

128
        /** @scrutinizer ignore-call */ 
129
        $cruds = self::getCruds();
Loading history...
129
130
        if (! empty($cruds)) {
131
            $crudPanel = reset($cruds);
132
133
            return $crudPanel;
134
        }
135
136
        return self::getCrudPanelInstance();
0 ignored issues
show
Bug Best Practice introduced by
The method Backpack\CRUD\CrudPanelM...:getCrudPanelInstance() is not static, but was called statically. ( Ignorable by Annotation )

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

136
        return self::/** @scrutinizer ignore-call */ getCrudPanelInstance();
Loading history...
137
    }
138
139
    public function getCruds(): array
140
    {
141
        return $this->cruds;
142
    }
143
}
144