Completed
Push — master ( dbc548...138c0b )
by John
01:54
created

Contact::getSessionId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Escopecz\MauticFormSubmit\Mautic;
4
5
/**
6
 * Mautic Contact
7
 */
8
class Contact
9
{
10
    /**
11
     * Mautic contact ID
12
     *
13
     * @var int
14
     */
15
    protected $id;
16
17
    /**
18
     * Mautic contact IP address
19
     *
20
     * @var string
21
     */
22
    protected $ip;
23
24
    /**
25
     * Mautic Session ID
26
     *
27
     * @var string
28
     */
29
    protected $sessionId;
30
31
    /**
32
     * Constructor
33
     *
34
     * @param int    $id will be taken from $_COOKIE if not provided
35
     * @param string $ip will be taken from $_SERVER if not provided
36
     * @param string $sessionId will be taken from $_COOKIE if not provided
37
     */
38 38
    public function __construct($id = null, $ip = null, $sessionId = null)
39
    {
40 38
        if ($id === null) {
41 36
            $id = $this->getIdFromCookie();
42 18
        }
43
44 38
        if ($ip === null) {
45 36
            $ip = $this->getIpFromServer();
46 18
        }
47
48 38
        if ($sessionId === null) {
49 38
            $sessionId = $this->getMauticSessionIdFromCookie();
50 19
        }
51
52 38
        $this->id = (int) $id;
53 38
        $this->ip = $ip;
54 38
        $this->sessionId = $sessionId;
55 38
    }
56
57
    /**
58
     * Returns Contact ID
59
     *
60
     * @return int
61
     */
62 18
    public function getId()
63
    {
64 18
        return $this->id;
65
    }
66
67
    /**
68
     * Returns Contact IP address
69
     *
70
     * @return string|null
71
     */
72 14
    public function getIp()
73
    {
74 14
        return $this->ip;
75
    }
76
77
    /**
78
     * Returns Mautic Contact Session DI
79
     *
80
     * @return string|null
81
     */
82 2
    public function getSessionId()
83
    {
84 2
        return $this->sessionId;
85
    }
86
87
    /**
88
     * Gets Contact ID from $_COOKIE
89
     *
90
     * @return int|null
91
     */
92 36
    public function getIdFromCookie()
0 ignored issues
show
Coding Style introduced by
getIdFromCookie uses the super-global variable $_COOKIE 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...
93
    {
94 36
        if (isset($_COOKIE['mtc_id'])) {
95 6
            return (int) $_COOKIE['mtc_id'];
96 34
        } elseif (isset($_COOKIE['mautic_session_id'])) {
97 2
            $mauticSessionId = $_COOKIE['mautic_session_id'];
98 2
            if (isset($_COOKIE[$mauticSessionId])) {
99 2
                return (int) $_COOKIE[$mauticSessionId];
100
            }
101
        }
102
103 32
        return null;
104
    }
105
106
    /**
107
     * Returns Mautic session ID if it exists in the cookie
108
     *
109
     * @return string|null
110
     */
111 38
    public function getMauticSessionIdFromCookie()
0 ignored issues
show
Coding Style introduced by
getMauticSessionIdFromCookie uses the super-global variable $_COOKIE 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...
112
    {
113 38
        if (isset($_COOKIE['mautic_session_id'])) {
114 6
            return $_COOKIE['mautic_session_id'];
115
        }
116
117 36
        if (isset($_COOKIE['mtc_sid'])) {
118 2
            return $_COOKIE['mtc_sid'];
119
        }
120
121 36
        return null;
122
    }
123
124
    /**
125
     * Set Mautic session ID to global cookie
126
     *
127
     * @param string $sessionId
128
     *
129
     * @return Contact
130
     */
131 2
    public function setSessionIdCookie($sessionId)
0 ignored issues
show
Coding Style introduced by
setSessionIdCookie uses the super-global variable $_COOKIE 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...
132
    {
133 2
        $this->sessionId = $sessionId;
134 2
        $_COOKIE['mautic_session_id'] = $sessionId;
135 2
        $_COOKIE['mtc_sid'] = $sessionId;
136
137 2
        return $this;
138
    }
139
140
    /**
141
     * Set Mautic Contact ID to global cookie
142
     *
143
     * @param string $contactId
144
     *
145
     * @return Contact
146
     */
147 2
    public function setIdCookie($contactId)
0 ignored issues
show
Coding Style introduced by
setIdCookie uses the super-global variable $_COOKIE 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...
148
    {
149 2
        $this->id = $contactId;
0 ignored issues
show
Documentation Bug introduced by
The property $id was declared of type integer, but $contactId is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
150
151 2
        $_COOKIE['mtc_id'] = $contactId;
152
153 2
        if ($this->sessionId) {
154
            $_COOKIE[$this->sessionId] = $contactId;
155
        }
156
157 2
        return $this;
158
    }
159
160
    /**
161
     * Guesses IP address from $_SERVER
162
     *
163
     * @return string
164
     */
165 36
    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...
166
    {
167 36
        $ip = '';
168
        $ipHolders = [
169 36
            'HTTP_CLIENT_IP',
170 18
            'HTTP_X_FORWARDED_FOR',
171 18
            'HTTP_X_FORWARDED',
172 18
            'HTTP_X_CLUSTER_CLIENT_IP',
173 18
            'HTTP_FORWARDED_FOR',
174 18
            'HTTP_FORWARDED',
175
            'REMOTE_ADDR'
176 18
        ];
177
178 36
        foreach ($ipHolders as $key) {
179 36
            if (!empty($_SERVER[$key])) {
180 4
                $ip = $_SERVER[$key];
181 4
                if (strpos($ip, ',') !== false) {
182
                    // Multiple IPs are present so use the last IP which should be
183
                    // the most reliable IP that last connected to the proxy
184
                    $ips = explode(',', $ip);
185
                    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...
186
                    $ip = end($ips);
187
                }
188 4
                $ip = trim($ip);
189 20
                break;
190
            }
191 18
        }
192
193 36
        return $ip;
194
    }
195
}
196