Passed
Push — master ( 77b30e...dc6911 )
by Zacchaeus
10:27 queued 02:55
created

ModelLogController::applyFilters()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 8.6047

Importance

Changes 0
Metric Value
cc 6
nc 32
nop 2
dl 0
loc 20
ccs 7
cts 12
cp 0.5833
crap 8.6047
rs 8.9777
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 1
    public function index(Request $request)
15
    {
16 1
        $modelLogs = ModelLog::query();
17 1
        $modelLogs = $this->applyFilters($modelLogs, $request);
18 1
        $modelLogs = $modelLogs->orderByDesc('updated_at')->paginate($request->per_page ?? 15);
19 1
        $modelActions = $this->actions;
20
21 1
        return view('ModelEventLogger::log-table', compact('modelLogs', 'modelActions'));
22
    }
23
24 2
    public function applyFilters(Builder $modelLogs, $request) : Builder
25
    {
26 2
        if ($request->created_before) {
27
            $modelLogs->where('created_at', '<', Carbon::parse($request->created_before));
28
        }
29 2
        if ($request->created_after) {
30
            $modelLogs->where('created_at', '>', Carbon::parse($request->created_after));
31
        }
32 2
        if ($request->reverted_before) {
33
            $modelLogs->where('reverted_at', '<', Carbon::parse($request->reverted_before));
34
        }
35 2
        if ($request->reverted_after) {
36
            $modelLogs->where('reverted_at', '>', Carbon::parse($request->reverted_after));
37
        }
38 2
        if ($request->action) {
39
            $modelLogs->where('action', $request->action);
40
        }
41
42 2
        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 1
    public function revert($id, Request $request)
61
    {
62 1
        $log = ModelLog::findOrFail($id);
63
64 1
        if ($log->reverted_at) {
65
            return response(trans('ModelEventLogger::model-event-logger.messages.logRevertedAlready'), 400);
66
        }
67
68 1
        $revert = model_event_revert($log);
69
70 1
        if (! $revert['status']) {
71
            return response($revert['message'], 400);
72
        }
73 1
        $log->revert_note = $request->revert_note;
74 1
        $log->reverted_at = now();
75 1
        $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 1
        $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 1
        $log->save();
78
79 1
        return response(trans('ModelEventLogger::model-event-logger.messages.logRevertedSuccessfully'));
80
    }
81
}
82