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 Failed
Pull Request — master (#3399)
by
unknown
11:40
created

CreateDummyOperation   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 30
c 2
b 1
f 0
dl 0
loc 76
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setupCreateDummyDefaults() 0 11 2
A setupCreateDummyRoutes() 0 6 1
A createDummy() 0 35 4
1
<?php
2
3
namespace Backpack\CRUD\app\Http\Controllers\Operations;
4
5
use Illuminate\Support\Facades\Route;
6
7
trait CreateDummyOperation
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 setupCreateDummyRoutes($segment, $routeName, $controller)
17
    {
18
        Route::post($segment.'/create-dummy', [
19
            'as' => $routeName.'.createDummy',
20
            'uses' => $controller.'@createDummy',
21
            'operation' => 'createDummy',
22
        ]);
23
    }
24
25
    /**
26
     * Add the default settings, buttons, etc that this operation needs.
27
     */
28
    protected function setupCreateDummyDefaults()
29
    {
30
        $this->crud->allowAccess('createDummy');
31
32
        $this->crud->operation('createDummy', function () {
33
            $this->crud->loadDefaultOperationSettingsFromConfig();
34
        });
35
36
        $this->crud->operation('list', function () {
37
            if (method_exists($this->crud->getModel(), 'factory')) {
38
                $this->crud->addButton('top', 'create_dummy', 'view', 'crud::buttons.create_dummy');
39
            }
40
        });
41
    }
42
43
    /**
44
     * Show the form for creating inserting a new row.
45
     *
46
     * @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...
47
     */
48
    public function createDummy()
49
    {
50
        // Access
51
        $this->crud->hasAccessOrFail('createDummy');
52
53
        // Check if Model has Factory trait
54
        if (! method_exists($this->crud->getModel(), 'factory')) {
55
            return response()->json([
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(..._error_message')), 500) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Backpack\CRUD\app\Http\C...ers\Operations\Response.
Loading history...
56
                'title' => trans('backpack::crud.create_dummy_error_title'),
57
                'message' => trans('backpack::crud.create_dummy_error_message'),
58
            ], 500);
59
        }
60
61
        // Options
62
        $TRUNCATE = 1;
63
        $CREATE = 2;
64
65
        $count = request()->input('count');
66
        $option = request()->input('option');
67
68
        // Truncate table
69
        if ($option & $TRUNCATE) {
70
            $this->crud->getModel()->truncate();
71
            $messages[] = trans('backpack::crud.create_dummy_success_truncate');
0 ignored issues
show
Comprehensibility Best Practice introduced by
$messages was never initialized. Although not strictly required by PHP, it is generally a good practice to add $messages = array(); before regardless.
Loading history...
72
        }
73
74
        // Create dummy entries
75
        if ($option & $CREATE) {
76
            $this->crud->getModel()::factory()->count($count)->create();
77
            $messages[] = trans_choice('backpack::crud.create_dummy_sucess_message', $count, ['count' => $count]);
78
        }
79
80
        return response()->json([
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(... join(' ', $messages))) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Backpack\CRUD\app\Http\C...ers\Operations\Response.
Loading history...
81
            'title' => trans('backpack::crud.create_dummy_sucess_title'),
82
            'message' => join(' ', $messages),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $messages does not seem to be defined for all execution paths leading up to this point.
Loading history...
83
        ]);
84
    }
85
}
86