Completed
Push — master ( fc6a36...784ac4 )
by John
01:48
created

Contact::getMauticSessionIdFromCookie()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2
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
     * Constructor
26
     *
27
     * @param int    $id will be taken from $_COOKIE if not provided
28
     * @param string $ip will be taken from $_SERVER if not provided
29
     */
30 34
    public function __construct($id = null, $ip = null)
31
    {
32 34
        if ($id === null) {
33 32
            $id = $this->getIdFromCookie();
34 16
        }
35
36 34
        if ($ip === null) {
37 32
            $ip = $this->getIpFromServer();
38 16
        }
39
40 34
        $this->id = (int) $id;
41 34
        $this->ip = $ip;
42 34
    }
43
44
    /**
45
     * Returns Contact ID
46
     *
47
     * @return int
48
     */
49 16
    public function getId()
50
    {
51 16
        return $this->id;
52
    }
53
54
    /**
55
     * Returns Contact IP address
56
     *
57
     * @return string
58
     */
59 14
    public function getIp()
60
    {
61 14
        return $this->ip;
62
    }
63
64
    /**
65
     * Gets Contact ID from $_COOKIE
66
     *
67
     * @return int|null
68
     */
69 32
    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...
70
    {
71 32
        if (isset($_COOKIE['mtc_id'])) {
72 4
            return (int) $_COOKIE['mtc_id'];
73 30
        } elseif (isset($_COOKIE['mautic_session_id'])) {
74 18
            $mauticSessionId = $_COOKIE['mautic_session_id'];
75 18
            if (isset($_COOKIE[$mauticSessionId])) {
76 2
                return (int) $_COOKIE[$mauticSessionId];
77
            }
78 8
        }
79
80 28
        return null;
81
    }
82
83
    /**
84
     * Returns Mautic session ID if it exists in the cookie
85
     *
86
     * @return string|null
87
     */
88 2
    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...
89
    {
90 2
        if (isset($_COOKIE['mautic_session_id'])) {
91 2
            return $_COOKIE['mautic_session_id'];
92
        }
93
94 2
        return null;
95
    }
96
97
    /**
98
     * Guesses IP address from $_SERVER
99
     *
100
     * @return string
101
     */
102 32
    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...
103
    {
104 32
        $ip = '';
105
        $ipHolders = [
106 32
            'HTTP_CLIENT_IP',
107 16
            'HTTP_X_FORWARDED_FOR',
108 16
            'HTTP_X_FORWARDED',
109 16
            'HTTP_X_CLUSTER_CLIENT_IP',
110 16
            'HTTP_FORWARDED_FOR',
111 16
            'HTTP_FORWARDED',
112
            'REMOTE_ADDR'
113 16
        ];
114
115 32
        foreach ($ipHolders as $key) {
116 32
            if (!empty($_SERVER[$key])) {
117 4
                $ip = $_SERVER[$key];
118 4
                if (strpos($ip, ',') !== false) {
119
                    // Multiple IPs are present so use the last IP which should be
120
                    // the most reliable IP that last connected to the proxy
121
                    $ips = explode(',', $ip);
122
                    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...
123
                    $ip = end($ips);
124
                }
125 4
                $ip = trim($ip);
126 18
                break;
127
            }
128 16
        }
129
130 32
        return $ip;
131
    }
132
}
133