Completed
Push — master ( 071fd8...3c3f7e )
by Mahmoud
03:40
created

RequestsMonitorMiddleware::handle()   C

Complexity

Conditions 7
Paths 9

Size

Total Lines 51
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 51
rs 6.9743
cc 7
eloc 25
nc 9
nop 2

How to fix   Long Method   

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 App\Containers\Debugger\Middlewares;
4
5
use App;
6
use Closure;
7
use Config;
8
use Illuminate\Http\Request;
9
use Log;
10
11
/**
12
 * Class RequestsMonitorMiddleware
13
 *
14
 * @author  Mahmoud Zalt  <[email protected]>
15
 */
16
class RequestsMonitorMiddleware
17
{
18
19
    /**
20
     * @param \Illuminate\Http\Request $request
21
     * @param \Closure                 $next
22
     *
23
     * @return  mixed
24
     */
25
    public function handle(Request $request, Closure $next)
26
    {
27
28
        $response = $next($request);
29
30
        if (App::environment() != 'testing' && Config::get('app.debug') === true) {
31
32
            Log::debug('');
33
            Log::debug('');
34
            Log::debug('REQUEST START------------------------------------------------------');
35
36
            // Endpoint URL:
37
            Log::debug('URL: ' . $request->getMethod() . ' ' . $request->fullUrl());
38
39
            // Request Device IP:
40
            Log::debug('IP: ' . $request->ip());
41
42
            // Request Headers:
43
            Log::debug('App Headers: ');
44
            Log::debug('   Authorization = ' . substr($request->header('Authorization'), 0, 80) . '...');
45
46
            // Request Data:
47
            if ($request->all()) {
48
                $data = http_build_query($request->all(), '', ' ; ');
49
            } else {
50
                $data = 'N/A';
51
            }
52
            Log::debug('Request Data: ' . $data);
53
54
            // Authenticated User:
55
            if ($request->user()) {
56
                $user = 'ID: ' . $request->user()->id;
57
            } else {
58
                $user = 'N/A';
59
            }
60
            Log::debug('Authenticated User: ' . $user);
61
62
            // Response Content:
63
            if ($response && method_exists($response, 'content')) {
64
                Log::debug('Response: ' . substr($response->content(), 0, 700) . '...');
65
            }
66
67
            Log::debug('');
68
            Log::debug('');
69
70
        }
71
72
        // Perform action
73
74
        return $response;
75
    }
76
}
77