Completed
Push — master ( ab60f2...c901a1 )
by Ricardo
07:00
created

AccessLogsController::index()   F

Complexity

Conditions 25
Paths 9216

Size

Total Lines 92

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 92
rs 0
c 0
b 0
f 0
cc 25
nc 9216
nop 1

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 Fabrica\Http\Api;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Facades\Event;
7
8
use Fabrica\Http\Requests;
9
use Fabrica\Http\Api\Controller;
10
11
use Fabrica\System\Eloquent\ApiAccessLogs;
12
use Maatwebsite\Excel\Facades\Excel;
13
14
class AccessLogsController extends Controller
15
{
16
    /**
17
     * Display a listing of the resource.
18
     *
19
     * @return \Illuminate\Http\Response
20
     */
21
    public function index(Request $request)
22
    {
23
        ini_set('memory_limit', '-1');
24
        ini_set('max_execution_time', '0');
25
26
        $query = ApiAccessLogs::query();
27
28
        $uid = $request->input('uid');
29
        if (isset($uid) && $uid)
30
        {
31
            $query = $query->where('user.id', $uid);
32
        }
33
34
        $method = $request->input('method');
35
        if (isset($method) && $method)
36
        {
37
            $query = $query->where('request_method', $method);
38
        }
39
40
        $project_key = $request->input('project_key');
41
        if (isset($project_key) && $project_key)
42
        {
43
            $query = $query->where('project_key', $project_key);
44
        }
45
46
        $module = $request->input('module');
47
        if (isset($module) && $module)
48
        {
49
            $query = $query->where('module', $module);
50
        }
51
52
        $request_time = $request->input('request_time');
53
        if (isset($request_time) && $request_time)
54
        {
55
            if (strpos($request_time, '~') !== false)
56
            {
57
                $sections = explode('~', $request_time);
58
                if ($sections[0])
59
                {
60
                    $query->where('requested_start_at', '>=', strtotime($sections[0]) * 1000);
61
                }
62
                if ($sections[1])
63
                {
64
                    $query->where('requested_start_at', '<=', strtotime($sections[1] . ' 23:59:59') * 1000);
65
                }
66
            }
67
        }
68
69
        $url = $request->input('request_url');
70
        if (isset($url) && $url)
71
        {
72
            $query->where('request_url', 'like', '%' . $url . '%');
73
        }
74
75
        $request_source_ip = $request->input('request_source_ip');
76
        if (isset($request_source_ip) && $request_source_ip)
77
        {
78
            $query->where('request_source_ip', $request_source_ip);
79
        }
80
81
        $exec_time = $request->input('exec_time');
82
        if (isset($exec_time) && $exec_time)
83
        {
84
            $flag = substr($exec_time, 0, 1);
85
            if ($flag == '-')
86
            {
87
                $query->where('exec_time', '<=', abs(floatval($exec_time)) * 1000);
88
            }
89
            else
90
            {
91
                $query->where('exec_time', '>=', abs(floatval($exec_time)) * 1000);
92
            }
93
        }
94
95
        // get total
96
        $total = $query->count();
97
        $query->orderBy('_id', 'desc');
98
        $page_size = $request->input('limit') ? intval($request->input('limit')) : 100;
99
        $page = $request->input('page') ?: 1;
100
        $query = $query->skip($page_size * ($page - 1))->take($page_size);
101
102
        $logs = $query->get();
103
104
        $from = $request->input('from');
105
        if (isset($from) && $from == 'export')
106
        {
107
            $this->export($logs);
108
            exit();
109
        }
110
111
        return Response()->json([ 'ecode' => 0, 'data' => $logs, 'options' => [ 'total' => $total, 'sizePerPage' => $page_size ] ]);
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...
112
    }
113
114
    /**
115
     * export xls for access logs
116
     *
117
     * @param  array $issues
0 ignored issues
show
Bug introduced by
There is no parameter named $issues. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
118
     * @return void
119
     */
120
    public function export($logs)
121
    {
122
        set_time_limit(0);
123
124
        $headers = [ 
125
            '用户', 
126
            '方法', 
127
            'Url', 
128
            '模块', 
129
            '请求开始时间', 
130
            '请求结束时间', 
131
            '请求时长(毫秒)', 
132
            '来源IP', 
133
            'User-Agent', 
134
            'Body' 
135
        ];
136
137
        $file_name = 'access-logs';
138
        Excel::create($file_name, function ($excel) use($headers, $logs) {
139
            $excel->sheet('Sheetname', function ($sheet) use($headers, $logs) {
140
                $sheet->appendRow($headers);
141
                foreach ($logs as $log)
142
                {
143
                    $tmp = [];
144
                    $tmp[] = $log->user ? $log->user['name'] : '';
145
                    $tmp[] = $log->request_method ?: '';
146
                    $tmp[] = $log->request_url ?: '';
147
                    $tmp[] = $log->module ?: '';
148
                    $tmp[] = $log->requested_start_at ? date('Y-m-d h:i:s', intval($log->requested_start_at / 1000)): '';
149
                    $tmp[] = $log->requested_end_at ? date('Y-m-d h:i:s', intval($log->requested_end_at / 1000)) : '';
150
                    $tmp[] = $log->exec_time ?: '';
151
                    $tmp[] = $log->request_source_ip ?: '';
152
                    $tmp[] = $log->request_user_agent ?: '';
153
                    $tmp[] = $log->request_body ? json_encode($log->request_body) : '';
154
                    $sheet->appendRow($tmp);
155
                }
156
            });
157
        })->download('xls');
158
    }
159
}
160