1 | <?php |
||
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) |
|
56 | |||
57 | /** |
||
58 | * Returns Contact ID |
||
59 | * |
||
60 | * @return int |
||
61 | */ |
||
62 | 18 | public function getId() |
|
66 | |||
67 | /** |
||
68 | * Returns Contact IP address |
||
69 | * |
||
70 | * @return string|null |
||
71 | */ |
||
72 | 14 | public function getIp() |
|
76 | |||
77 | /** |
||
78 | * Returns Mautic Contact Session DI |
||
79 | * |
||
80 | * @return string|null |
||
81 | */ |
||
82 | 2 | public function getSessionId() |
|
86 | |||
87 | /** |
||
88 | * Gets Contact ID from $_COOKIE |
||
89 | * |
||
90 | * @return int|null |
||
91 | */ |
||
92 | 36 | public function getIdFromCookie() |
|
105 | |||
106 | /** |
||
107 | * Returns Mautic session ID if it exists in the cookie |
||
108 | * |
||
109 | * @return string|null |
||
110 | */ |
||
111 | 38 | public function getMauticSessionIdFromCookie() |
|
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) |
|
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) |
|
159 | |||
160 | /** |
||
161 | * Guesses IP address from $_SERVER |
||
162 | * |
||
163 | * @return string |
||
164 | */ |
||
165 | 36 | public function getIpFromServer() |
|
195 | } |
||
196 |
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: