FirewallRequestsLog::prepareResponseData()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 11
nc 2
nop 1
dl 0
loc 18
ccs 0
cts 16
cp 0
crap 6
rs 9.9
c 1
b 0
f 0
1
<?php
2
3
namespace Someshwer\Firewall\Middleware;
4
5
use Closure;
6
use Illuminate\Http\Response;
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Illuminate\Support\Facades\Log;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Facades\Log was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Someshwer\Firewall\src\Entities\FirewallRequestsLogModel;
9
10
/**
11
 * Class FirewallRequestsLog.
12
 *
13
 * @author Someshwer Bandapally
14
 * Date: 14-08-2018
15
 *
16
 * Each and every incoming request is logged and stored into database.
17
 */
18
class FirewallRequestsLog
19
{
20
    /**
21
     * @var \Illuminate\Config\Repository|mixed
0 ignored issues
show
Bug introduced by
The type Illuminate\Config\Repository was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
     */
23
    private $log_request;
24
25
    /**
26
     * FirewallRequestsLog constructor.
27
     */
28
    public function __construct()
29
    {
30
        $this->log_request = config('firewall.log_request');
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
        $this->log_request = /** @scrutinizer ignore-call */ config('firewall.log_request');
Loading history...
31
    }
32
33
    /**
34
     * Handle an incoming request.
35
     *
36
     * @param \Illuminate\Http\Request $request
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
37
     * @param \Closure                 $next
38
     *
39
     * @return mixed
40
     */
41
    public function handle($request, Closure $next)
42
    {
43
        $firewall_requests_log = new FirewallRequestsLogModel();
44
        if ($this->log_request == true) {
45
            $firewall_requests_log = $this->prepareAndSaveLogData($request, $firewall_requests_log);
46
        }
47
        $response = $next($request);
48
        $response_data = $this->prepareResponseData($response);
49
        $this->logResponseData($response_data, $firewall_requests_log);
50
51
        return $response;
52
    }
53
54
    /**
55
     * @param $request
56
     * @param $firewall_requests_log
57
     *
58
     * @return FirewallRequestsLogModel
59
     *
60
     * Prepares request data to be stored in a table.
61
     * And stores prepared data in a table.
62
     */
63
    private function prepareAndSaveLogData($request, $firewall_requests_log)
64
    {
65
        $firewall_requests_log->fill([
66
            'path'             => $request->path(),
67
            'url'              => $request->url(),
68
            'full_url'         => $request->fullUrl(),
69
            'method'           => $_SERVER['REQUEST_METHOD'],
70
            'uri'              => $_SERVER['REQUEST_URI'],
71
            'query'            => $request->query() ? $request->query() : null,
72
            'file_name'        => $_SERVER['SCRIPT_FILENAME'],
73
            'http_host'        => $_SERVER['HTTP_HOST'],
74
            'http_user_agent'  => $_SERVER['HTTP_USER_AGENT'],
75
            'ip_address'       => $request->ip(),
76
            'all_request_data' => $_SERVER,
77
        ]);
78
        $firewall_requests_log->save();
79
80
        return $firewall_requests_log;
81
    }
82
83
    /**
84
     * Preparing response data to be stored.
85
     *
86
     * @param $response
87
     *
88
     * @return array
89
     */
90
    private function prepareResponseData($response)
91
    {
92
        try {
93
            $response_data = [
94
                'status_code' => $response->getStatusCode(),
95
                'headers'     => [
96
                    'cache_control' => $response->headers->get('cache-control'),
97
                    'content_type'  => $response->headers->get('content-type'),
98
                    'date'          => $response->headers->get('date'),
99
                ],
100
                // 'original_data'=>$response->getOriginalContent(),
101
            ];
102
        } catch (\Exception $e) {
103
            Log::error($e);
104
            $response_data = null;
105
        }
106
107
        return $response_data;
108
    }
109
110
    /**
111
     * This logs and stores the response data to firewall_requests_log table.
112
     *
113
     * @param $response
114
     * @param $firewall_requests_log
115
     *
116
     * @return mixed
117
     */
118
    private function logResponseData($response, $firewall_requests_log)
119
    {
120
        $firewall_requests_log->response_data = $response;
121
        $firewall_requests_log->save();
122
123
        return $firewall_requests_log;
124
    }
125
}
126