Completed
Push — master ( d67f41...79042b )
by Adam
07:27
created

ContactBoxController   B

Coupling/Cohesion

Components 1
Dependencies 16

Complexity

Total Complexity 12

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 16
dl 0
loc 89
rs 8.4614
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B indexAction() 0 49 5
A getRecipients() 0 11 2
A getDynamicAttachments() 0 17 4
A getReCaptchaHelper() 0 4 1
1
<?php
2
/*
3
 * WellCommerce Open-Source E-Commerce Platform
4
 *
5
 * This file is part of the WellCommerce package.
6
 *
7
 * (c) Adam Piotrowski <[email protected]>
8
 *
9
 * For the full copyright and license information,
10
 * please view the LICENSE file that was distributed with this source code.
11
 */
12
13
namespace WellCommerce\Bundle\CmsBundle\Controller\Box;
14
15
use Symfony\Component\HttpFoundation\File\UploadedFile;
16
use Symfony\Component\HttpFoundation\Response;
17
use WellCommerce\Bundle\AppBundle\Entity\Client;
18
use WellCommerce\Bundle\AppBundle\Service\ReCaptcha\Helper\ReCaptchaHelper;
19
use WellCommerce\Bundle\CmsBundle\Entity\Contact;
20
use WellCommerce\Bundle\CmsBundle\Entity\ContactTicket;
21
use WellCommerce\Bundle\CoreBundle\Controller\Box\AbstractBoxController;
22
use WellCommerce\Component\Layout\Collection\LayoutBoxSettingsCollection;
23
24
/**
25
 * Class ContactBoxController
26
 *
27
 * @author  Adam Piotrowski <[email protected]>
28
 */
29
class ContactBoxController extends AbstractBoxController
30
{
31
    private $allowedMimeTypes = ['image/jpg', 'image/png', 'application/pdf'];
32
    
33
    public function indexAction(LayoutBoxSettingsCollection $boxSettings): Response
34
    {
35
        /** @var ContactTicket $resource */
36
        $resource = $this->get('contact_ticket.manager')->initResource();
37
        $client   = $this->getSecurityHelper()->getCurrentClient();
38
        
39
        if ($client instanceof Client) {
40
            $resource->setEmail($client->getContactDetails()->getEmail());
41
            $resource->setName($client->getContactDetails()->getFirstName());
42
            $resource->setSurname($client->getContactDetails()->getLastName());
43
            $resource->setPhone($client->getContactDetails()->getPhone());
44
        }
45
        
46
        $form = $this->formBuilder->createForm($resource, [
47
            'enctype' => 'multipart/form-data',
48
        ]);
49
        
50
        if ($form->handleRequest()->isSubmitted()) {
51
            if ($form->isValid()) {
52
                if ($this->getReCaptchaHelper()->isValid()) {
53
                    $this->getManager()->createResource($resource);
54
                    
55
                    $this->getMailerHelper()->sendEmail([
56
                        'recipient'           => $this->getRecipients($resource),
57
                        'reply_to'            => $resource->getEmail(),
58
                        'subject'             => $resource->getSubject(),
59
                        'template'            => 'WellCommerceCmsBundle:Email:contact.html.twig',
60
                        'parameters'          => [
61
                            'contact' => $resource,
62
                        ],
63
                        'dynamic_attachments' => $this->getDynamicAttachments(),
64
                        'configuration'       => $this->getShopStorage()->getCurrentShop()->getMailerConfiguration(),
65
                    ]);
66
                    
67
                    $this->getFlashHelper()->addSuccess('contact_ticket.flash.success');
68
                }
69
                
70
                return $this->getRouterHelper()->redirectTo('front.contact.index');
71
            }
72
            
73
            $this->getFlashHelper()->addError('contact_ticket.flash.error');
74
        }
75
        
76
        return $this->displayTemplate('index', [
77
            'form'             => $form,
78
            'boxSettings'      => $boxSettings,
79
            'allowedMimeTypes' => $this->allowedMimeTypes,
80
        ]);
81
    }
82
    
83
    private function getRecipients(ContactTicket $ticket): array
84
    {
85
        $recipients   = [];
86
        $recipients[] = $this->getShopStorage()->getCurrentShop()->getMailerConfiguration()->getFrom();
87
        
88
        if ($ticket->getContact() instanceof Contact) {
89
            $recipients[] = $ticket->getContact()->translate()->getEmail();
90
        }
91
        
92
        return $recipients;
93
    }
94
    
95
    private function getDynamicAttachments(): array
96
    {
97
        $dynamicAttachments = [];
98
        $request            = $this->getRequestHelper()->getCurrentRequest();
99
        $uploadedFile       = $request->files->get('attachment');
100
        if ($uploadedFile instanceof UploadedFile) {
101
            if ($uploadedFile->isValid() && in_array($uploadedFile->getMimeType(), $this->allowedMimeTypes)) {
102
                $dynamicAttachments[] = [
103
                    'data' => file_get_contents($uploadedFile->getPathname()),
104
                    'name' => $uploadedFile->getClientOriginalName(),
105
                    'type' => $uploadedFile->getClientMimeType(),
106
                ];
107
            }
108
        }
109
        
110
        return $dynamicAttachments;
111
    }
112
    
113
    private function getReCaptchaHelper(): ReCaptchaHelper
114
    {
115
        return $this->get('recaptcha.helper');
116
    }
117
}
118