Completed
Branch cleanup (04d52f)
by Paul
01:50
created

IdentificationRequest::fromArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
ccs 7
cts 7
cp 1
crap 1
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 2
    public static function fromArray($vars)
24
    {
25 2
        $id = new IdentificationRequest;
26 2
        $id->ip = $vars['REMOTE_ADDR'] ?? null;
27 2
        $id->referrer = $vars['HTTP_REFERER'] ?? null;
28 2
        $id->url = $vars['REQUEST_URI'] ?? null;
29 2
        $id->user_agent = $vars['HTTP_USER_AGENT'] ?? null;
30 2
        return $id;
31
    }
32
33 10
    public function getRequestJSON()
34
    {
35 10
        $json = [];
36 10
        $allowed = ['url', 'referrer', 'user_agent', 'ip'];
37 10
        foreach ($allowed as $name) {
38 10
            if (isset($this->$name) && !empty($this->$name)) {
39 10
                $json[$name] = $this->$name;
40
            }
41
        }
42 10
        return json_encode($json);
43
    }
44
}
45