Passed
Push — master ( 6b0e6b...43789e )
by Stephen
03:30
created

ParseTrafficAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 19
rs 10
1
<?php
2
3
namespace Sfneal\Tracking\Actions;
4
5
use Illuminate\Http\RedirectResponse;
6
use Illuminate\Http\Request;
7
use Illuminate\Http\Response;
8
use Illuminate\Support\Facades\Cookie;
9
use Jenssegers\Agent\Agent;
10
use Sfneal\Actions\AbstractAction;
11
12
class ParseTrafficAction extends AbstractAction
13
{
14
    /**
15
     * @var array
16
     */
17
    private $tracking = [];
18
19
    // todo: add to config
20
    /**
21
     * Array keys to exclude from the 'request_payload' attribute.
22
     *
23
     * @var array
24
     */
25
    private $request_payload_exclusions = [
26
        '_token',
27
        '_method',
28
        'password',
29
    ];
30
31
    /**
32
     * Create a new event instance.
33
     *
34
     * @param Request                   $request
35
     * @param Response|RedirectResponse $response
36
     * @param string                    $time_stamp
37
     */
38
    public function __construct(
39
        Request $request,
40
        $response,
41
        string $time_stamp
42
    ) {
43
        // Initialize event for serialization
44
        $this->tracking['user_id'] = intval(activeUserID());
0 ignored issues
show
Bug introduced by
The function activeUserID 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

44
        $this->tracking['user_id'] = intval(/** @scrutinizer ignore-call */ activeUserID());
Loading history...
45
        $this->tracking['session_id'] = Cookie::get('hpa_laravel_session');
46
        $this->tracking['app_version'] = version();
0 ignored issues
show
Bug introduced by
The function version 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

46
        $this->tracking['app_version'] = /** @scrutinizer ignore-call */ version();
Loading history...
47
        $this->tracking['time_stamp'] = $this->getTimestamp($time_stamp);
48
49
        // Request data
50
        $this->parseRequest($request);
51
52
        // Response data
53
        $this->parseResponse($response, $time_stamp);
54
55
        // Request data
56
        $this->parseAgent();
57
    }
58
59
    /**
60
     * Execute the action.
61
     *
62
     * @return array
63
     */
64
    public function execute()
65
    {
66
        return $this->tracking;
67
    }
68
69
    /**
70
     * Parse a Request object to retrieve relevant data.
71
     *
72
     * @param Request $request
73
     */
74
    private function parseRequest(Request $request)
75
    {
76
        $this->tracking['request']['host'] = $request->getHttpHost();
77
        $this->tracking['request']['uri'] = $request->getRequestUri();
78
        $this->tracking['request']['method'] = strtoupper($request->getMethod());
79
        $this->tracking['request']['payload'] = $this->getRequestPayload($request);
80
        $this->tracking['request']['browser'] = $_SERVER['HTTP_USER_AGENT'];
81
        $this->tracking['request']['ip'] = $request->ip();
82
        $this->tracking['request']['referrer'] = (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null);
83
        $this->tracking['request']['token'] = $request->get('track_traffic_token');
84
    }
85
86
    /**
87
     * Parse a Response object to retrieve relevant data.
88
     *
89
     * @param Response|RedirectResponse $response
90
     * @param $time_stamp
91
     */
92
    private function parseResponse($response, $time_stamp)
93
    {
94
        $this->tracking['response']['code'] = $response->getStatusCode();
95
        $this->tracking['response']['time'] = $this->getResponseTime($time_stamp);
96
97
        // Store response content served if enabled
98
        if (env('TRACK_TRAFFIC_RESPONSE_CONTENT', false) == true) {
99
            $this->tracking['response']['content'] = $response->getContent();
100
        }
101
    }
102
103
    private function parseAgent()
104
    {
105
        $agent = new Agent();
106
107
        $this->tracking['agent']['platform'] = $agent->platform();
108
        $this->tracking['agent']['device'] = $agent->device();
109
        $this->tracking['agent']['browser'] = $agent->browser();
110
    }
111
112
    /**
113
     * @param Request $request
114
     *
115
     * @return array
116
     */
117
    private function getRequestPayload(Request $request)
118
    {
119
        return arrayRemoveKeys(array_merge($request->query(), $request->input()), $this->request_payload_exclusions);
0 ignored issues
show
Bug introduced by
It seems like $request->query() can also be of type null and string; however, parameter $array1 of array_merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

119
        return arrayRemoveKeys(array_merge(/** @scrutinizer ignore-type */ $request->query(), $request->input()), $this->request_payload_exclusions);
Loading history...
120
    }
121
122
    /**
123
     * Determine the amount of time taken to return a response.
124
     *
125
     * @param $time_stamp
126
     *
127
     * @return float
128
     */
129
    private function getResponseTime($time_stamp)
130
    {
131
        return number_format($time_stamp - LARAVEL_START, 2);
0 ignored issues
show
Bug introduced by
The constant Sfneal\Tracking\Actions\LARAVEL_START was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
132
    }
133
134
    /**
135
     * @param $time_stamp
136
     *
137
     * @return string
138
     */
139
    private function getTimestamp($time_stamp)
140
    {
141
        return date('Y-m-d H:i:s', $time_stamp);
142
    }
143
}
144