1 | <?php |
||
2 | |||
3 | /* |
||
4 | * @copyright 2016 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 MauticPlugin\MauticFullContactBundle\Helper; |
||
13 | |||
14 | use Mautic\CoreBundle\Helper\EncryptionHelper; |
||
15 | use Mautic\CoreBundle\Helper\UserHelper; |
||
16 | use Mautic\LeadBundle\Entity\Company; |
||
17 | use Mautic\LeadBundle\Entity\Lead; |
||
18 | use Mautic\LeadBundle\Model\CompanyModel; |
||
19 | use Mautic\LeadBundle\Model\LeadModel; |
||
20 | use Mautic\PluginBundle\Helper\IntegrationHelper; |
||
21 | use MauticPlugin\MauticFullContactBundle\Integration\FullContactIntegration; |
||
22 | use MauticPlugin\MauticFullContactBundle\Services\FullContact_Company; |
||
23 | use MauticPlugin\MauticFullContactBundle\Services\FullContact_Person; |
||
24 | use Monolog\Logger; |
||
25 | use Symfony\Bundle\FrameworkBundle\Routing\Router; |
||
26 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
||
27 | |||
28 | class LookupHelper |
||
29 | { |
||
30 | /** |
||
31 | * @var UserHelper |
||
32 | */ |
||
33 | protected $userHelper; |
||
34 | |||
35 | /** |
||
36 | * @var bool|FullContactIntegration |
||
37 | */ |
||
38 | protected $integration; |
||
39 | |||
40 | /** |
||
41 | * @var Logger |
||
42 | */ |
||
43 | protected $logger; |
||
44 | |||
45 | /** |
||
46 | * @var Router |
||
47 | */ |
||
48 | protected $router; |
||
49 | |||
50 | /** |
||
51 | * @var LeadModel |
||
52 | */ |
||
53 | protected $leadModel; |
||
54 | |||
55 | /** |
||
56 | * @var CompanyModel |
||
57 | */ |
||
58 | protected $companyModel; |
||
59 | |||
60 | public function __construct( |
||
61 | IntegrationHelper $integrationHelper, |
||
62 | UserHelper $userHelper, |
||
63 | Logger $logger, |
||
64 | Router $router, |
||
65 | LeadModel $leadModel, |
||
66 | CompanyModel $companyModel |
||
67 | ) { |
||
68 | $this->integration = $integrationHelper->getIntegrationObject('FullContact'); |
||
69 | $this->userHelper = $userHelper; |
||
70 | $this->logger = $logger; |
||
71 | $this->router = $router; |
||
72 | $this->leadModel = $leadModel; |
||
73 | $this->companyModel = $companyModel; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @param bool $notify |
||
78 | * @param bool $checkAuto |
||
79 | */ |
||
80 | public function lookupContact(Lead $lead, $notify = false, $checkAuto = false) |
||
81 | { |
||
82 | if (!$lead->getEmail()) { |
||
83 | return; |
||
84 | } |
||
85 | |||
86 | /** @var FullContact_Person $fullcontact */ |
||
87 | if ($fullcontact = $this->getFullContact()) { |
||
88 | if (!$checkAuto || ($checkAuto && $this->integration->shouldAutoUpdate())) { |
||
89 | try { |
||
90 | list($cacheId, $webhookId, $cache) = $this->getCache($lead, $notify); |
||
91 | |||
92 | if (!array_key_exists($cacheId, $cache['fullcontact'])) { |
||
93 | $fullcontact->setWebhookUrl( |
||
94 | $this->router->generate( |
||
95 | 'mautic_plugin_fullcontact_index', |
||
96 | [], |
||
97 | UrlGeneratorInterface::ABSOLUTE_URL |
||
98 | ), |
||
99 | $webhookId |
||
100 | ); |
||
101 | $res = $fullcontact->lookupByEmail($lead->getEmail()); |
||
102 | // Prevent from filling up the cache |
||
103 | $cache['fullcontact'] = [ |
||
104 | $cacheId => serialize($res), |
||
105 | 'nonce' => $cache['fullcontact']['nonce'], |
||
106 | ]; |
||
107 | $lead->setSocialCache($cache); |
||
108 | |||
109 | if ($checkAuto) { |
||
110 | $this->leadModel->getRepository()->saveEntity($lead); |
||
111 | } else { |
||
112 | $this->leadModel->saveEntity($lead); |
||
113 | } |
||
114 | } |
||
115 | } catch (\Exception $ex) { |
||
116 | $this->logger->log('error', 'Error while using FullContact to lookup '.$lead->getEmail().': '.$ex->getMessage()); |
||
117 | } |
||
118 | } |
||
119 | } |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * @param bool $notify |
||
124 | * @param bool $checkAuto |
||
125 | */ |
||
126 | public function lookupCompany(Company $company, $notify = false, $checkAuto = false) |
||
127 | { |
||
128 | if (!$website = $company->getFieldValue('companywebsite')) { |
||
129 | return; |
||
130 | } |
||
131 | |||
132 | /** @var FullContact_Company $fullcontact */ |
||
133 | if ($fullcontact = $this->getFullContact(false)) { |
||
134 | if (!$checkAuto || ($checkAuto && $this->integration->shouldAutoUpdate())) { |
||
135 | try { |
||
136 | $parse = parse_url($website); |
||
137 | list($cacheId, $webhookId, $cache) = $this->getCache($company, $notify); |
||
138 | |||
139 | if (isset($parse['host']) && !array_key_exists($cacheId, $cache['fullcontact'])) { |
||
140 | $fullcontact->setWebhookUrl( |
||
141 | $this->router->generate( |
||
142 | 'mautic_plugin_fullcontact_index', |
||
143 | [], |
||
144 | UrlGeneratorInterface::ABSOLUTE_URL |
||
145 | ), |
||
146 | $webhookId |
||
147 | ); |
||
148 | $res = $fullcontact->lookupByDomain($parse['host']); |
||
149 | // Prevent from filling up the cache |
||
150 | $cache['fullcontact'] = [ |
||
151 | $cacheId => serialize($res), |
||
152 | 'nonce' => $cache['fullcontact']['nonce'], |
||
153 | ]; |
||
154 | $company->setSocialCache($cache); |
||
155 | if ($checkAuto) { |
||
156 | $this->companyModel->getRepository()->saveEntity($company); |
||
157 | } else { |
||
158 | $this->companyModel->saveEntity($company); |
||
159 | } |
||
160 | } |
||
161 | } catch (\Exception $ex) { |
||
162 | $this->logger->log('error', 'Error while using FullContact to lookup '.$parse['host'].': '.$ex->getMessage()); |
||
163 | } |
||
164 | } |
||
165 | } |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * @param $oid |
||
170 | */ |
||
171 | public function validateRequest($oid) |
||
172 | { |
||
173 | // prefix#entityId#hour#userId#nonce |
||
174 | list($w, $id, $hour, $uid, $nonce) = explode('#', $oid, 5); |
||
175 | $notify = (false !== strpos($w, '_notify') && $uid) ? $uid : false; |
||
176 | $type = (0 === strpos($w, 'fullcontactcomp')) ? 'company' : 'person'; |
||
177 | |||
178 | switch ($type) { |
||
179 | case 'person': |
||
180 | $entity = $this->leadModel->getEntity($id); |
||
181 | break; |
||
182 | case 'company': |
||
183 | $entity = $this->companyModel->getEntity($id); |
||
184 | break; |
||
185 | } |
||
186 | |||
187 | if ($entity) { |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
188 | $socialCache = $entity->getSocialCache(); |
||
189 | $cacheId = $w.'#'.$id.'#'.$hour; |
||
190 | |||
191 | if (isset($socialCache['fullcontact'][$cacheId]) && !empty($socialCache['fullcontact']['nonce']) && !empty($nonce) |
||
192 | && $socialCache['fullcontact']['nonce'] === $nonce |
||
193 | ) { |
||
194 | return [ |
||
195 | 'notify' => $notify, |
||
196 | 'entity' => $entity, |
||
197 | 'type' => $type, |
||
198 | ]; |
||
199 | } |
||
200 | } |
||
201 | |||
202 | return false; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * @param bool $person |
||
207 | * |
||
208 | * @return bool|FullContact_Company|FullContact_Person |
||
209 | */ |
||
210 | protected function getFullContact($person = true) |
||
211 | { |
||
212 | if (!$this->integration || !$this->integration->getIntegrationSettings()->getIsPublished()) { |
||
213 | return false; |
||
214 | } |
||
215 | |||
216 | // get api_key from plugin settings |
||
217 | $keys = $this->integration->getDecryptedApiKeys(); |
||
218 | |||
219 | return ($person) ? new FullContact_Person($keys['apikey']) : new FullContact_Company($keys['apikey']); |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * @param $entity |
||
224 | * @param $notify |
||
225 | * |
||
226 | * @return array |
||
227 | */ |
||
228 | protected function getCache($entity, $notify) |
||
229 | { |
||
230 | /** @var User $user */ |
||
231 | $user = $this->userHelper->getUser(); |
||
232 | $nonce = substr(EncryptionHelper::generateKey(), 0, 16); |
||
233 | $cacheId = sprintf('fullcontact%s%s#', $entity instanceof Company ? 'comp' : '', $notify ? '_notify' : '').$entity->getId().'#'.gmdate('YmdH'); |
||
234 | $webhookId = $cacheId.'#'.$user->getId().'#'.$nonce; |
||
235 | |||
236 | $cache = $entity->getSocialCache(); |
||
237 | if (!isset($cache['fullcontact'])) { |
||
238 | $cache['fullcontact'] = []; |
||
239 | } |
||
240 | |||
241 | $cache['fullcontact']['nonce'] = $nonce; |
||
242 | |||
243 | return [$cacheId, $webhookId, $cache]; |
||
244 | } |
||
245 | } |
||
246 |