Completed
Push — master ( 3c3d26...8e11a5 )
by Mahmoud
03:30
created

RequestsDebuggerTask::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace App\Containers\Debugger\Tasks;
4
5
use App;
6
use DB;
7
use Illuminate\Support\Facades\Config;
8
use Jenssegers\Agent\Facades\Agent;
9
use Log;
10
use Monolog\Formatter\LineFormatter;
11
use Monolog\Handler\StreamHandler;
12
use Monolog\Logger;
13
14
/**
15
 * Class RequestsDebuggerTask
16
 *
17
 * @author  Mahmoud Zalt  <[email protected]>
18
 */
19
class RequestsDebuggerTask
20
{
21
22
    CONST TESTING_ENV = 'testing';
23
24
    protected $responseDataCut;
25
26
    protected $tokenDataCut;
27
28
    protected $debuggingEnabled;
29
30
    protected $environment;
31
32
    protected $logger;
33
34
    protected $logFile;
35
36
    /**
37
     * RequestsDebuggerTask constructor.
38
     */
39
    public function __construct()
40
    {
41
        $this->prepareConfigs();
42
        $this->prepareLogger();
43
    }
44
45
    /**
46
     * @param $request
47
     * @param $response
48
     */
49
    public function run($request, $response)
50
    {
51
        if ($this->environment != self::TESTING_ENV && $this->debuggingEnabled === true) {
52
53
            // Prepare some data to be displayed
54
55
            // Auth Header
56
            $authHeader = $request->header("Authorization");
57
58
            // User
59
            $user = $request->user() ? "ID: " . $request->user()->id . " (Name: " . $request->user()->name . ")" : "N/A";
60
61
            // Browser
62
            $browser = Agent::browser();
63
64
            // Request Data
65
            $requestData = $request->all() ? http_build_query($request->all(), "", " + ") : "N/A";
66
67
            // Response Data
68
            $responseContent = ($response && method_exists($response, "content")) ? $response->content() : "N/A";
69
70
            // Call the Logger.
71
            $this->log($request, $user, $browser, $authHeader, $responseContent, $requestData);
72
        }
73
    }
74
75
    /**
76
     * Feel free to pass any extra data to be displayed.
77
     *
78
     * @param $request
79
     * @param $user
80
     * @param $browser
81
     * @param $authHeader
82
     * @param $responseContent
83
     * @param $requestData
84
     */
85
    private function log($request, $user, $browser, $authHeader, $responseContent, $requestData)
86
    {
87
        $print = "\n";
88
        $print .= "----------------- NEW REQUEST ---------------------------------------------------";
89
        $print .= "\n \n";
90
        $print .= "REQUEST: \n";
91
        $print .= "   Endpoint: " . $request->fullUrl() . "\n";
92
        $print .= "   Method: " . $request->getMethod() . "\n";
93
        $print .= "   Version: " . $request->version() . "\n";
94
        $print .= "   IP: " . $request->ip() . " (Port: " . $request->getPort() . ") \n";
95
        $print .= "   Format: " . $request->format() . "\n";
96
97
        $print .= "\n \n";
98
        $print .= "USER: \n";
99
100
        $print .= "   Access Token: " . substr($authHeader, 0,
101
            $this->tokenDataCut) . !is_null($authHeader) ? "..." : "N/A" . "\n";
102
        $print .= "   User: " . $user . "\n";
103
        $print .= "   Device: " . Agent::device() . " (Platform: " . Agent::platform() . ") \n";
104
        $print .= "   Browser: " . $browser . " (Version: " . Agent::version($browser) . ") \n";
105
        $print .= "   Languages: " . implode(", ", Agent::languages()) . "\n";
106
107
        $print .= "\n \n";
108
        $print .= "REQUEST DATA: \n";
109
        $print .= "   " . $requestData . "\n";
110
        $print .= "\n \n";
111
        $print .= "RESPONSE DATA: \n";
112
        $print .= "   " . substr($responseContent, 0, $this->responseDataCut) . "..." . "\n";
113
114
        // ...
115
        // ......
116
        // ...
117
118
        $print .= "\n";
119
        // Log the String
120
        $this->logger->info($print);
121
    }
122
123
    /**
124
     * @void
125
     */
126
    private function prepareConfigs()
127
    {
128
        $this->environment = App::environment();
129
130
        $this->debuggingEnabled = Config::get("debugger.requests.debug");
131
        $this->logFile = Config::get("debugger.requests.log_file");
132
        $this->responseDataCut = Config::get("debugger.requests.response_show_first");
133
        $this->tokenDataCut = Config::get("debugger.requests.token_show_first");
134
    }
135
136
    /**
137
     * @void
138
     */
139
    private function prepareLogger()
140
    {
141
        $handler = new StreamHandler(storage_path('logs/' . $this->logFile));
142
        $handler->setFormatter(new LineFormatter(null, null, true, true));
143
144
        $this->logger = new Logger("REQUESTS DEBUGGER");
145
        $this->logger->pushHandler($handler, Logger::INFO);
0 ignored issues
show
Unused Code introduced by
The call to Logger::pushHandler() has too many arguments starting with \Monolog\Logger::INFO.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
146
    }
147
148
}
149