Issues (3627)

Controller/ClearbitController.php (14 issues)

1
<?php
2
3
/*
4
 * @copyright   2016 Mautic, Inc. 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\MauticClearbitBundle\Controller;
13
14
use Mautic\FormBundle\Controller\FormController;
15
use Mautic\LeadBundle\Entity\Company;
16
use Mautic\LeadBundle\Entity\Lead;
17
use MauticPlugin\MauticClearbitBundle\Form\Type\BatchLookupType;
18
use MauticPlugin\MauticClearbitBundle\Form\Type\LookupType;
19
use Symfony\Component\HttpFoundation\JsonResponse;
20
use Symfony\Component\HttpFoundation\Response;
21
22
class ClearbitController extends FormController
23
{
24
    /**
25
     * @param string $objectId
26
     *
27
     * @return JsonResponse
28
     *
29
     * @throws \InvalidArgumentException
30
     */
31
    public function lookupPersonAction($objectId = '')
32
    {
33
        if ('POST' === $this->request->getMethod()) {
34
            $data     = $this->request->request->get('clearbit_lookup', [], true);
35
            $objectId = $data['objectId'];
36
        }
37
        /** @var \Mautic\LeadBundle\Model\LeadModel $model */
38
        $model = $this->getModel('lead');
39
        $lead  = $model->getEntity($objectId);
40
41
        if (!$this->get('mautic.security')->hasEntityAccess(
42
            'lead:leads:editown',
43
            'lead:leads:editother',
44
            $lead->getPermissionUser()
45
        )
46
        ) {
47
            $this->addFlash(
0 ignored issues
show
Deprecated Code introduced by
The function Mautic\CoreBundle\Contro...nController::addFlash() has been deprecated: Will be removed in Mautic 3.0. Use CommonController::flashBag->addFlash() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

47
            /** @scrutinizer ignore-deprecated */ $this->addFlash(

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.

Loading history...
48
                $this->translator->trans('mautic.plugin.clearbit.forbidden'),
49
                [],
50
                'error'
51
            );
52
53
            return new JsonResponse(
54
                [
55
                    'closeModal' => true,
56
                    'flashes'    => $this->getFlashContent(),
57
                ]
58
            );
59
        }
60
61
        if ('GET' === $this->request->getMethod()) {
62
            $route = $this->generateUrl(
63
                'mautic_plugin_clearbit_action',
64
                [
65
                    'objectAction' => 'lookupPerson',
66
                ]
67
            );
68
69
            return $this->delegateView(
70
                [
71
                    'viewParameters' => [
72
                        'form' => $this->createForm(
73
                            LookupType::class,
74
                            [
75
                                'objectId' => $objectId,
76
                            ],
77
                            [
78
                                'action' => $route,
79
                            ]
80
                        )->createView(),
81
                        'lookupItem' => $lead->getEmail(),
82
                    ],
83
                    'contentTemplate' => 'MauticClearbitBundle:Clearbit:lookup.html.php',
84
                    'passthroughVars' => [
85
                        'activeLink'    => '#mautic_contact_index',
86
                        'mauticContent' => 'lead',
87
                        'route'         => $route,
88
                    ],
89
                ]
90
            );
91
        } else {
92
            if ('POST' === $this->request->getMethod()) {
93
                try {
94
                    $this->get('mautic.plugin.clearbit.lookup_helper')->lookupContact($lead, array_key_exists('notify', $data));
95
                    $this->addFlash(
0 ignored issues
show
Deprecated Code introduced by
The function Mautic\CoreBundle\Contro...nController::addFlash() has been deprecated: Will be removed in Mautic 3.0. Use CommonController::flashBag->addFlash() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

95
                    /** @scrutinizer ignore-deprecated */ $this->addFlash(

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.

Loading history...
96
                        'mautic.lead.batch_leads_affected',
97
                        [
98
                            'pluralCount' => 1,
99
                            '%count%'     => 1,
100
                        ]
101
                    );
102
                } catch (\Exception $ex) {
103
                    $this->addFlash(
0 ignored issues
show
Deprecated Code introduced by
The function Mautic\CoreBundle\Contro...nController::addFlash() has been deprecated: Will be removed in Mautic 3.0. Use CommonController::flashBag->addFlash() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

103
                    /** @scrutinizer ignore-deprecated */ $this->addFlash(

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.

Loading history...
104
                        $ex->getMessage(),
105
                        [],
106
                        'error'
107
                    );
108
                }
109
110
                return new JsonResponse(
111
                    [
112
                        'closeModal' => true,
113
                        'flashes'    => $this->getFlashContent(),
114
                    ]
115
                );
116
            }
117
        }
118
119
        return new Response('Bad Request', 400);
120
    }
121
122
    /**
123
     * @return JsonResponse
124
     *
125
     * @throws \InvalidArgumentException
126
     */
127
    public function batchLookupPersonAction()
128
    {
129
        /** @var \Mautic\LeadBundle\Model\LeadModel $model */
130
        $model = $this->getModel('lead');
131
        if ('GET' === $this->request->getMethod()) {
132
            $data = $this->request->query->get('clearbit_batch_lookup', [], true);
0 ignored issues
show
The call to Symfony\Component\HttpFo...ion\ParameterBag::get() has too many arguments starting with true. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

132
            /** @scrutinizer ignore-call */ 
133
            $data = $this->request->query->get('clearbit_batch_lookup', [], true);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
133
        } else {
134
            $data = $this->request->request->get('clearbit_batch_lookup', [], true);
135
        }
136
137
        $entities = [];
138
        if (array_key_exists('ids', $data)) {
139
            $ids = $data['ids'];
140
141
            if (!is_array($ids)) {
142
                $ids = json_decode($ids, true);
143
            }
144
145
            if (is_array($ids) && count($ids)) {
146
                $entities = $model->getEntities(
147
                    [
148
                        'filter' => [
149
                            'force' => [
150
                                [
151
                                    'column' => 'l.id',
152
                                    'expr'   => 'in',
153
                                    'value'  => $ids,
154
                                ],
155
                            ],
156
                        ],
157
                        'ignore_paginator' => true,
158
                    ]
159
                );
160
            }
161
        }
162
163
        $lookupEmails = [];
164
        if ($count = count($entities)) {
165
            /** @var Lead $lead */
166
            foreach ($entities as $lead) {
167
                if ($this->get('mautic.security')->hasEntityAccess(
168
                        'lead:leads:editown',
169
                        'lead:leads:editother',
170
                        $lead->getPermissionUser()
171
                    )
172
                    && $lead->getEmail()
173
                ) {
174
                    $lookupEmails[$lead->getId()] = $lead->getEmail();
175
                }
176
            }
177
178
            $count = count($lookupEmails);
179
        }
180
181
        if (0 === $count) {
182
            $this->addFlash(
0 ignored issues
show
Deprecated Code introduced by
The function Mautic\CoreBundle\Contro...nController::addFlash() has been deprecated: Will be removed in Mautic 3.0. Use CommonController::flashBag->addFlash() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

182
            /** @scrutinizer ignore-deprecated */ $this->addFlash(

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.

Loading history...
183
                $this->translator->trans('mautic.plugin.clearbit.empty'),
184
                [],
185
                'error'
186
            );
187
188
            return new JsonResponse(
189
                [
190
                    'closeModal' => true,
191
                    'flashes'    => $this->getFlashContent(),
192
                ]
193
            );
194
        } else {
195
            if ($count > 20) {
196
                $this->addFlash(
0 ignored issues
show
Deprecated Code introduced by
The function Mautic\CoreBundle\Contro...nController::addFlash() has been deprecated: Will be removed in Mautic 3.0. Use CommonController::flashBag->addFlash() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

196
                /** @scrutinizer ignore-deprecated */ $this->addFlash(

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.

Loading history...
197
                    $this->translator->trans('mautic.plugin.clearbit.toomany'),
198
                    [],
199
                    'error'
200
                );
201
202
                return new JsonResponse(
203
                    [
204
                        'closeModal' => true,
205
                        'flashes'    => $this->getFlashContent(),
206
                    ]
207
                );
208
            }
209
        }
210
        if ('GET' === $this->request->getMethod()) {
211
            $route = $this->generateUrl(
212
                'mautic_plugin_clearbit_action',
213
                [
214
                    'objectAction' => 'batchLookupPerson',
215
                ]
216
            );
217
218
            return $this->delegateView(
219
                [
220
                    'viewParameters' => [
221
                        'form' => $this->createForm(
222
                            BatchLookupType::class,
223
                            [],
224
                            [
225
                                'action' => $route,
226
                            ]
227
                        )->createView(),
228
                        'lookupItems' => array_values($lookupEmails),
229
                    ],
230
                    'contentTemplate' => 'MauticClearbitBundle:Clearbit:batchLookup.html.php',
231
                    'passthroughVars' => [
232
                        'activeLink'    => '#mautic_contact_index',
233
                        'mauticContent' => 'leadBatch',
234
                        'route'         => $route,
235
                    ],
236
                ]
237
            );
238
        } else {
239
            if ('POST' === $this->request->getMethod()) {
240
                $notify = array_key_exists('notify', $data);
241
                foreach ($lookupEmails as $id => $lookupEmail) {
242
                    if ($lead = $model->getEntity($id)) {
243
                        try {
244
                            $this->get('mautic.plugin.clearbit.lookup_helper')->lookupContact($lead, $notify);
245
                        } catch (\Exception $ex) {
246
                            $this->addFlash(
247
                                    $ex->getMessage(),
248
                                    [],
249
                                    'error'
250
                                );
251
                            --$count;
252
                        }
253
                    }
254
                }
255
256
                if ($count) {
257
                    $this->addFlash(
0 ignored issues
show
Deprecated Code introduced by
The function Mautic\CoreBundle\Contro...nController::addFlash() has been deprecated: Will be removed in Mautic 3.0. Use CommonController::flashBag->addFlash() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

257
                    /** @scrutinizer ignore-deprecated */ $this->addFlash(

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.

Loading history...
258
                            'mautic.lead.batch_leads_affected',
259
                            [
260
                                'pluralCount' => $count,
261
                                '%count%'     => $count,
262
                            ]
263
                        );
264
                }
265
266
                return new JsonResponse(
267
                    [
268
                        'closeModal' => true,
269
                         'flashes'   => $this->getFlashContent(),
270
                    ]
271
                );
272
            }
273
        }
274
275
        return new Response('Bad Request', 400);
276
    }
277
278
    /***************** COMPANY ***********************/
279
280
    /**
281
     * @param string $objectId
282
     *
283
     * @return JsonResponse
284
     *
285
     * @throws \InvalidArgumentException
286
     */
287
    public function lookupCompanyAction($objectId = '')
288
    {
289
        if ('POST' === $this->request->getMethod()) {
290
            $data     = $this->request->request->get('clearbit_lookup', [], true);
291
            $objectId = $data['objectId'];
292
        }
293
        /** @var \Mautic\LeadBundle\Model\CompanyModel $model */
294
        $model = $this->getModel('lead.company');
295
        /** @var Company $company */
296
        $company = $model->getEntity($objectId);
297
298
        if ('GET' === $this->request->getMethod()) {
299
            $route = $this->generateUrl(
300
                'mautic_plugin_clearbit_action',
301
                [
302
                    'objectAction' => 'lookupCompany',
303
                ]
304
            );
305
306
            $website = $company->getFieldValue('companywebsite');
307
308
            if (!$website) {
309
                $this->addFlash(
0 ignored issues
show
Deprecated Code introduced by
The function Mautic\CoreBundle\Contro...nController::addFlash() has been deprecated: Will be removed in Mautic 3.0. Use CommonController::flashBag->addFlash() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

309
                /** @scrutinizer ignore-deprecated */ $this->addFlash(

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.

Loading history...
310
                    $this->translator->trans('mautic.plugin.clearbit.compempty'),
311
                    [],
312
                    'error'
313
                );
314
315
                return new JsonResponse(
316
                    [
317
                        'closeModal' => true,
318
                        'flashes'    => $this->getFlashContent(),
319
                    ]
320
                );
321
            }
322
            $parse = parse_url($website);
323
324
            return $this->delegateView(
325
                [
326
                    'viewParameters' => [
327
                        'form' => $this->createForm(
328
                            LookupType::class,
329
                            [
330
                                'objectId' => $objectId,
331
                            ],
332
                            [
333
                                'action' => $route,
334
                            ]
335
                        )->createView(),
336
                        'lookupItem' => $parse['host'],
337
                    ],
338
                    'contentTemplate' => 'MauticClearbitBundle:Clearbit:lookup.html.php',
339
                    'passthroughVars' => [
340
                        'activeLink'    => '#mautic_company_index',
341
                        'mauticContent' => 'company',
342
                        'route'         => $route,
343
                    ],
344
                ]
345
            );
346
        } else {
347
            if ('POST' === $this->request->getMethod()) {
348
                try {
349
                    $this->get('mautic.plugin.clearbit.lookup_helper')->lookupCompany($company, array_key_exists('notify', $data));
350
                    $this->addFlash(
0 ignored issues
show
Deprecated Code introduced by
The function Mautic\CoreBundle\Contro...nController::addFlash() has been deprecated: Will be removed in Mautic 3.0. Use CommonController::flashBag->addFlash() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

350
                    /** @scrutinizer ignore-deprecated */ $this->addFlash(

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.

Loading history...
351
                        'mautic.company.batch_companies_affected',
352
                        [
353
                            'pluralCount' => 1,
354
                            '%count%'     => 1,
355
                        ]
356
                    );
357
                } catch (\Exception $ex) {
358
                    $this->addFlash(
0 ignored issues
show
Deprecated Code introduced by
The function Mautic\CoreBundle\Contro...nController::addFlash() has been deprecated: Will be removed in Mautic 3.0. Use CommonController::flashBag->addFlash() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

358
                    /** @scrutinizer ignore-deprecated */ $this->addFlash(

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.

Loading history...
359
                        $ex->getMessage(),
360
                        [],
361
                        'error'
362
                    );
363
                }
364
365
                return new JsonResponse(
366
                    [
367
                        'closeModal' => true,
368
                        'flashes'    => $this->getFlashContent(),
369
                    ]
370
                );
371
            }
372
        }
373
374
        return new Response('Bad Request', 400);
375
    }
376
377
    /**
378
     * @return JsonResponse
379
     *
380
     * @throws \InvalidArgumentException
381
     */
382
    public function batchLookupCompanyAction()
383
    {
384
        /** @var \Mautic\LeadBundle\Model\CompanyModel $model */
385
        $model = $this->getModel('lead.company');
386
        if ('GET' === $this->request->getMethod()) {
387
            $data = $this->request->query->get('clearbit_batch_lookup', [], true);
0 ignored issues
show
The call to Symfony\Component\HttpFo...ion\ParameterBag::get() has too many arguments starting with true. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

387
            /** @scrutinizer ignore-call */ 
388
            $data = $this->request->query->get('clearbit_batch_lookup', [], true);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
388
        } else {
389
            $data = $this->request->request->get('clearbit_batch_lookup', [], true);
390
        }
391
392
        $entities = [];
393
        if (array_key_exists('ids', $data)) {
394
            $ids = $data['ids'];
395
396
            if (!is_array($ids)) {
397
                $ids = json_decode($ids, true);
398
            }
399
400
            if (is_array($ids) && count($ids)) {
401
                $entities = $model->getEntities(
402
                    [
403
                        'filter' => [
404
                            'force' => [
405
                                [
406
                                    'column' => 'comp.id',
407
                                    'expr'   => 'in',
408
                                    'value'  => $ids,
409
                                ],
410
                            ],
411
                        ],
412
                        'ignore_paginator' => true,
413
                    ]
414
                );
415
            }
416
        }
417
418
        $lookupWebsites = [];
419
        if ($count = count($entities)) {
420
            /** @var Company $company */
421
            foreach ($entities as $company) {
422
                if ($company->getFieldValue('companywebsite')) {
423
                    $website = $company->getFieldValue('companywebsite');
424
                    $parse   = parse_url($website);
425
                    if (!isset($parse['host'])) {
426
                        continue;
427
                    }
428
                    $lookupWebsites[$company->getId()] = $parse['host'];
429
                }
430
            }
431
432
            $count = count($lookupWebsites);
433
        }
434
435
        if (0 === $count) {
436
            $this->addFlash(
0 ignored issues
show
Deprecated Code introduced by
The function Mautic\CoreBundle\Contro...nController::addFlash() has been deprecated: Will be removed in Mautic 3.0. Use CommonController::flashBag->addFlash() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

436
            /** @scrutinizer ignore-deprecated */ $this->addFlash(

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.

Loading history...
437
                $this->translator->trans('mautic.plugin.clearbit.compempty'),
438
                [],
439
                'error'
440
            );
441
442
            return new JsonResponse(
443
                [
444
                    'closeModal' => true,
445
                    'flashes'    => $this->getFlashContent(),
446
                ]
447
            );
448
        } else {
449
            if ($count > 20) {
450
                $this->addFlash(
0 ignored issues
show
Deprecated Code introduced by
The function Mautic\CoreBundle\Contro...nController::addFlash() has been deprecated: Will be removed in Mautic 3.0. Use CommonController::flashBag->addFlash() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

450
                /** @scrutinizer ignore-deprecated */ $this->addFlash(

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.

Loading history...
451
                    $this->translator->trans('mautic.plugin.clearbit.comptoomany'),
452
                    [],
453
                    'error'
454
                );
455
456
                return new JsonResponse(
457
                    [
458
                        'closeModal' => true,
459
                        'flashes'    => $this->getFlashContent(),
460
                    ]
461
                );
462
            }
463
        }
464
        if ('GET' === $this->request->getMethod()) {
465
            $route = $this->generateUrl(
466
                'mautic_plugin_clearbit_action',
467
                [
468
                    'objectAction' => 'batchLookupCompany',
469
                ]
470
            );
471
472
            return $this->delegateView(
473
                [
474
                    'viewParameters' => [
475
                        'form' => $this->createForm(
476
                            BatchLookupType::class,
477
                            [],
478
                            [
479
                                'action' => $route,
480
                            ]
481
                        )->createView(),
482
                        'lookupItems' => array_values($lookupWebsites),
483
                    ],
484
                    'contentTemplate' => 'MauticClearbitBundle:Clearbit:batchLookup.html.php',
485
                    'passthroughVars' => [
486
                        'activeLink'    => '#mautic_company_index',
487
                        'mauticContent' => 'companyBatch',
488
                        'route'         => $route,
489
                    ],
490
                ]
491
            );
492
        } else {
493
            if ('POST' === $this->request->getMethod()) {
494
                $notify = array_key_exists('notify', $data);
495
                foreach ($lookupWebsites as $id => $lookupWebsite) {
496
                    if ($company = $model->getEntity($id)) {
497
                        try {
498
                            $this->get('mautic.plugin.clearbit.lookup_helper')->lookupCompany($company, $notify);
499
                        } catch (\Exception $ex) {
500
                            $this->addFlash(
501
                                $ex->getMessage(),
502
                                [],
503
                                'error'
504
                            );
505
                            --$count;
506
                        }
507
                    }
508
                }
509
510
                if ($count) {
511
                    $this->addFlash(
0 ignored issues
show
Deprecated Code introduced by
The function Mautic\CoreBundle\Contro...nController::addFlash() has been deprecated: Will be removed in Mautic 3.0. Use CommonController::flashBag->addFlash() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

511
                    /** @scrutinizer ignore-deprecated */ $this->addFlash(

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.

Loading history...
512
                        'mautic.company.batch_companies_affected',
513
                        [
514
                            'pluralCount' => $count,
515
                            '%count%'     => $count,
516
                        ]
517
                    );
518
                }
519
520
                return new JsonResponse(
521
                    [
522
                        'closeModal' => true,
523
                        'flashes'    => $this->getFlashContent(),
524
                    ]
525
                );
526
            }
527
        }
528
529
        return new Response('Bad Request', 400);
530
    }
531
}
532