Completed
Branch cleanup (6c017e)
by Paul
13:14
created

IdentificationRequest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B fromArray() 0 17 5
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
        if (isset($vars['REMOTE_ADDR'])) {
27
            $id->ip = $vars['REMOTE_ADDR'];
28
        }
29
        if (isset($vars['HTTP_REFERER'])) {
30
            $id->referrer = $vars['HTTP_REFERER'];
31
        }
32
        if (isset($vars['REQUEST_URI'])) {
33
            $id->url = $vars['REQUEST_URI'];
34
        }
35
        if (isset($vars['HTTP_USER_AGENT'])) {
36
            $id->user_agent = $vars['HTTP_USER_AGENT'];
37
        }
38
        return $id;
39
    }
40
41
    public function getRequestJSON()
42
    {
43
        $json = [];
44
        $allowed = ['url', 'referrer', 'user_agent', 'ip'];
45
        foreach ($allowed as $name) {
46
            if (isset($this->$name) && !empty($this->$name)) {
47
                $json[$name] = $this->$name;
48
            }
49
        }
50
        return json_encode($json);
51
    }
52
}
53