Issues (3627)

Integration/Pipedrive/Import/CompanyImport.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\Helper\IdentifyCompanyHelper;
8
use Mautic\LeadBundle\Model\CompanyModel;
9
use Symfony\Component\HttpFoundation\Response;
10
11
class CompanyImport extends AbstractImport
12
{
13
    /**
14
     * @var CompanyModel
15
     */
16
    private $companyModel;
17
18
    /**
19
     * CompanyImport constructor.
20
     */
21
    public function __construct(EntityManager $em, CompanyModel $companyModel)
22
    {
23
        parent::__construct($em);
24
25
        $this->companyModel = $companyModel;
26
    }
27
28
    /**
29
     * @return bool
30
     *
31
     * @throws \Doctrine\ORM\OptimisticLockException
32
     * @throws \Exception
33
     */
34
    public function create(array $data = [])
35
    {
36
        if (!$this->getIntegration()->isCompanySupportEnabled()) {
37
            return false; //feature disabled
38
        }
39
40
        $integrationEntity = $this->getCompanyIntegrationEntity(['integrationEntityId' => $data['id']]);
41
42
        if ($integrationEntity) {
43
            throw new \Exception('Company already have integration', Response::HTTP_CONFLICT);
44
        }
45
46
        $company = new Company();
47
48
        // prevent listeners from exporting
49
        $company->setEventData('pipedrive.webhook', 1);
50
51
        $data       = $this->convertPipedriveData($data, $this->getIntegration()->getApiHelper()->getFields(self::ORGANIZATION_ENTITY_TYPE));
52
        $mappedData = $this->getMappedCompanyData($data);
53
54
        // find company exists
55
        $findCompany = IdentifyCompanyHelper::findCompany($mappedData, $this->companyModel);
56
        if (isset($findCompany[0]['id'])) {
57
            throw new \Exception('Company already exist', Response::HTTP_CONFLICT);
58
        }
59
60
        $this->companyModel->setFieldValues($company, $mappedData);
61
        $this->companyModel->saveEntity($company);
62
63
        if ($data['owner_id']) {
64
            $this->addOwnerToCompany($data['owner_id'], $company);
65
        }
66
67
        $integrationEntity = $this->getCompanyIntegrationEntity(['integrationEntityId' => $data['id']]);
68
69
        if (!$integrationEntity) {
70
            $integrationEntity = $this->createIntegrationCompanyEntity(new \DateTime(), $data['id'], $company->getId());
71
        }
72
        $this->em->persist($integrationEntity);
73
        $this->em->flush();
74
75
        return true;
76
    }
77
78
    /**
79
     * @return bool
80
     *
81
     * @throws \Doctrine\ORM\OptimisticLockException
82
     * @throws \Exception
83
     */
84
    public function update(array $data = [])
85
    {
86
        if (!$this->getIntegration()->isCompanySupportEnabled()) {
87
            return false; //feature disabled
88
        }
89
90
        $integrationEntity = $this->getCompanyIntegrationEntity(['integrationEntityId' => $data['id']]);
91
92
        if (!$integrationEntity) {
93
            return $this->create($data);
94
        }
95
96
        /** @var Company $company */
97
        $company = $this->companyModel->getEntity($integrationEntity->getInternalEntityId());
98
99
        // prevent listeners from exporting
100
        $company->setEventData('pipedrive.webhook', 1);
101
102
        $data    = $this->convertPipedriveData($data, $this->getIntegration()->getApiHelper()->getFields(self::ORGANIZATION_ENTITY_TYPE));
103
        if ($data['owner_id']) {
104
            $this->addOwnerToCompany($data['owner_id'], $company);
105
        }
106
107
        $mappedData = $this->getMappedCompanyData($data);
108
109
        $this->companyModel->setFieldValues($company, $mappedData, true);
110
        $this->companyModel->saveEntity($company);
111
112
        $integrationEntity->setLastSyncDate(new \DateTime());
113
        $this->em->persist($integrationEntity);
114
        $this->em->flush();
115
116
        return true;
117
    }
118
119
    /**
120
     * @throws \Exception
121
     */
122
    public function delete(array $data = [])
123
    {
124
        if (!$this->getIntegration()->isCompanySupportEnabled()) {
125
            return; //feature disabled
126
        }
127
128
        $integrationEntity = $this->getCompanyIntegrationEntity(['integrationEntityId' => $data['id']]);
129
130
        if (!$integrationEntity) {
131
            throw new \Exception('Company doesn\'t have integration', Response::HTTP_NOT_FOUND);
132
        }
133
134
        /** @var Company $company */
135
        $company = $this->em->getRepository(Company::class)->findOneById($integrationEntity->getInternalEntityId());
136
137
        if (!$company) {
0 ignored issues
show
$company is of type Mautic\LeadBundle\Entity\Company, thus it always evaluated to true.
Loading history...
138
            throw new \Exception('Company doesn\'t exists', Response::HTTP_NOT_FOUND);
139
        }
140
141
        // prevent listeners from exporting
142
        $company->setEventData('pipedrive.webhook', 1);
143
        $this->companyModel->deleteEntity($company);
144
145
        if (!empty($company->deletedId)) {
146
            $this->em->remove($integrationEntity);
147
        }
148
    }
149
150
    /**
151
     * @param $data
152
     *
153
     * @return array
154
     */
155
    private function getMappedCompanyData(array $data)
156
    {
157
        $mappedData    = [];
158
        $companyFields = $this->getIntegration()->getIntegrationSettings()->getFeatureSettings()['companyFields'];
159
160
        foreach ($companyFields as $externalField => $internalField) {
161
            if (!array_key_exists($externalField, $data)) {
162
                continue;
163
            }
164
165
            $mappedData[$internalField] = $data[$externalField];
166
        }
167
168
        return $mappedData;
169
    }
170
171
    /**
172
     * @param $integrationOwnerId
173
     */
174
    private function addOwnerToCompany($integrationOwnerId, Company $company)
175
    {
176
        $mauticOwner = $this->getOwnerByIntegrationId($integrationOwnerId);
177
        $company->setOwner($mauticOwner);
178
    }
179
}
180