Completed
Push — master ( 4c7c03...701d94 )
by Mahmoud
03:31
created

Output::ip()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace App\Containers\Debugger\Objects;
4
5
use Config;
6
use Illuminate\Http\Request;
7
use Illuminate\Http\Response;
8
use Jenssegers\Agent\Facades\Agent;
9
10
/**
11
 * Class Output
12
 *
13
 * @author  Mahmoud Zalt  <[email protected]>
14
 */
15
class Output
16
{
17
18
    /**
19
     * @var  string
20
     */
21
    public $output = '';
22
23
    /**
24
     * @var
25
     */
26
    private $request;
27
28
    /**
29
     * @var
30
     */
31
    private $response;
32
33
    /**
34
     * @var
35
     */
36
    protected $responseDataCut;
37
38
    /**
39
     * @var
40
     */
41
    protected $tokenDataCut;
42
43
    /**
44
     * Output constructor.
45
     *
46
     * @param \Illuminate\Http\Request  $request
47
     * @param \Illuminate\Http\Response $response
48
     */
49
    public function __construct(Request $request, Response $response)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
50
    {
51
        $this->request = $request;
52
        $this->response = $response;
53
54
        $this->responseDataCut = Config::get("debugger.requests.response_show_first");
55
        $this->tokenDataCut = Config::get("debugger.requests.token_show_first");
56
    }
57
58
    /**
59
     * @return  string
60
     */
61
    protected function set($text)
62
    {
63
        return $this->output = $text;
64
    }
65
66
    /**
67
     * @return  string
68
     */
69
    public function get()
70
    {
71
        return $this->output;
72
    }
73
74
    /**
75
     * @void
76
     */
77
    public function clear()
78
    {
79
        $this->set('');
80
    }
81
82
    /**
83
     * Add header
84
     *
85
     * @param $name
86
     */
87
    public function header($name)
88
    {
89
        $this->append("$name: \n");
90
    }
91
92
    /**
93
     * Add line to indicate new request
94
     *
95
     * @void
96
     */
97
    public function newRequest()
98
    {
99
        $this->append("----------------- NEW REQUEST -----------------");
100
    }
101
102
    /**
103
     * Add empty line
104
     *
105
     * @void
106
     */
107
    public function spaceLine()
108
    {
109
        $this->append("\n \n");
110
    }
111
112
    /**
113
     * @void
114
     */
115
    public function endpoint()
116
    {
117
        $this->append(" * Endpoint: " . $this->request->fullUrl() . "\n");
118
        $this->append(" * Method: " . $this->request->getMethod() . "\n");
119
    }
120
121
    /**
122
     * @void
123
     */
124
    public function version()
125
    {
126
        if (method_exists($this->request, 'version')) {
127
            $this->append(" * Version: " . $this->request->version() . "\n");
128
        }
129
    }
130
131
    /**
132
     * @void
133
     */
134
    public function ip()
135
    {
136
        $this->append(" * IP: " . $this->request->ip() . " (Port: " . $this->request->getPort() . ") \n");
137
    }
138
139
    /**
140
     * @void
141
     */
142
    public function format()
143
    {
144
        $this->append(" * Format: " . $this->request->format() . "\n");
145
    }
146
147
    /**
148
     * @void
149
     */
150
    public function userInfo()
151
    {
152
        // Auth Header
153
        $authHeader = $this->request->header("Authorization");
154
        // User
155
        $user = $this->request->user() ? "ID: " . $this->request->user()->id . " (Name: " . $this->request->user()->name . ")" : "N/A";
156
        // Browser
157
        $browser = Agent::browser();
158
159
        $this->append(" * Access Token: " . substr($authHeader, 0,
160
                $this->tokenDataCut) . (!is_null($authHeader) ? "..." : "N/A") . "\n");
161
        $this->append(" * User: " . $user . "\n");
162
        $this->append(" * Device: " . Agent::device() . " (Platform: " . Agent::platform() . ") \n");
163
        $this->append(" * Browser: " . $browser . " (Version: " . Agent::version($browser) . ") \n");
164
        $this->append(" * Languages: " . implode(", ", Agent::languages()) . "\n");
165
    }
166
167
    /**
168
     * @void
169
     */
170
    public function requestData()
171
    {
172
        // Request Data
173
        $requestData = $this->request->all() ? http_build_query($this->request->all(), "", " + ") : "N/A";
174
175
        $this->append(" * " . $requestData . "\n");
176
    }
177
178
    /**
179
     * @void
180
     */
181
    public function responseData()
182
    {
183
        // Response Data
184
        $responseContent = ($this->response && method_exists($this->response,
185
                "content")) ? $this->response->content() : "N/A";
186
187
        $this->append(" * " . substr($responseContent, 0, $this->responseDataCut) . "..." . "\n");
188
    }
189
190
    /**
191
     * @param $output
192
     *
193
     * @return  string
194
     */
195
    private function append($output)
196
    {
197
        return $this->output .= $output;
198
    }
199
200
}
201