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

Test Setup Failed
Pull Request — master (#4161)
by
unknown
15:22
created

ShowOperation   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 55
c 1
b 0
f 0
dl 0
loc 146
rs 10
wmc 28

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setupShowRoutes() 0 6 1
C removeColumnsThatDontBelongInsideShowOperation() 0 35 14
A setupShowDefaults() 0 33 4
A autoSetupShowOperation() 0 20 6
A show() 0 19 3
1
<?php
2
3
namespace Backpack\CRUD\app\Http\Controllers\Operations;
4
5
use Illuminate\Support\Facades\Route;
6
7
trait ShowOperation
8
{
9
    /**
10
     * Define which routes are needed for this operation.
11
     *
12
     * @param  string  $segment  Name of the current entity (singular). Used as first URL segment.
13
     * @param  string  $routeName  Prefix of the route name.
14
     * @param  string  $controller  Name of the current CrudController.
15
     */
16
    protected function setupShowRoutes($segment, $routeName, $controller)
17
    {
18
        Route::get($segment.'/{id}/show', [
19
            'as'        => $routeName.'.show',
20
            'uses'      => $controller.'@show',
21
            'operation' => 'show',
22
        ]);
23
    }
24
25
    /**
26
     * Add the default settings, buttons, etc that this operation needs.
27
     */
28
    protected function setupShowDefaults()
29
    {
30
        $this->crud->allowAccess('show');
31
        $this->crud->setOperationSetting('setFromDb', true);
32
33
        $this->crud->operation('show', function () {
34
            $this->crud->loadDefaultOperationSettingsFromConfig();
35
36
            if (! method_exists($this, 'setupShowOperation')) {
37
                $this->autoSetupShowOperation();
38
            }
39
        });
40
41
        $this->crud->operation('list', function () {
42
            $this->crud->addButton('line', 'show', 'view', 'crud::buttons.show', 'beginning');
43
        });
44
45
        $this->crud->operation(['create', 'update'], function () {
46
            $this->crud->addSaveAction([
47
                'name' => 'save_and_preview',
48
                'visible' => function ($crud) {
49
                    return $crud->hasAccess('show');
50
                },
51
                'redirect' => function ($crud, $request, $itemId = null) {
52
                    $itemId = $itemId ?: $request->input('id');
53
                    $redirectUrl = $crud->route.'/'.$itemId.'/show';
54
                    if ($request->has('_locale')) {
55
                        $redirectUrl .= '?_locale='.$request->input('_locale');
56
                    }
57
58
                    return $redirectUrl;
59
                },
60
                'button_text' => trans('backpack::crud.save_action_save_and_preview'),
61
            ]);
62
        });
63
    }
64
65
    /**
66
     * Display the specified resource.
67
     *
68
     * @param  int  $id
69
     * @return Response
0 ignored issues
show
Bug introduced by
The type Backpack\CRUD\app\Http\C...ers\Operations\Response was not found. Did you mean Response? If so, make sure to prefix the type with \.
Loading history...
70
     */
71
    public function show($id)
72
    {
73
        $this->crud->hasAccessOrFail('show');
74
75
        // get entry ID from Request (makes sure its the last ID for nested resources)
76
        $id = $this->crud->getCurrentEntryId() ?? $id;
77
78
        // get the info for that entry (include softDeleted items if the trait is used)
79
        if ($this->crud->get('show.softDeletes') && in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($this->crud->model))) {
80
            $this->data['entry'] = $this->crud->getModel()->withTrashed()->findOrFail($id);
0 ignored issues
show
Bug Best Practice introduced by
The property data does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
81
        } else {
82
            $this->data['entry'] = $this->crud->getEntry($id);
83
        }
84
85
        $this->data['crud'] = $this->crud;
86
        $this->data['title'] = $this->crud->getTitle() ?? trans('backpack::crud.preview').' '.$this->crud->entity_name;
0 ignored issues
show
Bug introduced by
Are you sure trans('backpack::crud.preview') of type array|string can be used in concatenation? ( Ignorable by Annotation )

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

86
        $this->data['title'] = $this->crud->getTitle() ?? /** @scrutinizer ignore-type */ trans('backpack::crud.preview').' '.$this->crud->entity_name;
Loading history...
87
88
        // load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package
89
        return view($this->crud->getShowView(), $this->data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view($this->crud-...howView(), $this->data) returns the type Illuminate\View\View which is incompatible with the documented return type Backpack\CRUD\app\Http\C...ers\Operations\Response.
Loading history...
90
    }
91
92
    /**
93
     * Default behaviour for the Show Operation, in case none has been
94
     * provided by including a setupShowOperation() method in the CrudController.
95
     */
96
    protected function autoSetupShowOperation()
97
    {
98
        // guess which columns to show, from the database table
99
        if ($this->crud->get('show.setFromDb')) {
100
            $this->crud->setFromDb(false, true);
101
        }
102
103
        // if the model has timestamps, add columns for created_at and updated_at
104
        if ($this->crud->get('show.timestamps') && $this->crud->model->usesTimestamps()) {
105
            $this->crud->column($this->crud->model->getCreatedAtColumn())->type('datetime');
106
            $this->crud->column($this->crud->model->getUpdatedAtColumn())->type('datetime');
107
        }
108
109
        // if the model has SoftDeletes, add column for deleted_at
110
        if ($this->crud->get('show.softDeletes') && in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($this->crud->model))) {
111
            $this->crud->column($this->crud->model->getDeletedAtColumn())->type('datetime');
112
        }
113
114
        // remove the columns that usually don't make sense inside the Show operation
115
        $this->removeColumnsThatDontBelongInsideShowOperation();
116
    }
117
118
    protected function removeColumnsThatDontBelongInsideShowOperation()
119
    {
120
        // cycle through columns
121
        foreach ($this->crud->columns() as $key => $column) {
122
123
            // remove any autoset relationship columns
124
            if (array_key_exists('model', $column) && array_key_exists('autoset', $column) && $column['autoset']) {
125
                $this->crud->removeColumn($column['key']);
126
            }
127
128
            // remove any autoset table columns
129
            if ($column['type'] == 'table' && array_key_exists('autoset', $column) && $column['autoset']) {
130
                $this->crud->removeColumn($column['key']);
131
            }
132
133
            // remove the row_number column, since it doesn't make sense in this context
134
            if ($column['type'] == 'row_number') {
135
                $this->crud->removeColumn($column['key']);
136
            }
137
138
            // remove columns that have visibleInShow set as false
139
            if (isset($column['visibleInShow'])) {
140
                if ((is_callable($column['visibleInShow']) && $column['visibleInShow']($this->data['entry']) === false) || $column['visibleInShow'] === false) {
141
                    $this->crud->removeColumn($column['key']);
142
                }
143
            }
144
145
            // remove the character limit on columns that take it into account
146
            if (in_array($column['type'], ['text', 'email', 'model_function', 'model_function_attribute', 'phone', 'row_number', 'select'])) {
147
                $this->crud->modifyColumn($column['key'], ['limit' => ($column['limit'] ?? 999)]);
148
            }
149
        }
150
151
        // remove bulk actions colums
152
        $this->crud->removeColumns(['blank_first_column', 'bulk_actions']);
153
    }
154
}
155