Skip to content

Commit 6e62fd0

Browse files
committed
Fix SonataAdminBundle integration
1 parent 8d9d175 commit 6e62fd0

File tree

3 files changed

+89
-25
lines changed

3 files changed

+89
-25
lines changed

DependencyInjection/PayumExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ protected function loadDynamicGateways(array $dynamicGatewaysConfig, ContainerBu
228228
$container->setDefinition('payum.dynamic_registry', $registry);
229229

230230
if ($dynamicGatewaysConfig['sonata_admin']) {
231-
throw new \LogicException('Not supported. Has to wait till Sonata Admin 4.x will be released.');
231+
//throw new \LogicException('Not supported. Has to wait till Sonata Admin 4.x will be released.');
232232

233233
if (false === class_exists(AbstractAdmin::class)) {
234234
throw new LogicException('Admin class does not exists. Did you install SonataAdmin bundle?');
@@ -239,7 +239,7 @@ protected function loadDynamicGateways(array $dynamicGatewaysConfig, ContainerBu
239239
$configClass,
240240
null
241241
]);
242-
$gatewayConfigAdmin->addMethodCall('setFormFactory', [new Reference('form.factory')]);
242+
$gatewayConfigAdmin->addMethodCall('setGatewayFactoryRegistry', [new Reference('payum')]);
243243

244244
if ($container->hasDefinition('payum.dynamic_gateways.cypher')) {
245245
$gatewayConfigAdmin->addMethodCall('setCypher', [new Reference('payum.dynamic_gateways.cypher')]);

Form/Type/GatewayConfigType.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,20 @@ public function buildCredentials(FormEvent $event): void
4949
}
5050

5151
$form = $event->getForm();
52+
53+
$gatewayFactory = $this->registry->getGatewayFactory($factoryName);
54+
$config = $gatewayFactory->createConfig();
55+
56+
if (isset($config['payum.gateway_config_type'])) {
57+
$form->add('config', $config['payum.gateway_config_type']);
58+
$event->setData($data);
59+
60+
return;
61+
}
5262

5363
$form->add('config', FormType::class);
5464
$configForm = $form->get('config');
5565

56-
$gatewayFactory = $this->registry->getGatewayFactory($factoryName);
57-
$config = $gatewayFactory->createConfig();
5866
$propertyPath = is_array($data) ? '[config]' : 'config';
5967
$firstTime = ! PropertyAccess::createPropertyAccessor()->getValue($data, $propertyPath);
6068
foreach ($config['payum.default_options'] as $name => $value) {

Sonata/GatewayConfigAdmin.php

Lines changed: 77 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,62 @@
11
<?php
22
namespace Payum\Bundle\PayumBundle\Sonata;
33

4+
use Payum\Bundle\PayumBundle\Form\Type\GatewayFactoriesChoiceType;
5+
use Payum\Core\Registry\GatewayFactoryRegistryInterface;
46
use Payum\Core\Security\CryptedInterface;
57
use Payum\Core\Security\CypherInterface;
68
use Sonata\AdminBundle\Admin\AbstractAdmin;
79
use Sonata\AdminBundle\Datagrid\ListMapper;
810
use Sonata\AdminBundle\Form\FormMapper;
11+
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
12+
use Symfony\Component\Form\Extension\Core\Type\FormType;
13+
use Symfony\Component\Form\Extension\Core\Type\TextType;
914
use Symfony\Component\Form\FormBuilderInterface;
15+
use Symfony\Component\Form\FormEvent;
16+
use Symfony\Component\Form\FormEvents;
1017
use Symfony\Component\Form\FormFactoryInterface;
1118
use Payum\Bundle\PayumBundle\Form\Type\GatewayConfigType;
19+
use Symfony\Component\PropertyAccess\PropertyAccess;
1220

1321
class GatewayConfigAdmin extends AbstractAdmin
1422
{
15-
protected FormFactoryInterface $formFactory;
1623
protected ?CypherInterface $cypher;
1724

18-
public function setFormFactory(FormFactoryInterface $formFactory): void
19-
{
20-
$this->formFactory = $formFactory;
21-
}
22-
25+
protected GatewayFactoryRegistryInterface $registry;
26+
2327
public function setCypher(CypherInterface $cypher): void
2428
{
2529
$this->cypher = $cypher;
2630
}
31+
32+
public function setGatewayFactoryRegistry(GatewayFactoryRegistryInterface $registry)
33+
{
34+
$this->registry = $registry;
35+
}
2736

2837
/**
2938
* {@inheritdoc}
3039
*/
3140
protected function configureFormFields(FormMapper $form): void
3241
{
33-
$form->reorder(array()); //hack!
42+
43+
$form
44+
->with('General', ['class' => 'col-md-4'])->end()
45+
->with('Configuration', ['class' => 'col-md-8'])->end()
46+
;
47+
48+
$form
49+
->with('General')
50+
->add('gatewayName')
51+
->add('factoryName', GatewayFactoriesChoiceType::class, [
52+
'disabled' => (bool) $this->getSubject() && null !== $this->getSubject()->getId(),
53+
])
54+
->end()
55+
;
56+
57+
if ($this->getSubject() && $this->getSubject()->getId()) {
58+
$this->buildCredentials($form, $this->getSubject());
59+
}
3460
}
3561

3662
/**
@@ -78,28 +104,58 @@ public function prePersist($object): void
78104
/**
79105
* {@inheritdoc}
80106
*/
81-
public function getObject($id)
107+
protected function alterObject(object $object): void
82108
{
83-
$object = parent::getObject($id);
84-
85109
if ($this->cypher && $object instanceof CryptedInterface) {
86110
$object->decrypt($this->cypher);
87111
}
88-
89-
return $object;
90112
}
91113

92-
/**
93-
* {@inheritDoc}
94-
*/
95-
public function getFormBuilder(): FormBuilderInterface
114+
public function buildCredentials(FormMapper $form, object $object): void
96115
{
97-
$formBuilder = $this->formFactory->createBuilder(GatewayConfigType::class, $this->getSubject(), array(
98-
'data_class' => get_class($this->getSubject()),
99-
));
116+
/** @var array $data */
117+
$data = $object;
118+
119+
120+
$propertyPath = is_array($data) ? '[factoryName]' : 'factoryName';
121+
$factoryName = PropertyAccess::createPropertyAccessor()->getValue($data, $propertyPath);
122+
if (empty($factoryName)) {
123+
return;
124+
}
125+
126+
$gatewayFactory = $this->registry->getGatewayFactory($factoryName);
127+
$config = $gatewayFactory->createConfig();
128+
129+
if (isset($config['payum.gateway_config_type'])) {
130+
$form
131+
->with('Configuration')
132+
->add('config', $config['payum.gateway_config_type'], [
133+
'label' => false,
134+
])
135+
->end()
136+
;
100137

101-
$this->defineFormBuilder($formBuilder);
138+
return;
139+
}
140+
141+
$form->add('config', FormType::class);
142+
$configForm = $form->get('config');
143+
144+
$propertyPath = is_array($data) ? '[config]' : 'config';
145+
$firstTime = ! PropertyAccess::createPropertyAccessor()->getValue($data, $propertyPath);
146+
147+
foreach ($config['payum.default_options'] as $name => $value) {
148+
$propertyPath = is_array($data) ? "[config][{$name}]" : "config[{$name}]";
149+
if ($firstTime) {
150+
PropertyAccess::createPropertyAccessor()->setValue($data, $propertyPath, $value);
151+
}
102152

103-
return $formBuilder;
153+
$type = is_bool($value) ? CheckboxType::class : TextType::class;
154+
155+
$options = [];
156+
$options['required'] = in_array($name, $config['payum.required_options']);
157+
158+
$configForm->add($name, $type, $options);
159+
}
104160
}
105161
}

0 commit comments

Comments
 (0)