Completed
Push — unused-definitions ( d9908f )
by Kamil
18:33
created

ContactController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 6
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\ShopBundle\Controller;
13
14
use Sylius\Bundle\CoreBundle\EmailManager\ContactEmailManager;
15
use Sylius\Bundle\CoreBundle\Form\Type\ContactType;
16
use Sylius\Component\Channel\Context\ChannelContextInterface;
17
use Sylius\Component\Customer\Context\CustomerContextInterface;
18
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
19
use Symfony\Component\Form\FormFactoryInterface;
20
use Symfony\Component\HttpFoundation\RedirectResponse;
21
use Symfony\Component\HttpFoundation\Request;
22
use Symfony\Component\HttpFoundation\Response;
23
use Symfony\Component\Routing\RouterInterface;
24
25
/**
26
 * @author Grzegorz Sadowski <[email protected]>
27
 */
28
class ContactController
29
{
30
    /**
31
     * @var RouterInterface
32
     */
33
    private $router;
34
35
    /**
36
     * @var FormFactoryInterface
37
     */
38
    private $formFactory;
39
40
    /**
41
     * @var EngineInterface
42
     */
43
    private $templatingEngine;
44
45
    /**
46
     * @var ChannelContextInterface
47
     */
48
    private $channelContext;
49
50
    /**
51
     * @var CustomerContextInterface
52
     */
53
    private $customerContext;
54
55
    /**
56
     * @var ContactEmailManager
57
     */
58
    private $contactEmailManager;
59
60
    /**
61
     * @param RouterInterface $router
62
     * @param FormFactoryInterface $formFactory
63
     * @param EngineInterface $templatingEngine
64
     * @param ChannelContextInterface $channelContext
65
     * @param CustomerContextInterface $customerContext
66
     * @param ContactEmailManager $contactEmailManager
67
     */
68
    public function __construct(
69
        RouterInterface $router,
70
        FormFactoryInterface $formFactory,
71
        EngineInterface $templatingEngine,
72
        ChannelContextInterface $channelContext,
73
        CustomerContextInterface $customerContext,
74
        ContactEmailManager $contactEmailManager
75
    ) {
76
        $this->router = $router;
77
        $this->formFactory = $formFactory;
78
        $this->templatingEngine = $templatingEngine;
79
        $this->channelContext = $channelContext;
80
        $this->customerContext = $customerContext;
81
        $this->contactEmailManager = $contactEmailManager;
82
    }
83
84
    /**
85
     * @param Request $request
86
     *
87
     * @return Response
88
     */
89
    public function requestAction(Request $request)
90
    {
91
        $formType = $this->getSyliusAttribute($request, 'form', ContactType::class);
92
        $form = $this->formFactory->create($formType, null, $this->getFormOptions());
93
94
        if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
95
            $data = $form->getData();
96
            $channel = $this->channelContext->getChannel();
97
            $contactEmail = $channel->getContactEmail();
98
99
            if (null === $contactEmail) {
100
                $errorMessage = $this->getSyliusAttribute(
101
                    $request,
102
                    'error_flash',
103
                    'sylius.contact.request_error'
104
                );
105
                $request->getSession()->getFlashBag()->add('error', $errorMessage);
106
107
                return new RedirectResponse($request->headers->get('referer'));
108
            }
109
110
            $this->contactEmailManager->sendContactRequest($data, [$contactEmail]);
111
112
            $successMessage = $this->getSyliusAttribute(
113
                $request,
114
                'success_flash',
115
                'sylius.contact.request_success'
116
            );
117
            $request->getSession()->getFlashBag()->add('success', $successMessage);
118
119
            $redirectRoute = $this->getSyliusAttribute($request, 'redirect', 'referer');
120
121
            return new RedirectResponse($this->router->generate($redirectRoute));
122
        }
123
124
        $template = $this->getSyliusAttribute($request, 'template', "@SyliusShop/Contact/request.html.twig");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal @SyliusShop/Contact/request.html.twig does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
125
126
        return $this->templatingEngine->renderResponse($template, ['form' => $form->createView()]);
127
    }
128
129
    /**
130
     * @param Request $request
131
     * @param string $attributeName
132
     * @param string|null $default
133
     *
134
     * @return string|null
135
     */
136
    private function getSyliusAttribute(Request $request, $attributeName, $default = null)
137
    {
138
        $attributes = $request->attributes->get('_sylius');
139
140
        return isset($attributes[$attributeName]) ? $attributes[$attributeName] : $default;
141
    }
142
143
    /**
144
     * @return array
145
     */
146
    private function getFormOptions()
147
    {
148
        $customer = $this->customerContext->getCustomer();
149
150
        if (null === $customer) {
151
            return [];
152
        }
153
154
        return ['email' => $customer->getEmail()];
155
    }
156
}
157