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 | * 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) |
|
43 | |||
44 | /** |
||
45 | * Returns Contact ID |
||
46 | * |
||
47 | * @return int |
||
48 | */ |
||
49 | 16 | public function getId() |
|
53 | |||
54 | /** |
||
55 | * Returns Contact IP address |
||
56 | * |
||
57 | * @return string |
||
58 | */ |
||
59 | 14 | public function getIp() |
|
63 | |||
64 | /** |
||
65 | * Gets Contact ID from $_COOKIE |
||
66 | * |
||
67 | * @return int|null |
||
68 | */ |
||
69 | 32 | public function getIdFromCookie() |
|
82 | |||
83 | /** |
||
84 | * Returns Mautic session ID if it exists in the cookie |
||
85 | * |
||
86 | * @return string|null |
||
87 | */ |
||
88 | 4 | public function getMauticSessionIdFromCookie() |
|
100 | |||
101 | /** |
||
102 | * Guesses IP address from $_SERVER |
||
103 | * |
||
104 | * @return string |
||
105 | */ |
||
106 | 32 | public function getIpFromServer() |
|
136 | } |
||
137 |
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: