Completed
Push — master ( 5e45ac...a1e047 )
by John
04:04
created

Contact::setDeviceId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 10
ccs 2
cts 2
cp 1
cc 1
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
     * Set Mautic device ID to global cookie
109
     *
110
     * @param string $deviceId
111
     *
112 38
     * @return Contact
113
     */
114 38
    public function setDeviceId($deviceId)
115
    {
116 38
        $this->cookie->setDeviceId($deviceId);
117
118
        return $this;
119
    }
120
121
    /**
122
     * Guesses IP address from $_SERVER
123
     *
124
     * @return string
125 38
     */
126 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...
127 4
    {
128 4
        $ip = '';
129
        $ipHolders = [
130
            'HTTP_CLIENT_IP',
131
            'HTTP_X_FORWARDED_FOR',
132
            'HTTP_X_FORWARDED',
133
            'HTTP_X_CLUSTER_CLIENT_IP',
134
            'HTTP_FORWARDED_FOR',
135 4
            'HTTP_FORWARDED',
136 38
            'REMOTE_ADDR'
137
        ];
138
139
        foreach ($ipHolders as $key) {
140 38
            if (!empty($_SERVER[$key])) {
141
                $ip = $_SERVER[$key];
142
                if (strpos($ip, ',') !== false) {
143
                    // Multiple IPs are present so use the last IP which should be
144
                    // the most reliable IP that last connected to the proxy
145
                    $ips = explode(',', $ip);
146
                    $ips = array_map('trim', $ips);
147
                    $ip = end($ips);
148
                }
149
                $ip = trim($ip);
150
                break;
151
            }
152
        }
153
154
        return $ip;
155
    }
156
}
157