Issues (3627)

Integration/Pipedrive/Import/LeadImport.php (1 issue)

1
<?php
2
3
namespace MauticPlugin\MauticCrmBundle\Integration\Pipedrive\Import;
4
5
use Doctrine\ORM\EntityManager;
6
use Mautic\LeadBundle\Entity\Company;
7
use Mautic\LeadBundle\Entity\Lead;
8
use Mautic\LeadBundle\Model\CompanyModel;
9
use Mautic\LeadBundle\Model\LeadModel;
10
use Symfony\Component\HttpFoundation\Response;
11
12
class LeadImport extends AbstractImport
13
{
14
    /**
15
     * @var LeadModel
16
     */
17
    private $leadModel;
18
19
    /**
20
     * @var CompanyModel
21
     */
22
    private $companyModel;
23
24
    /**
25
     * LeadImport constructor.
26
     */
27
    public function __construct(EntityManager $em, LeadModel $leadModel, CompanyModel $companyModel)
28
    {
29
        parent::__construct($em);
30
31
        $this->leadModel    = $leadModel;
32
        $this->companyModel = $companyModel;
33
    }
34
35
    /**
36
     * @return bool
37
     *
38
     * @throws \Doctrine\ORM\ORMException
39
     * @throws \Doctrine\ORM\OptimisticLockException
40
     * @throws \Exception
41
     */
42
    public function create(array $data = [])
43
    {
44
        $integrationEntity = $this->getLeadIntegrationEntity(['integrationEntityId' => $data['id']]);
45
46
        if ($integrationEntity) {
47
            throw new \Exception('Lead already have integration', Response::HTTP_CONFLICT);
48
        }
49
        $data         = $this->convertPipedriveData($data, $this->getIntegration()->getApiHelper()->getFields(self::PERSON_ENTITY_TYPE));
50
        $dataToUpdate = $this->getIntegration()->populateMauticLeadData($data);
51
52
        if (!$lead =  $this->leadModel->checkForDuplicateContact($dataToUpdate)) {
53
            $lead = new Lead();
54
        }
55
        // prevent listeners from exporting
56
        $lead->setEventData('pipedrive.webhook', 1);
57
58
        $this->leadModel->setFieldValues($lead, $dataToUpdate);
59
60
        if (isset($data['owner_id'])) {
61
            $this->addOwnerToLead($data['owner_id'], $lead);
62
        }
63
64
        $this->leadModel->saveEntity($lead);
65
66
        $integrationEntity = $this->getLeadIntegrationEntity(['integrationEntityId' => $data['id']]);
67
        if (!$integrationEntity) {
68
            $integrationEntity = $this->createIntegrationLeadEntity(new \DateTime(), $data['id'], $lead->getId());
69
        }
70
71
        $this->em->persist($integrationEntity);
72
        $this->em->flush();
73
74
        if (isset($data['org_id']) && $this->getIntegration()->isCompanySupportEnabled()) {
75
            $this->addLeadToCompany($data['org_id'], $lead);
76
            $this->em->flush();
77
        }
78
79
        return true;
80
    }
81
82
    /**
83
     * @return bool
84
     *
85
     * @throws \Doctrine\ORM\ORMException
86
     * @throws \Doctrine\ORM\OptimisticLockException
87
     * @throws \Exception
88
     */
89
    public function update(array $data = [])
90
    {
91
        $integrationEntity = $this->getLeadIntegrationEntity(['integrationEntityId' => $data['id']]);
92
93
        if (!$integrationEntity) {
94
            return $this->create($data);
95
        }
96
97
        /** @var Lead $lead * */
98
        $lead = $this->leadModel->getEntity($integrationEntity->getInternalEntityId());
99
100
        // prevent listeners from exporting
101
        $lead->setEventData('pipedrive.webhook', 1);
102
103
        $data         = $this->convertPipedriveData($data, $this->getIntegration()->getApiHelper()->getFields(self::PERSON_ENTITY_TYPE));
104
        $dataToUpdate = $this->getIntegration()->populateMauticLeadData($data);
105
106
        $lastSyncDate      = $integrationEntity->getLastSyncDate();
107
        $leadDateModified  = $lead->getDateModified();
108
109
        if ($lastSyncDate->format('Y-m-d H:i:s') >= $data['update_time']) {
110
            return false;
111
        } //Do not push lead if contact was modified in Mautic, and we don't wanna mofify it
112
113
        $lead->setDateModified(new \DateTime());
114
        $this->leadModel->setFieldValues($lead, $dataToUpdate, true);
115
116
        if (!isset($data['owner_id']) && $lead->getOwner()) {
117
            $lead->setOwner(null);
118
        } elseif (isset($data['owner_id'])) {
119
            $this->addOwnerToLead($data['owner_id'], $lead);
120
        }
121
        $this->leadModel->saveEntity($lead);
122
123
        $integrationEntity->setLastSyncDate(new \DateTime());
124
        $this->em->persist($integrationEntity);
125
        $this->em->flush();
126
127
        if (!$this->getIntegration()->isCompanySupportEnabled()) {
128
            return;
129
        }
130
131
        if (empty($data['org_id']) && $lead->getCompany()) {
132
            $this->removeLeadFromCompany($lead->getCompany(), $lead);
133
        } elseif (isset($data['org_id'])) {
134
            $this->addLeadToCompany($data['org_id'], $lead);
135
        }
136
137
        return true;
138
    }
139
140
    /**
141
     * @throws \Exception
142
     */
143
    public function delete(array $data = [])
144
    {
145
        $integrationEntity = $this->getLeadIntegrationEntity(['integrationEntityId' => $data['id']]);
146
147
        if (!$integrationEntity) {
148
            throw new \Exception('Lead doesn\'t have integration', Response::HTTP_NOT_FOUND);
149
        }
150
151
        /** @var Lead $lead */
152
        $lead = $this->em->getRepository(Lead::class)->findOneById($integrationEntity->getInternalEntityId());
153
154
        if (!$lead) {
0 ignored issues
show
$lead is of type Mautic\LeadBundle\Entity\Lead, thus it always evaluated to true.
Loading history...
155
            throw new \Exception('Lead doesn\'t exists in Mautic', Response::HTTP_NOT_FOUND);
156
        }
157
158
        // prevent listeners from exporting
159
        $lead->setEventData('pipedrive.webhook', 1);
160
161
        $this->leadModel->deleteEntity($lead);
162
163
        if (!empty($lead->deletedId)) {
164
            $this->em->remove($integrationEntity);
165
        }
166
    }
167
168
    /**
169
     * @param $integrationOwnerId
170
     */
171
    private function addOwnerToLead($integrationOwnerId, Lead $lead)
172
    {
173
        $mauticOwner = $this->getOwnerByIntegrationId($integrationOwnerId);
174
        $lead->setOwner($mauticOwner);
175
    }
176
177
    /**
178
     * @param $companyName
179
     *
180
     * @throws \Doctrine\ORM\ORMException
181
     */
182
    private function removeLeadFromCompany($companyName, Lead $lead)
183
    {
184
        $company = $this->em->getRepository(Company::class)->findOneByName($companyName);
185
186
        if (!$company) {
187
            return;
188
        }
189
190
        $this->companyModel->removeLeadFromCompany($company, $lead);
191
    }
192
193
    /**
194
     * @param $integrationCompanyId
195
     *
196
     * @throws \Doctrine\ORM\ORMException
197
     */
198
    private function addLeadToCompany($integrationCompanyId, Lead $lead)
199
    {
200
        $integrationEntityCompany = $this->getCompanyIntegrationEntity(['integrationEntityId' => $integrationCompanyId]);
201
202
        if (!$integrationEntityCompany) {
203
            return;
204
        }
205
206
        if (!$company = $this->companyModel->getEntity($integrationEntityCompany->getInternalEntityId())) {
207
            return;
208
        }
209
210
        $this->companyModel->addLeadToCompany($company, $lead);
211
    }
212
}
213