1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace PTS\SyliusReferralPlugin\Controller; |
5
|
|
|
|
6
|
|
|
use FOS\RestBundle\View\View; |
7
|
|
|
use Http\Client\Exception\HttpException; |
8
|
|
|
use http\Exception\InvalidArgumentException; |
9
|
|
|
use PTS\SyliusReferralPlugin\Event\EnrollerChangeEvent; |
10
|
|
|
use PTS\SyliusReferralPlugin\Repository\CustomerRepository; |
11
|
|
|
use PTS\SyliusReferralPlugin\Service\CustomerManager; |
12
|
|
|
use Sylius\Bundle\ResourceBundle\Controller\ResourceController; |
13
|
|
|
use Sylius\Component\Core\Model\ShopUser; |
14
|
|
|
use Sylius\Component\Resource\ResourceActions; |
15
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
17
|
|
|
use Symfony\Component\HttpFoundation\Response; |
18
|
|
|
|
19
|
|
|
class CustomerController extends ResourceController |
20
|
|
|
{ |
21
|
|
|
public function createAction(Request $request): Response |
22
|
|
|
{ |
23
|
|
|
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
24
|
|
|
|
25
|
|
|
$this->isGrantedOr403($configuration, ResourceActions::CREATE); |
26
|
|
|
|
27
|
|
|
$newResource = $this->newResourceFactory->create($configuration, $this->factory); |
28
|
|
|
|
29
|
|
|
$form = $this->resourceFormFactory->create($configuration, $newResource); |
30
|
|
|
|
31
|
|
|
$refManager = $this->get('app.referral.manager'); |
32
|
|
|
|
33
|
|
|
$refManager->checkReferralValidity(); |
34
|
|
|
|
35
|
|
|
if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) { |
36
|
|
|
$newResource = $form->getData(); |
37
|
|
|
|
38
|
|
|
$referrer = $refManager->getReferrerFromSession(); |
39
|
|
|
if ($referrer) { |
40
|
|
|
$newResource->setEnroller($referrer); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$refManager->removeReferral(); |
44
|
|
|
|
45
|
|
|
$event = $this->eventDispatcher->dispatchPreEvent(ResourceActions::CREATE, $configuration, $newResource); |
46
|
|
|
|
47
|
|
|
if ($event->isStopped() && !$configuration->isHtmlRequest()) { |
48
|
|
|
throw new HttpException($event->getErrorCode(), $event->getMessage()); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
if ($event->isStopped()) { |
51
|
|
|
$this->flashHelper->addFlashFromEvent($configuration, $event); |
52
|
|
|
|
53
|
|
|
if ($event->hasResponse()) { |
54
|
|
|
return $event->getResponse(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this->redirectHandler->redirectToIndex($configuration, $newResource); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if ($configuration->hasStateMachine()) { |
61
|
|
|
$this->stateMachine->apply($configuration, $newResource); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$this->repository->add($newResource); |
65
|
|
|
$postEvent = $this->eventDispatcher->dispatchPostEvent(ResourceActions::CREATE, $configuration, $newResource); |
66
|
|
|
|
67
|
|
|
if (!$configuration->isHtmlRequest()) { |
68
|
|
|
return $this->viewHandler->handle($configuration, View::create($newResource, Response::HTTP_CREATED)); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$this->flashHelper->addSuccessFlash($configuration, ResourceActions::CREATE, $newResource); |
72
|
|
|
|
73
|
|
|
if ($postEvent->hasResponse()) { |
74
|
|
|
return $postEvent->getResponse(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this->redirectHandler->redirectToResource($configuration, $newResource); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (!$configuration->isHtmlRequest()) { |
81
|
|
|
return $this->viewHandler->handle($configuration, View::create($form, Response::HTTP_BAD_REQUEST)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$initializeEvent = $this->eventDispatcher->dispatchInitializeEvent(ResourceActions::CREATE, $configuration, $newResource); |
85
|
|
|
if ($initializeEvent->hasResponse()) { |
86
|
|
|
return $initializeEvent->getResponse(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$view = View::create() |
90
|
|
|
->setData([ |
91
|
|
|
'configuration' => $configuration, |
92
|
|
|
'metadata' => $this->metadata, |
93
|
|
|
'resource' => $newResource, |
94
|
|
|
$this->metadata->getName() => $newResource, |
95
|
|
|
'form' => $form->createView(), |
96
|
|
|
]) |
97
|
|
|
->setTemplate($configuration->getTemplate(ResourceActions::CREATE . '.html')); |
98
|
|
|
|
99
|
|
|
return $this->viewHandler->handle($configuration, $view); |
100
|
|
|
} |
101
|
|
|
public function adminShowAction(Request $request, $id): Response |
102
|
|
|
{ |
103
|
|
|
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
104
|
|
|
|
105
|
|
|
$enabledEnrollerEdit = $this->getParameter('app_edit_enroller_enabled'); |
106
|
|
|
|
107
|
|
|
$this->isGrantedOr403($configuration, ResourceActions::SHOW); |
108
|
|
|
$resource = $this->findOr404($configuration); |
109
|
|
|
|
110
|
|
|
$this->eventDispatcher->dispatch(ResourceActions::SHOW, $configuration, $resource); |
111
|
|
|
|
112
|
|
|
$customerManager = $this->get('app.customer.manager'); |
113
|
|
|
|
114
|
|
|
$routeName = $request->get('_route'); |
115
|
|
|
|
116
|
|
|
$data = $customerManager->getCustomerReferrals($request, $routeName, $id); |
117
|
|
|
|
118
|
|
|
$enroller = $customerManager->getEnroller($id); |
119
|
|
|
|
120
|
|
|
$view = View::create($resource); |
121
|
|
|
|
122
|
|
|
if ($configuration->isHtmlRequest()) { |
123
|
|
|
$view |
124
|
|
|
->setTemplate($configuration->getTemplate(ResourceActions::SHOW . '.html')) |
125
|
|
|
->setTemplateVar($this->metadata->getName()) |
126
|
|
|
->setData([ |
127
|
|
|
'configuration' => $configuration, |
128
|
|
|
'metadata' => $this->metadata, |
129
|
|
|
'resource' => $resource, |
130
|
|
|
'customers' => $data['customers'], |
131
|
|
|
'pagination' => $data['pagination'], |
132
|
|
|
'enroller' => $enroller, |
133
|
|
|
'enabledEditEnroller' => $enabledEnrollerEdit, |
134
|
|
|
$this->metadata->getName() => $resource, |
135
|
|
|
]) |
136
|
|
|
; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $this->viewHandler->handle($configuration, $view); |
140
|
|
|
} |
141
|
|
|
public function adminEditEnrollerAction(Request $request, $id): Response |
142
|
|
|
{ |
143
|
|
|
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
144
|
|
|
$this->isGrantedOr403($configuration, ResourceActions::SHOW); |
145
|
|
|
$resource = $this->findOr404($configuration); |
146
|
|
|
|
147
|
|
|
$translator = $this->get('translator'); |
148
|
|
|
$session = $this->get('session'); |
149
|
|
|
$enabled = $this->getParameter('app_edit_enroller_enabled'); |
150
|
|
|
if (!$enabled) { |
151
|
|
|
$session->getFlashBag()->add( |
152
|
|
|
'error', |
153
|
|
|
$translator->trans('app.messages.error.editEnrollerDisabled') |
154
|
|
|
); |
155
|
|
|
return $this->redirectToRoute('sylius_admin_customer_show', ['id' => $id]); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if ($request->query->has('_enroller') && $request->query->has('_action')) { |
159
|
|
|
$enrollerId = $request->query->get('_enroller'); |
160
|
|
|
if ($enrollerId !== $id && $request->query->get('_action') === 'selectEnroller') { |
161
|
|
|
/** @var CustomerManager $customerManager */ |
162
|
|
|
$customerManager = $this->get('app.customer.manager'); |
163
|
|
|
$customerManager->changeCustomerEnroller($id, $enrollerId); |
164
|
|
|
/** @var CustomerRepository $customerRepository */ |
165
|
|
|
$customerRepository = $this->get('sylius.repository.customer'); |
166
|
|
|
$enroller = $customerRepository->getCustomerById($enrollerId); |
167
|
|
|
$enrollerChangeEvent = new EnrollerChangeEvent($resource, $enroller); |
168
|
|
|
/** @var EventDispatcher $eventDispatcher */ |
169
|
|
|
$eventDispatcher = $this->get('event_dispatcher'); |
170
|
|
|
$eventDispatcher->dispatch($enrollerChangeEvent, EnrollerChangeEvent::NAME); |
171
|
|
|
$session->getFlashBag()->add( |
172
|
|
|
'success', |
173
|
|
|
$translator->trans('app.messages.success.changedEnroller') |
174
|
|
|
); |
175
|
|
|
return $this->redirectToRoute('sylius_admin_customer_show', ['id' => $id]); |
176
|
|
|
} |
177
|
|
|
else { |
178
|
|
|
throw new InvalidArgumentException("Invalid request. Can't choose customer as his own enroller"); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$resources = $this->resourcesCollectionProvider->get($configuration, $this->repository); |
183
|
|
|
|
184
|
|
|
$this->eventDispatcher->dispatch(ResourceActions::SHOW, $configuration, $resource); |
185
|
|
|
|
186
|
|
|
$view = View::create($resource); |
187
|
|
|
|
188
|
|
|
if ($configuration->isHtmlRequest()) { |
189
|
|
|
$view |
190
|
|
|
->setTemplate($configuration->getTemplate(ResourceActions::SHOW . '.html')) |
191
|
|
|
->setTemplateVar($this->metadata->getName()) |
192
|
|
|
->setData([ |
193
|
|
|
'configuration' => $configuration, |
194
|
|
|
'metadata' => $this->metadata, |
195
|
|
|
'resource' => $resource, |
196
|
|
|
'resources' => $resources, |
197
|
|
|
$this->metadata->getName() => $resource, |
198
|
|
|
]) |
199
|
|
|
; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
return $this->viewHandler->handle($configuration, $view); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
} |
This check compares calls to functions or methods with their respective definitions. If the call has less 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.