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 ( f66a1f...9806d9 )
by Pedro
11:53
created

Datatable::applyWidgetDatatableConfig()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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

24
        CrudManager::/** @scrutinizer ignore-call */ 
25
                     setActiveController($controller);
Loading history...
25
26
        $this->crud ??= CrudManager::crudFromController($controller, 'list');
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

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

38
        if (! $this->crud->/** @scrutinizer ignore-call */ has('list.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...
39
            $this->crud->set('list.datatablesUrl', $this->crud->getRoute());
40
        }
41
42
        // Reset the active controller
43
        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

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

66
        /** @scrutinizer ignore-call */ 
67
        $cruds = CrudManager::getCruds();
Loading history...
67
        $parentCrud = reset($cruds);
68
69
        if ($parentCrud && $parentCrud->getCurrentEntry()) {
70
            $parentEntry = $parentCrud->getCurrentEntry();
71
            $parentController = $parentCrud->controller;
72
            $cacheKey = 'datatable_config_'.$this->tableId;
73
74
            Cache::forget($cacheKey);
75
76
            // Store the controller class, parent entry, element type and name
77
            Cache::put($cacheKey, [
78
                'controller' => $controllerClass,
79
                'parentController' => $parentController,
80
                'parent_entry' => $parentEntry,
81
                'element_type' => $this->type,
82
                'element_name' => $this->name,
83
            ], now()->addHours(1));
84
85
            $this->crud->set('list.datatable_id', $this->tableId);
86
        }
87
    }
88
89
    public static function applyCachedConfigurationClosure($crud)
90
    {
91
        $tableId = request('datatable_id');
92
93
        if (! $tableId) {
94
            \Log::debug('Missing datatable_id in request parameters');
95
96
            return false;
97
        }
98
99
        $cacheKey = 'datatable_config_'.$tableId;
100
        $cachedData = Cache::get($cacheKey);
101
102
        if (! $cachedData) {
103
            return false;
104
        }
105
106
        try {
107
            // Get the parent crud instance
108
            $parentCrud = CrudManager::crudFromController($cachedData['parentController']);
109
            $parentCrud->initialized = false;
110
            $parentCrud = CrudManager::crudFromController($cachedData['parentController'], 'show');
0 ignored issues
show
Unused Code introduced by
The assignment to $parentCrud is dead and can be removed.
Loading history...
111
            $entry = $cachedData['parent_entry'];
112
            // Get element type and name from cached data
113
            $elementType = $cachedData['element_type'];
114
            $elementName = $cachedData['element_name'];
115
116
            if ($elementType === 'widget') {
117
                $widgets = Widget::collection();
118
119
                foreach ($widgets as $widget) {
120
                    if ($widget['type'] === 'datatable' &&
121
                        (isset($widget['name']) && $widget['name'] === $elementName) &&
122
                        (isset($widget['configure']) && $widget['configure'] instanceof \Closure)) {
123
                            $widget['configure']($crud, $entry);
124
                            Cache::forget($cacheKey);
125
                            return true;
126
                    }
127
                }
128
                Cache::forget($cacheKey);
129
                return false;
130
            }
131
        } catch (\Exception $e) {
132
            \Log::error('Error applying cached datatable config: '.$e->getMessage(), [
133
                'exception' => $e,
134
            ]);
135
        }
136
        Cache::forget($cacheKey);
137
        return false;
138
    }
139
140
    public function render()
141
    {
142
        return view('crud::datatable.datatable', [
143
            'crud' => $this->crud,
144
            'updatesUrl' => $this->updatesUrl,
145
            'tableId' => $this->tableId,
146
        ]);
147
    }
148
}
149