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 ( 878ac2...dae5da )
by Pedro
13:06
created

Datatable::generateTableId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\app\Library\Datatable;
4
5
use Backpack\CRUD\app\Library\CrudPanel\CrudPanel;
6
use Backpack\CRUD\CrudManager;
7
use Illuminate\Support\Facades\Cache;
8
use Illuminate\View\Component;
9
10
class Datatable extends Component
11
{
12
    protected string $tableId;
13
14
    public function __construct(
15
        private string $controller,
16
        private ?CrudPanel $crud = null,
17
        private bool $updatesUrl = true,
18
        private ?\Closure $configure = null,
19
        private ?string $type = null,
20
        private ?string $name = null
21
    ) {
22
        // Set active controller for proper context
23
        CrudManager::setActiveController($controller);
0 ignored issues
show
Bug introduced by
The method setActiveController() 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

23
        CrudManager::/** @scrutinizer ignore-call */ 
24
                     setActiveController($controller);
Loading history...
24
25
        $this->crud ??= CrudManager::crudFromController($controller);
0 ignored issues
show
Bug introduced by
The method crudFromController() 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

25
        $this->crud ??= CrudManager::/** @scrutinizer ignore-call */ crudFromController($controller);
Loading history...
26
27
        $this->tableId = $this->generateTableId();
28
29
        if ($this->configure) {
30
            // Apply the configuration
31
            ($this->configure)($this->crud);
32
33
            // Store the configuration in cache for Ajax requests
34
            $this->storeDatatableConfig();
35
        }
36
37
        if (! $this->crud->getOperationSetting('datatablesUrl')) {
0 ignored issues
show
Bug introduced by
The method getOperationSetting() 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

37
        if (! $this->crud->/** @scrutinizer ignore-call */ getOperationSetting('datatablesUrl')) {

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...
38
            $this->crud->setOperationSetting('datatablesUrl', $this->crud->getRoute());
39
        }
40
41
        // Reset the active controller
42
        CrudManager::unsetActiveController($controller);
0 ignored issues
show
Bug introduced by
The method unsetActiveController() 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

42
        CrudManager::/** @scrutinizer ignore-call */ 
43
                     unsetActiveController($controller);
Loading history...
43
    }
44
45
    private function generateTableId(): string
46
    {
47
        $controllerPart = str_replace('\\', '_', $this->controller);
48
        $typePart = $this->type ?? 'default';
49
        $namePart = $this->name ?? 'default';
50
        $uniqueId = md5($controllerPart.'_'.$typePart.'_'.$namePart);
51
        return 'crudTable_'.$uniqueId;
52
    }
53
54
    /**
55
     * Store the datatable configuration in the cache for later use in Ajax requests.
56
     */
57
    private function storeDatatableConfig()
58
    {
59
        if (! $this->configure) {
60
            return;
61
        }
62
63
        $controllerClass = $this->controller;
64
        $cruds = CrudManager::getCruds();
0 ignored issues
show
Bug introduced by
The method getCruds() 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

64
        /** @scrutinizer ignore-call */ 
65
        $cruds = CrudManager::getCruds();
Loading history...
65
        $parentCrud = reset($cruds);
66
67
        if ($parentCrud && $parentCrud->getCurrentEntry()) {
68
            $parentEntry = $parentCrud->getCurrentEntry();
69
            $parentController = $parentCrud->controller;
70
            $cacheKey = 'datatable_config_'.$this->tableId;
71
72
            // Store the controller class, parent entry, element type and name
73
            Cache::put($cacheKey, [
74
                'controller' => $controllerClass,
75
                'parentController' => $parentController,
76
                'parent_entry' => $parentEntry,
77
                'element_type' => $this->type,
78
                'element_name' => $this->name,
79
            ], now()->addHours(1));
80
81
            $this->crud->setOperationSetting('datatable_id', $this->tableId);
82
        }
83
    }
84
85
    public static function applyCachedConfig($crud)
86
    {
87
        $tableId = request('datatable_id');
88
89
        if (! $tableId) {
90
            \Log::debug('Missing datatable_id in request parameters');
91
92
            return false;
93
        }
94
95
        $cacheKey = 'datatable_config_'.$tableId;
96
        $cachedData = Cache::get($cacheKey);
97
98
        if (! $cachedData) {
99
            \Log::debug('No cached configuration found for the given datatable_id');
100
101
            return false;
102
        }
103
104
        try {
105
            \Log::debug('Found matching configuration by table ID', [
106
                'controller' => $cachedData['controller'],
107
                'element_type' => $cachedData['element_type'],
108
                'element_name' => $cachedData['element_name'],
109
                'table_id' => $tableId,
110
            ]);
111
112
            // Get the parent crud instance
113
            $parentCrud = CrudManager::crudFromController($cachedData['parentController'], 'show');
114
            $entry = $cachedData['parent_entry'];
115
116
            // Get element type and name from cached data
117
            $elementType = $cachedData['element_type'];
118
            $elementName = $cachedData['element_name'];
119
120
            \Log::debug('Element type and name', [
121
                'element_type' => $elementType,
122
                'element_name' => $elementName,
123
            ]);
124
125
            if ($elementType === 'column') {
126
                $column = $parentCrud->columns()[$elementName] ?? null;
127
                if ($column && isset($column['configure'])) {
128
                    self::applyColumnDatatableConfig($parentCrud, $crud, $elementName, $entry);
129
                    // clear the cache after applying the configuration
130
                    Cache::forget($cacheKey);
131
132
                    return true;
133
                }
134
                \Log::debug('Column not found or no configure closure defined');
135
136
                return false;
137
            } elseif ($elementType === 'widget') {
138
                $widgets = $parentCrud->getOperationSetting('widgets') ?? [];
139
                foreach ($widgets as $widget) {
140
                    if ($widget['type'] === 'datatable' &&
141
                        (isset($widget['name']) && $widget['name'] === $elementName) &&
142
                        isset($widget['configure'])) {
143
                        self::applyWidgetDatatableConfig($parentCrud, $crud, $elementName, $entry);
144
                        // clear the cache after applying the configuration
145
                        Cache::forget($cacheKey);
146
147
                        return true;
148
                    }
149
                }
150
                \Log::debug('Widget not found or no configure closure defined');
151
152
                return false;
153
            }
154
        } catch (\Exception $e) {
155
            \Log::error('Error applying cached datatable config: '.$e->getMessage(), [
156
                'exception' => $e,
157
            ]);
158
        }
159
160
        \Log::debug('No matching configuration found');
161
162
        return false;
163
    }
164
165
    private static function applyColumnDatatableConfig($parentCrud, $crud, $elementName, $entry)
166
    {
167
        $column = $parentCrud->columns()[$elementName];
168
        if (isset($column['configure'])) {
169
            ($column['configure'])($crud, $entry);
170
171
            return true;
172
        }
173
174
        return false;
175
    }
176
177
    private static function applyWidgetDatatableConfig($parentCrud, $crud, $elementName, $entry)
178
    {
179
        $widgets = $parentCrud->getOperationSetting('widgets') ?? [];
180
        foreach ($widgets as $widget) {
181
            if ($widget['type'] === 'datatable' &&
182
                (isset($widget['name']) && $widget['name'] === $elementName) &&
183
                isset($widget['configure'])) {
184
                ($widget['configure'])($crud, $entry);
185
186
                return true;
187
            }
188
        }
189
190
        return false;
191
    }
192
193
    public function render()
194
    {
195
        return view('crud::datatable.datatable', [
196
            'crud' => $this->crud,
197
            'updatesUrl' => $this->updatesUrl,
198
            'tableId' => $this->tableId,
199
        ]);
200
    }
201
}
202