IdentificationRequest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fromArray() 0 9 1
A getRequestJSON() 0 11 4
1
<?php
2
3
namespace LibLynx\Connect;
4
5
/**
6
 * Provides a simple way to construct a LibLynx identification request
7
 * @package LibLynx\Connect
8
 */
9
class IdentificationRequest
10
{
11
    public $url;
12
    public $referrer;
13
    public $ip;
14
    public $user_agent;
15
16
    /**
17
     * This can be used to rapidly construct a request from superglobals simply by calling
18
     *
19
     * $req = class IdentificationRequest::fromArray($_SERVER);
20
     * @param $vars
21
     * @return IdentificationRequest
22
     */
23
    public static function fromArray($vars)
24
    {
25
        $id = new IdentificationRequest;
26
        $id->ip = $vars['REMOTE_ADDR'] ?? null;
27
        $id->referrer = $vars['HTTP_REFERER'] ?? null;
28
        $id->url = $vars['REQUEST_URI'] ?? null;
29
        $id->user_agent = $vars['HTTP_USER_AGENT'] ?? null;
30
        return $id;
31
    }
32
33
    public function getRequestJSON()
34
    {
35
        $json = [];
36
        $allowed = ['url', 'referrer', 'user_agent', 'ip'];
37
        foreach ($allowed as $name) {
38
            if (isset($this->$name) && !empty($this->$name)) {
39
                $json[$name] = $this->$name;
40
            }
41
        }
42
        return json_encode($json);
43
    }
44
}
45