Total Complexity | 54 |
Total Lines | 385 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like ContactTracker often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ContactTracker, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class ContactTracker |
||
32 | { |
||
33 | use DefaultValueTrait; |
||
34 | /** |
||
35 | * @var LeadRepository |
||
36 | */ |
||
37 | private $leadRepository; |
||
38 | |||
39 | /** |
||
40 | * @var ContactTrackingServiceInterface |
||
41 | */ |
||
42 | private $contactTrackingService; |
||
43 | |||
44 | /** |
||
45 | * @var DeviceTracker |
||
46 | */ |
||
47 | private $deviceTracker; |
||
48 | |||
49 | /** |
||
50 | * @var CorePermissions |
||
51 | */ |
||
52 | private $security; |
||
53 | |||
54 | /** |
||
55 | * @var null|Lead |
||
56 | */ |
||
57 | private $systemContact; |
||
58 | |||
59 | /** |
||
60 | * @var null|Lead |
||
61 | */ |
||
62 | private $trackedContact; |
||
63 | |||
64 | /** |
||
65 | * @var Logger |
||
66 | */ |
||
67 | private $logger; |
||
68 | |||
69 | /** |
||
70 | * @var IpLookupHelper |
||
71 | */ |
||
72 | private $ipLookupHelper; |
||
73 | |||
74 | /** |
||
75 | * @var Request |
||
76 | */ |
||
77 | private $request; |
||
78 | |||
79 | /** |
||
80 | * @var CoreParametersHelper |
||
81 | */ |
||
82 | private $coreParametersHelper; |
||
83 | |||
84 | /** |
||
85 | * @var EventDispatcherInterface |
||
86 | */ |
||
87 | private $dispatcher; |
||
88 | |||
89 | /** |
||
90 | * @var FieldModel |
||
91 | */ |
||
92 | private $leadFieldModel; |
||
93 | |||
94 | /** |
||
95 | * ContactTracker constructor. |
||
96 | * |
||
97 | * @param LeadRepository $leadRepository |
||
98 | * @param ContactTrackingServiceInterface $contactTrackingService |
||
99 | * @param DeviceTracker $deviceTracker |
||
100 | * @param CorePermissions $security |
||
101 | * @param Logger $logger |
||
102 | * @param IpLookupHelper $ipLookupHelper |
||
103 | * @param RequestStack $requestStack |
||
104 | * @param CoreParametersHelper $coreParametersHelper |
||
105 | * @param EventDispatcherInterface $dispatcher |
||
106 | * @param FieldModel $leadFieldModel |
||
107 | */ |
||
108 | public function __construct( |
||
109 | LeadRepository $leadRepository, |
||
110 | ContactTrackingServiceInterface $contactTrackingService, |
||
111 | DeviceTracker $deviceTracker, |
||
112 | CorePermissions $security, |
||
113 | Logger $logger, |
||
114 | IpLookupHelper $ipLookupHelper, |
||
115 | RequestStack $requestStack, |
||
116 | CoreParametersHelper $coreParametersHelper, |
||
117 | EventDispatcherInterface $dispatcher, |
||
118 | FieldModel $leadFieldModel |
||
119 | ) { |
||
120 | $this->leadRepository = $leadRepository; |
||
121 | $this->contactTrackingService = $contactTrackingService; |
||
122 | $this->deviceTracker = $deviceTracker; |
||
123 | $this->security = $security; |
||
124 | $this->logger = $logger; |
||
125 | $this->ipLookupHelper = $ipLookupHelper; |
||
126 | $this->request = $requestStack->getCurrentRequest(); |
||
127 | $this->coreParametersHelper = $coreParametersHelper; |
||
128 | $this->dispatcher = $dispatcher; |
||
129 | $this->leadFieldModel = $leadFieldModel; |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @return Lead|null |
||
134 | */ |
||
135 | public function getContact() |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * Set the contact and generate cookies for future tracking. |
||
163 | * |
||
164 | * @param Lead $lead |
||
165 | */ |
||
166 | public function setTrackedContact(Lead $trackedContact) |
||
206 | } |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * System contact bypasses cookie tracking. |
||
211 | * |
||
212 | * @param Lead|null $lead |
||
213 | */ |
||
214 | public function setSystemContact(Lead $lead = null) |
||
215 | { |
||
216 | if (null !== $lead) { |
||
217 | $this->logger->addDebug("LEAD: {$lead->getId()} set as system lead."); |
||
218 | |||
219 | $fields = $lead->getFields(); |
||
220 | if (empty($fields)) { |
||
221 | $this->hydrateCustomFieldData($lead); |
||
222 | } |
||
223 | } |
||
224 | |||
225 | $this->systemContact = $lead; |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * @return null|string |
||
230 | */ |
||
231 | public function getTrackingId() |
||
232 | { |
||
233 | // Use the new method first |
||
234 | if ($trackedDevice = $this->deviceTracker->getTrackedDevice()) { |
||
235 | return $trackedDevice->getTrackingId(); |
||
236 | } |
||
237 | |||
238 | // That failed, so look for the old cookies |
||
239 | return $this->contactTrackingService->getTrackedIdentifier(); |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * @return Lead|null |
||
244 | */ |
||
245 | private function getSystemContact() |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * @return Lead|null |
||
262 | */ |
||
263 | private function getCurrentContact() |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * @return Lead|null |
||
274 | */ |
||
275 | private function getContactByTrackedDevice() |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * @return Lead |
||
307 | */ |
||
308 | private function getContactByIpAddress() |
||
309 | { |
||
310 | $ip = $this->ipLookupHelper->getIpAddress(); |
||
311 | // if no trackingId cookie set the lead is not tracked yet so create a new one |
||
312 | if ($ip && !$ip->isTrackable()) { |
||
313 | // Don't save leads that are from a non-trackable IP by default |
||
314 | return $this->createNewContact($ip, false); |
||
315 | } |
||
316 | |||
317 | if ($this->coreParametersHelper->getParameter('track_contact_by_ip') && $this->coreParametersHelper->getParameter('anonymize_ip')) { |
||
318 | /** @var Lead[] $leads */ |
||
319 | $leads = $this->leadRepository->getLeadsByIp($ip->getIpAddress()); |
||
320 | if (count($leads)) { |
||
321 | $lead = $leads[0]; |
||
322 | $this->logger->addDebug("CONTACT: Existing lead found with ID# {$lead->getId()}."); |
||
323 | |||
324 | return $lead; |
||
325 | } |
||
326 | } |
||
327 | |||
328 | return $this->createNewContact($ip); |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * @param IpAddress|null $ip |
||
333 | * @param bool $persist |
||
334 | * |
||
335 | * @return Lead |
||
336 | */ |
||
337 | private function createNewContact(IpAddress $ip = null, $persist = true) |
||
338 | { |
||
339 | //let's create a lead |
||
340 | $lead = new Lead(); |
||
341 | $lead->setNewlyCreated(true); |
||
342 | |||
343 | if ($ip) { |
||
344 | $lead->addIpAddress($ip); |
||
345 | } |
||
346 | |||
347 | if ($persist && !defined('MAUTIC_NON_TRACKABLE_REQUEST')) { |
||
348 | // Dispatch events for new lead to write create log, ip address change, etc |
||
349 | $event = new LeadEvent($lead, true); |
||
350 | $this->dispatcher->dispatch(LeadEvents::LEAD_PRE_SAVE, $event); |
||
351 | $this->setEntityDefaultValues($lead); |
||
352 | $this->leadRepository->saveEntity($lead); |
||
353 | $this->hydrateCustomFieldData($lead); |
||
354 | |||
355 | $this->dispatcher->dispatch(LeadEvents::LEAD_POST_SAVE, $event); |
||
356 | |||
357 | $this->logger->addDebug("CONTACT: New lead created with ID# {$lead->getId()}."); |
||
358 | } |
||
359 | |||
360 | return $lead; |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * @param Lead $lead |
||
365 | */ |
||
366 | private function hydrateCustomFieldData(Lead $lead = null) |
||
367 | { |
||
368 | if (null === $lead) { |
||
369 | return; |
||
370 | } |
||
371 | |||
372 | // Hydrate fields with custom field data |
||
373 | $fields = $this->leadRepository->getFieldValues($lead->getId()); |
||
374 | $lead->setFields($fields); |
||
375 | } |
||
376 | |||
377 | /** |
||
378 | * @return bool |
||
379 | */ |
||
380 | private function useSystemContact() |
||
381 | { |
||
382 | return $this->isUserSession() || $this->systemContact || defined('IN_MAUTIC_CONSOLE') || $this->request === null; |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * @return bool |
||
387 | */ |
||
388 | private function isUserSession() |
||
391 | } |
||
392 | |||
393 | /** |
||
394 | * @param Lead $previouslyTrackedContact |
||
395 | * @param $previouslyTrackedId |
||
396 | */ |
||
397 | private function dispatchContactChangeEvent(Lead $previouslyTrackedContact, $previouslyTrackedId) |
||
408 | } |
||
409 | } |
||
410 | } |
||
411 | |||
412 | private function generateTrackingCookies() |
||
416 | } |
||
417 | } |
||
418 | } |
||
419 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.