ModelLogController::show()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 14
ccs 8
cts 8
cp 1
crap 1
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace Djunehor\EventRevert;
4
5
use Carbon\Carbon;
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Http\Request;
8
use Illuminate\Routing\Controller;
9
10
class ModelLogController extends Controller
11
{
12
    private $actions = ['update', 'delete', 'create'];
13
14 2
    public function index(Request $request)
15
    {
16 2
        $modelLogs = ModelLog::query();
17 2
        $modelLogs = $this->applyFilters($modelLogs, $request);
18 2
        $modelLogs = $modelLogs->orderByDesc('updated_at')->paginate($request->per_page ?? 15);
19 2
        $modelActions = $this->actions;
20
21 2
        return view('ModelEventLogger::log-table', compact('modelLogs', 'modelActions'));
22
    }
23
24 3
    public function applyFilters(Builder $modelLogs, $request) : Builder
25
    {
26 3
        if ($request->created_before) {
27 1
            $modelLogs->where('created_at', '<', Carbon::parse($request->created_before));
28
        }
29 3
        if ($request->created_after) {
30 1
            $modelLogs->where('created_at', '>', Carbon::parse($request->created_after));
31
        }
32 3
        if ($request->reverted_before) {
33 1
            $modelLogs->where('reverted_at', '<', Carbon::parse($request->reverted_before));
34
        }
35 3
        if ($request->reverted_after) {
36 1
            $modelLogs->where('reverted_at', '>', Carbon::parse($request->reverted_after));
37
        }
38 3
        if ($request->action) {
39 1
            $modelLogs->where('action', $request->action);
40
        }
41
42 3
        return $modelLogs;
43
    }
44
45 1
    public function show(ModelLog $log, Request $request)
46
    {
47 1
        $modelLogs = ModelLog::query()
48 1
                             ->where('subject_type', $log->subject_type)
49 1
                             ->where('subject_id', $log->subject_id);
50
51 1
        $modelLogs = $this->applyFilters($modelLogs, $request);
52
53 1
        $modelLogs = $modelLogs->orderByDesc('updated_at')->paginate($request->per_page ?? 15);
54
55 1
        $modelActions = $this->actions;
56
57 1
        return view('ModelEventLogger::log-table', compact('modelLogs', 'modelActions'));
58
    }
59
60 4
    public function revert($id, Request $request)
61
    {
62 4
        $log = ModelLog::findOrFail($id);
63
64 3
        if ($log->reverted_at) {
65 1
            return response()->json(trans('ModelEventLogger::model-event-logger.messages.logRevertedAlready'), 400);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
66
        }
67
68 3
        $revert = model_event_revert($log);
69
70 3
        if (! $revert['status']) {
71 1
            return response()->json($revert['message'], 400);
72
        }
73 2
        $log->revert_note = $request->revert_note;
74 2
        $log->reverted_at = now();
75 2
        $log->reverter_type = auth()->user() ? get_class($request->user()) : null;
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
76 2
        $log->reverter_id = auth()->id();
0 ignored issues
show
Bug introduced by
The method id does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
77 2
        $log->save();
78
79 2
        return response()->json(trans('ModelEventLogger::model-event-logger.messages.logRevertedSuccessfully'));
80
    }
81
}
82