|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace PH\Bundle\CoreBundle\Controller; |
|
6
|
|
|
|
|
7
|
|
|
use FOS\RestBundle\View\View; |
|
8
|
|
|
use PH\Bundle\CoreBundle\SubscriptionEvents; |
|
9
|
|
|
use PH\Bundle\CoreBundle\Facade\SubscriptionFacadeInterface; |
|
10
|
|
|
use PH\Component\Subscription\Model\SubscriptionInterface; |
|
11
|
|
|
use Sylius\Bundle\ResourceBundle\Controller\ResourceController; |
|
12
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
15
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
|
16
|
|
|
|
|
17
|
|
|
class SubscriptionController extends ResourceController |
|
18
|
|
|
{ |
|
19
|
|
|
public function addAction(Request $request): Response |
|
20
|
|
|
{ |
|
21
|
|
|
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
|
22
|
|
|
$this->isGrantedOr403($configuration, SubscriptionEvents::CREATE); |
|
23
|
|
|
|
|
24
|
|
|
/** @var SubscriptionInterface $subscription */ |
|
25
|
|
|
$subscription = $this->newResourceFactory->create($configuration, $this->factory); |
|
26
|
|
|
|
|
27
|
|
|
$form = $this->getFormFactory()->createNamed( |
|
28
|
|
|
'', |
|
29
|
|
|
$configuration->getFormType(), |
|
30
|
|
|
$subscription, |
|
31
|
|
|
array_merge($configuration->getFormOptions(), ['csrf_protection' => false]) |
|
32
|
|
|
); |
|
33
|
|
|
|
|
34
|
|
|
if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) { |
|
35
|
|
|
/** @var SubscriptionInterface $subscription */ |
|
36
|
|
|
$subscription = $form->getData(); |
|
37
|
|
|
|
|
38
|
|
|
$event = $this->eventDispatcher->dispatchPreEvent(SubscriptionEvents::CREATE, $configuration, $subscription); |
|
39
|
|
|
|
|
40
|
|
|
if ($event->isStopped() && !$configuration->isHtmlRequest()) { |
|
41
|
|
|
throw new HttpException($event->getErrorCode(), $event->getMessage()); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$subscription = $this->getSubscriptionFacade()->prepareSubscription($subscription); |
|
|
|
|
|
|
45
|
|
|
if ($configuration->hasStateMachine()) { |
|
46
|
|
|
$this->stateMachine->apply($configuration, $subscription); |
|
47
|
|
|
} |
|
48
|
|
|
$this->repository->add($subscription); |
|
49
|
|
|
$this->eventDispatcher->dispatchPostEvent(SubscriptionEvents::CREATE, $configuration, $subscription); |
|
50
|
|
|
|
|
51
|
|
|
return $this->viewHandler->handle($configuration, View::create($subscription, Response::HTTP_CREATED)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $this->viewHandler->handle($configuration, View::create($form, Response::HTTP_BAD_REQUEST)); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return FormFactoryInterface |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function getFormFactory(): FormFactoryInterface |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->get('form.factory'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return SubscriptionFacadeInterface |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function getSubscriptionFacade(): SubscriptionFacadeInterface |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->get('ph.facade.subscription'); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.