Issues (3627)

EventListener/SetContactAvatarFormSubscriber.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2019 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\LeadBundle\EventListener;
13
14
use Mautic\FormBundle\Entity\Field;
15
use Mautic\FormBundle\Event\SubmissionEvent;
16
use Mautic\FormBundle\Form\Type\FormFieldFileType;
17
use Mautic\FormBundle\FormEvents;
18
use Mautic\FormBundle\Helper\FormUploader;
19
use Mautic\LeadBundle\Model\LeadModel;
20
use Mautic\LeadBundle\Templating\Helper\AvatarHelper;
21
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
22
23
class SetContactAvatarFormSubscriber implements EventSubscriberInterface
24
{
25
    /**
26
     * @var AvatarHelper
27
     */
28
    private $avatarHelper;
29
30
    /**
31
     * @var FormUploader
32
     */
33
    private $uploader;
34
35
    /**
36
     * @var LeadModel
37
     */
38
    private $leadModel;
39
40
    public function __construct(AvatarHelper $avatarHelper, FormUploader $uploader, LeadModel $leadModel)
41
    {
42
        $this->avatarHelper = $avatarHelper;
43
        $this->uploader     = $uploader;
44
        $this->leadModel    = $leadModel;
45
    }
46
47
    /**
48
     * @return array
49
     */
50
    public static function getSubscribedEvents()
51
    {
52
        return [
53
            FormEvents::FORM_ON_SUBMIT => ['onFormSubmit', 0],
54
        ];
55
    }
56
57
    public function onFormSubmit(SubmissionEvent $submissionEvent)
58
    {
59
        $fields  = $submissionEvent->getForm()->getFields();
60
        $contact = $submissionEvent->getLead();
61
        $results = $submissionEvent->getResults();
62
63
        if (!$contact) {
64
            return;
65
        }
66
67
        /** @var Field $field */
68
        foreach ($fields as $field) {
69
            switch ($field->getType()) {
70
                case 'file':
71
                    $properties = $field->getProperties();
72
                    if (empty($properties[FormFieldFileType::PROPERTY_PREFERED_PROFILE_IMAGE])) {
73
                        break;
74
                    }
75
                    if (empty($results[$field->getAlias()])) {
76
                        break;
77
                    }
78
                    try {
79
                        $filePath = $this->uploader->getCompleteFilePath($field, $results[$field->getAlias()]);
80
                        $this->avatarHelper->createAvatarFromFile($contact, $filePath);
81
                        $contact->setPreferredProfileImage('custom');
82
                        $this->leadModel->saveEntity($contact);
83
84
                        return;
85
                    } catch (\Exception $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
86
                    }
87
88
                    break;
89
            }
90
        }
91
    }
92
}
93