Completed
Push — master ( 861886...cceff3 )
by John
10:56
created

Contact::setSessionId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Escopecz\MauticFormSubmit\Mautic;
4
5
use Escopecz\MauticFormSubmit\Mautic\Cookie;
6
7
/**
8
 * Mautic Contact
9
 */
10
class Contact
11
{
12
    /**
13
     * Mautic contact IP address
14
     *
15
     * @var string
16
     */
17
    protected $ip;
18
19
    /**
20
     * Mautic Cookie
21
     *
22
     * @var Cookie
23
     */
24
    protected $cookie;
25
26
    /**
27
     * Constructor
28
     *
29
     * @param Cookie $cookie
30
     */
31 38
    public function __construct(Cookie $cookie)
32
    {
33 38
        $this->cookie = $cookie;
34 38
        $this->ip = $this->getIpFromServer();
35 38
    }
36
37
    /**
38
     * Returns Contact ID
39
     *
40
     * @return int
41
     */
42 16
    public function getId()
43
    {
44 16
        return (int) $this->cookie->getContactId();
45
    }
46
47
    /**
48
     * Set Mautic Contact ID to global cookie
49
     *
50
     * @param int $contactId
51
     *
52
     * @return Contact
53
     */
54 6
    public function setId($contactId)
55
    {
56 6
        $this->cookie->setContactId($contactId);
57
58 6
        return $this;
59
    }
60
61
    /**
62
     * Returns Contact IP address
63
     *
64
     * @return string|null
65
     */
66 12
    public function getIp()
67
    {
68 12
        return $this->ip;
69
    }
70
71
    /**
72
     * Sert Contact IP address
73
     *
74
     * @param string $ip
75
     *
76
     * @return Contact
77
     */
78 4
    public function setIp($ip)
79
    {
80 4
        $this->ip = $ip;
81 4
    }
82
83
    /**
84
     * Returns Mautic Contact Session ID
85
     *
86
     * @return string|null
87
     */
88 4
    public function getSessionId()
89
    {
90 4
        return $this->cookie->getSessionId();
91
    }
92
93
    /**
94
     * Set Mautic session ID to global cookie
95
     *
96
     * @param string $sessionId
97
     *
98
     * @return Contact
99
     */
100 2
    public function setSessionId($sessionId)
101
    {
102 2
        $this->cookie->setSessionId($sessionId);
103
104 2
        return $this;
105
    }
106
107
    /**
108
     * Guesses IP address from $_SERVER
109
     *
110
     * @return string
111
     */
112 38
    public function getIpFromServer()
0 ignored issues
show
Coding Style introduced by
getIpFromServer uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
113
    {
114 38
        $ip = '';
115
        $ipHolders = [
116 38
            'HTTP_CLIENT_IP',
117
            'HTTP_X_FORWARDED_FOR',
118
            'HTTP_X_FORWARDED',
119
            'HTTP_X_CLUSTER_CLIENT_IP',
120
            'HTTP_FORWARDED_FOR',
121
            'HTTP_FORWARDED',
122
            'REMOTE_ADDR'
123
        ];
124
125 38
        foreach ($ipHolders as $key) {
126 38
            if (!empty($_SERVER[$key])) {
127 4
                $ip = $_SERVER[$key];
128 4
                if (strpos($ip, ',') !== false) {
129
                    // Multiple IPs are present so use the last IP which should be
130
                    // the most reliable IP that last connected to the proxy
131
                    $ips = explode(',', $ip);
132
                    array_walk($ips, create_function('&$val', '$val = trim($val);'));
0 ignored issues
show
Security Best Practice introduced by
The use of create_function is highly discouraged, better use a closure.

create_function can pose a great security vulnerability as it is similar to eval, and could be used for arbitrary code execution. We highly recommend to use a closure instead.

// Instead of
$function = create_function('$a, $b', 'return $a + $b');

// Better use
$function = function($a, $b) { return $a + $b; }
Loading history...
133
                    $ip = end($ips);
134
                }
135 4
                $ip = trim($ip);
136 38
                break;
137
            }
138
        }
139
140 38
        return $ip;
141
    }
142
}
143