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\PayumBundle\Form\Extension; |
13
|
|
|
|
14
|
|
|
use Payum\Core\Security\CryptedInterface; |
15
|
|
|
use Payum\Core\Security\CypherInterface; |
16
|
|
|
use Sylius\Bundle\PayumBundle\Form\Type\GatewayConfigType; |
17
|
|
|
use Symfony\Component\Form\AbstractTypeExtension; |
18
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
19
|
|
|
use Symfony\Component\Form\FormEvent; |
20
|
|
|
use Symfony\Component\Form\FormEvents; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Kamil Kokot <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
final class CryptedGatewayConfigTypeExtension extends AbstractTypeExtension |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var CypherInterface|null |
29
|
|
|
*/ |
30
|
|
|
private $cypher; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param CypherInterface|null $cypher |
34
|
|
|
*/ |
35
|
|
|
public function __construct(CypherInterface $cypher = null) |
36
|
|
|
{ |
37
|
|
|
$this->cypher = $cypher; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options) |
44
|
|
|
{ |
45
|
|
|
if (null === $this->cypher) { |
46
|
|
|
return; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$builder |
50
|
|
|
->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) { |
51
|
|
|
$gatewayConfig = $event->getData(); |
52
|
|
|
|
53
|
|
|
if (!$gatewayConfig instanceof CryptedInterface) { |
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$gatewayConfig->decrypt($this->cypher); |
|
|
|
|
58
|
|
|
|
59
|
|
|
$event->setData($gatewayConfig); |
60
|
|
|
}) |
61
|
|
|
->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) { |
62
|
|
|
$gatewayConfig = $event->getData(); |
63
|
|
|
|
64
|
|
|
if (!$gatewayConfig instanceof CryptedInterface) { |
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$gatewayConfig->encrypt($this->cypher); |
|
|
|
|
69
|
|
|
|
70
|
|
|
$event->setData($gatewayConfig); |
71
|
|
|
}) |
72
|
|
|
; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
|
|
public function getExtendedType() |
79
|
|
|
{ |
80
|
|
|
return GatewayConfigType::class; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: