1 | <?php |
||
2 | |||
3 | /* |
||
4 | * @copyright 2018 Mautic Contributors. All rights reserved |
||
5 | * @author Mautic, Inc. |
||
6 | * |
||
7 | * @link https://mautic.org |
||
8 | * |
||
9 | * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html |
||
10 | */ |
||
11 | |||
12 | namespace Mautic\LeadBundle\Model; |
||
13 | |||
14 | use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException; |
||
15 | use Doctrine\DBAL\Exception\UniqueConstraintViolationException; |
||
16 | use Doctrine\ORM\EntityManager; |
||
17 | use Mautic\CoreBundle\Entity\IpAddress; |
||
18 | use Mautic\LeadBundle\Entity\Lead; |
||
19 | use Psr\Log\LoggerInterface; |
||
20 | |||
21 | class IpAddressModel |
||
22 | { |
||
23 | /** |
||
24 | * @var LoggerInterface |
||
25 | */ |
||
26 | protected $logger; |
||
27 | |||
28 | /** |
||
29 | * @var EntityManager |
||
30 | */ |
||
31 | protected $entityManager; |
||
32 | |||
33 | public function __construct(EntityManager $entityManager, LoggerInterface $logger) |
||
34 | { |
||
35 | $this->entityManager = $entityManager; |
||
36 | $this->logger = $logger; |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * Saving IP Address references sometimes throws UniqueConstraintViolationException exception on Lead entity save. |
||
41 | * Rather pre-save the IP references here and catch the exception. |
||
42 | */ |
||
43 | public function saveIpAddressesReferencesForContact(Lead $contact) |
||
44 | { |
||
45 | foreach ($contact->getIpAddresses() as $ipAddress) { |
||
46 | $this->insertIpAddressReference($contact, $ipAddress); |
||
47 | } |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * @param string $ip |
||
52 | * |
||
53 | * @return IpAddress|null |
||
54 | */ |
||
55 | public function findOneByIpAddress($ip) |
||
56 | { |
||
57 | return $this->entityManager->getRepository(IpAddress::class)->findOneByIpAddress($ip); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Tries to insert the Lead/IP relation and continues even if UniqueConstraintViolationException is thrown. |
||
62 | */ |
||
63 | private function insertIpAddressReference(Lead $contact, IpAddress $ipAddress) |
||
64 | { |
||
65 | $ipAddressAdded = isset($contact->getChanges()['ipAddressList'][$ipAddress->getIpAddress()]); |
||
66 | if (!$ipAddressAdded || !$ipAddress->getId() || !$contact->getId()) { |
||
67 | return; |
||
68 | } |
||
69 | |||
70 | $qb = $this->entityManager->getConnection()->createQueryBuilder(); |
||
71 | $values = [ |
||
72 | 'lead_id' => ':leadId', |
||
73 | 'ip_id' => ':ipId', |
||
74 | ]; |
||
75 | |||
76 | $qb->insert(MAUTIC_TABLE_PREFIX.'lead_ips_xref'); |
||
77 | $qb->values($values); |
||
78 | $qb->setParameter('leadId', $contact->getId()); |
||
79 | $qb->setParameter('ipId', $ipAddress->getId()); |
||
80 | |||
81 | try { |
||
82 | $qb->execute(); |
||
83 | } catch (UniqueConstraintViolationException $e) { |
||
84 | $this->logger->warning("The reference for contact {$contact->getId()} and IP address {$ipAddress->getId()} is already there. (Unique constraint)"); |
||
85 | } catch (ForeignKeyConstraintViolationException $e) { |
||
86 | $this->logger->warning("The reference for contact {$contact->getId()} and IP address {$ipAddress->getId()} is already there. (Foreign key constraint)"); |
||
87 | } |
||
88 | |||
89 | $this->entityManager->detach($ipAddress); |
||
0 ignored issues
–
show
|
|||
90 | } |
||
91 | } |
||
92 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.