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 ( 2d583e...f66a1f )
by Pedro
33:26
created

Datatable::applyCachedConfig()   C

Complexity

Conditions 13
Paths 27

Size

Total Lines 78
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 13
eloc 46
nc 27
nop 1
dl 0
loc 78
rs 6.6166
c 2
b 1
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B Datatable::applyCachedConfigurationClosure() 0 51 10
A Datatable::render() 0 6 1
A Datatable::applyWidgetDatatableConfig() 0 14 6

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
52
        return 'crudTable_'.$uniqueId;
53
    }
54
55
    /**
56
     * Store the datatable configuration in the cache for later use in Ajax requests.
57
     */
58
    private function storeDatatableConfig()
59
    {
60
        if (! $this->configure) {
61
            return;
62
        }
63
64
        $controllerClass = $this->controller;
65
        $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

65
        /** @scrutinizer ignore-call */ 
66
        $cruds = CrudManager::getCruds();
Loading history...
66
        $parentCrud = reset($cruds);
67
68
        if ($parentCrud && $parentCrud->getCurrentEntry()) {
69
            $parentEntry = $parentCrud->getCurrentEntry();
70
            $parentController = $parentCrud->controller;
71
            $cacheKey = 'datatable_config_'.$this->tableId;
72
73
            Cache::forget($cacheKey);
74
75
            // Store the controller class, parent entry, element type and name
76
            Cache::put($cacheKey, [
77
                'controller' => $controllerClass,
78
                'parentController' => $parentController,
79
                'parent_entry' => $parentEntry,
80
                'element_type' => $this->type,
81
                'element_name' => $this->name,
82
            ], now()->addHours(1));
83
84
            $this->crud->setOperationSetting('datatable_id', $this->tableId);
85
        }
86
    }
87
88
    public static function applyCachedConfigurationClosure($crud)
89
    {
90
        $tableId = request('datatable_id');
91
92
        if (! $tableId) {
93
            \Log::debug('Missing datatable_id in request parameters');
94
95
            return false;
96
        }
97
98
        $cacheKey = 'datatable_config_'.$tableId;
99
        $cachedData = Cache::get($cacheKey);
100
101
        if (! $cachedData) {
102
            \Log::debug('No cached configuration found for the given datatable_id');
103
104
            return false;
105
        }
106
107
        try {
108
            // Get the parent crud instance
109
            $parentCrud = CrudManager::crudFromController($cachedData['parentController'], 'show');
110
            $entry = $cachedData['parent_entry'];
111
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 = $parentCrud->getOperationSetting('widgets') ?? [];
118
                foreach ($widgets as $widget) {
119
                    if ($widget['type'] === 'datatable' &&
120
                        (isset($widget['name']) && $widget['name'] === $elementName) &&
121
                        isset($widget['configure'])) {
122
                        self::applyWidgetDatatableConfig($parentCrud, $crud, $elementName, $entry);
123
                        // clear the cache after applying the configuration
124
                        Cache::forget($cacheKey);
125
126
                        return true;
127
                    }
128
                }
129
130
                return false;
131
            }
132
        } catch (\Exception $e) {
133
            \Log::error('Error applying cached datatable config: '.$e->getMessage(), [
134
                'exception' => $e,
135
            ]);
136
        }
137
138
        return false;
139
    }
140
141
    private static function applyWidgetDatatableConfig($parentCrud, $crud, $elementName, $entry)
142
    {
143
        $widgets = $parentCrud->getOperationSetting('widgets') ?? [];
144
        foreach ($widgets as $widget) {
145
            if ($widget['type'] === 'datatable' &&
146
                (isset($widget['name']) && $widget['name'] === $elementName) &&
147
                isset($widget['configure'])) {
148
                ($widget['configure'])($crud, $entry);
149
150
                return true;
151
            }
152
        }
153
154
        return false;
155
    }
156
157
    public function render()
158
    {
159
        return view('crud::datatable.datatable', [
160
            'crud' => $this->crud,
161
            'updatesUrl' => $this->updatesUrl,
162
            'tableId' => $this->tableId,
163
        ]);
164
    }
165
}
166