1 | <?php |
||
2 | |||
3 | namespace MauticPlugin\MauticCrmBundle\Integration\Pipedrive\Export; |
||
4 | |||
5 | use Doctrine\ORM\EntityManager; |
||
6 | use Mautic\LeadBundle\Entity\CompanyLead; |
||
7 | use Mautic\LeadBundle\Entity\Lead; |
||
8 | use MauticPlugin\MauticCrmBundle\Entity\PipedriveOwner; |
||
9 | use MauticPlugin\MauticCrmBundle\Integration\Pipedrive\AbstractPipedrive; |
||
10 | use Symfony\Component\PropertyAccess\PropertyAccess; |
||
11 | |||
12 | class LeadExport extends AbstractPipedrive |
||
13 | { |
||
14 | /** |
||
15 | * @var CompanyExport |
||
16 | */ |
||
17 | private $companyExport; |
||
18 | |||
19 | /** |
||
20 | * LeadExport constructor. |
||
21 | */ |
||
22 | public function __construct(EntityManager $em, CompanyExport $companyExport) |
||
23 | { |
||
24 | $this->em = $em; |
||
25 | $this->companyExport = $companyExport; |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * @return bool |
||
30 | */ |
||
31 | public function create(Lead $lead) |
||
32 | { |
||
33 | // stop for anynomouse |
||
34 | if ($lead->isAnonymous() || empty($lead->getEmail())) { |
||
35 | return false; |
||
36 | } |
||
37 | |||
38 | $mappedData = $this->getMappedLeadData($lead); |
||
39 | $leadId = $lead->getId(); |
||
40 | |||
41 | /** @var IntegrationEntity $integrationEntity */ |
||
42 | $integrationEntity = $this->getLeadIntegrationEntity(['internalEntityId' => $leadId]); |
||
43 | $personData = $this->getIntegration()->getApiHelper()->findByEmail($lead->getEmail()); |
||
44 | // Pipedrive contact already exists, then create just integration entity |
||
45 | if (!$integrationEntity && !empty($personData)) { |
||
46 | $integrationEntityCreate = $this->createIntegrationLeadEntity(new \DateTime(), $personData[0]['id'], $leadId); |
||
47 | $integrationEntity = clone $integrationEntityCreate; |
||
48 | $this->em->persist($integrationEntityCreate); |
||
49 | $this->em->flush(); |
||
50 | } |
||
51 | |||
52 | // Integration entity exist and Pipedrive contact exist, then just update Pipedrive contact |
||
53 | if ($integrationEntity && !empty($personData)) { |
||
54 | return $this->update($lead); |
||
55 | } |
||
56 | |||
57 | try { |
||
58 | $createdLeadData = $this->getIntegration()->getApiHelper()->createLead($mappedData, $lead); |
||
59 | if (empty($createdLeadData['id'])) { |
||
60 | return false; |
||
61 | } |
||
62 | $integrationEntity = $this->createIntegrationLeadEntity(new \DateTime(), $createdLeadData['id'], $leadId); |
||
63 | |||
64 | $this->em->persist($integrationEntity); |
||
65 | $this->em->flush(); |
||
66 | |||
67 | return true; |
||
68 | } catch (\Exception $e) { |
||
69 | $this->getIntegration()->logIntegrationError($e); |
||
70 | } |
||
71 | |||
72 | return false; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @return bool |
||
77 | */ |
||
78 | public function update(Lead $lead) |
||
79 | { |
||
80 | $leadId = $lead->getId(); |
||
81 | $integrationEntity = $this->getLeadIntegrationEntity(['internalEntityId' => $leadId]); |
||
82 | if (!$integrationEntity) { |
||
83 | // create new contact |
||
84 | |||
85 | return $this->create($lead); |
||
86 | } |
||
87 | |||
88 | try { |
||
89 | $mappedData = $this->getMappedLeadData($lead); |
||
90 | $this->getIntegration()->getApiHelper()->updateLead($mappedData, $integrationEntity->getIntegrationEntityId()); |
||
91 | |||
92 | $integrationEntity->setLastSyncDate(new \DateTime()); |
||
93 | |||
94 | $this->em->persist($integrationEntity); |
||
95 | $this->em->flush(); |
||
96 | |||
97 | return true; |
||
98 | } catch (\Exception $e) { |
||
99 | $this->getIntegration()->logIntegrationError($e); |
||
100 | } |
||
101 | |||
102 | return false; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * @return bool |
||
107 | */ |
||
108 | public function delete(Lead $lead) |
||
109 | { |
||
110 | $integrationEntity = $this->getLeadIntegrationEntity(['internalEntityId' => $lead->getId()]); |
||
111 | |||
112 | if (!$integrationEntity) { |
||
113 | return true; |
||
114 | } |
||
115 | |||
116 | try { |
||
117 | $this->getIntegration()->getApiHelper()->deleteLead($integrationEntity->getIntegrationEntityId()); |
||
118 | |||
119 | $this->em->remove($integrationEntity); |
||
120 | $this->em->flush(); |
||
121 | |||
122 | return true; |
||
123 | } catch (\Exception $e) { |
||
124 | $this->getIntegration()->logIntegrationError($e); |
||
125 | } |
||
126 | |||
127 | return false; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * @return mixed |
||
132 | */ |
||
133 | private function getMappedLeadData(Lead $lead) |
||
134 | { |
||
135 | $mappedData = []; |
||
136 | $leadFields = $this->getIntegration()->getIntegrationSettings()->getFeatureSettings()['leadFields']; |
||
137 | |||
138 | $accessor = PropertyAccess::createPropertyAccessor(); |
||
139 | |||
140 | foreach ($leadFields as $externalField => $internalField) { |
||
141 | if (in_array($externalField, self::NO_ALLOWED_FIELDS_TO_EXPORT)) { |
||
142 | continue; |
||
143 | } |
||
144 | $mappedData[$externalField] = $accessor->getValue($lead, $internalField); |
||
145 | } |
||
146 | |||
147 | if ($this->getIntegration()->isCompanySupportEnabled()) { |
||
148 | $mappedData['org_id'] = $this->getLeadIntegrationCompanyId($lead); |
||
149 | } |
||
150 | |||
151 | $mappedData['owner_id'] = $this->getLeadIntegrationOwnerId($lead); |
||
152 | |||
153 | return $this->convertMauticData($mappedData); |
||
154 | } |
||
155 | |||
156 | private function getLeadIntegrationCompanyId(Lead $lead) |
||
157 | { |
||
158 | $leadCompanies = $this->em->getRepository(CompanyLead::class)->findBy([ |
||
159 | 'lead' => $lead, |
||
160 | ]); |
||
161 | |||
162 | if (!$leadCompanies) { |
||
163 | return 0; |
||
164 | } |
||
165 | |||
166 | $leadCompany = array_pop($leadCompanies); |
||
167 | |||
168 | $integrationEntityCompany = $this->getCompanyIntegrationEntity(['internalEntityId' => $leadCompany->getCompany()->getId()]); |
||
169 | |||
170 | if (!$integrationEntityCompany) { |
||
171 | // check if company already exist on Pipedrive |
||
172 | $companyData = $this->getIntegration()->getApiHelper()->findCompanyByName($leadCompany->getCompany()->getName(), 0, 1); |
||
173 | if (!empty($companyData)) { |
||
174 | $integrationEntityCompany = $this->createIntegrationLeadEntity(new \DateTime(), $companyData[0]['id'], $leadCompany->getCompany()->getId()); |
||
175 | $this->em->persist($integrationEntityCompany); |
||
176 | $this->em->flush(); |
||
177 | } else { |
||
178 | // create new company on Pipedrive |
||
179 | $this->companyExport->setIntegration($this->getIntegration()); |
||
180 | if ($this->companyExport->pushCompany($leadCompany->getCompany())) { |
||
181 | $integrationEntityCompany = $this->getCompanyIntegrationEntity(['internalEntityId' => $leadCompany->getCompany()->getId()]); |
||
182 | } |
||
183 | } |
||
184 | } |
||
185 | |||
186 | if (!$integrationEntityCompany) { |
||
187 | return 0; |
||
188 | } |
||
189 | |||
190 | return $integrationEntityCompany->getIntegrationEntityId(); |
||
191 | } |
||
192 | |||
193 | private function getLeadIntegrationOwnerId(Lead $lead) |
||
194 | { |
||
195 | $mauticOwner = $lead->getOwner(); |
||
196 | |||
197 | if (!$mauticOwner) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
198 | return 0; |
||
199 | } |
||
200 | |||
201 | $pipedriveOwner = $this->em->getRepository(PipedriveOwner::class)->findOneByEmail($mauticOwner->getEmail()); |
||
202 | |||
203 | if (!$pipedriveOwner) { |
||
204 | return 0; |
||
205 | } |
||
206 | |||
207 | return $pipedriveOwner->getOwnerId(); |
||
208 | } |
||
209 | |||
210 | private function convertMauticData($data) |
||
211 | { |
||
212 | $data['name'] = $data['first_name'].' '.$data['last_name']; |
||
213 | |||
214 | return $data; |
||
215 | } |
||
216 | } |
||
217 |