|
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\CoreBundle\Form\EventSubscriber; |
|
13
|
|
|
|
|
14
|
|
|
use Sylius\Bundle\CoreBundle\Form\Type\Promotion\PromotionConfigurationType; |
|
15
|
|
|
use Sylius\Bundle\PromotionBundle\Form\EventListener\BuildPromotionRuleFormSubscriber; |
|
16
|
|
|
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface; |
|
17
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
|
18
|
|
|
use Sylius\Component\Core\Promotion\Checker\Rule\ChannelBasedRuleCheckerInterface; |
|
19
|
|
|
use Sylius\Component\Registry\ServiceRegistryInterface; |
|
20
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
|
21
|
|
|
use Symfony\Component\Form\FormInterface; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @author Mateusz Zalewski <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
final class BuildChannelBasedPromotionRuleFormSubscriber extends BuildPromotionRuleFormSubscriber |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var ChannelRepositoryInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $channelRepository; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param ServiceRegistryInterface $ruleCheckerRegistry |
|
35
|
|
|
* @param FormFactoryInterface $factory |
|
36
|
|
|
* @param string $registryIdentifier |
|
37
|
|
|
* @param ChannelRepositoryInterface $channelRepository |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct( |
|
40
|
|
|
ServiceRegistryInterface $ruleCheckerRegistry, |
|
41
|
|
|
FormFactoryInterface $factory, |
|
42
|
|
|
$registryIdentifier, |
|
43
|
|
|
ChannelRepositoryInterface $channelRepository |
|
44
|
|
|
) { |
|
45
|
|
|
parent::__construct($ruleCheckerRegistry, $factory, $registryIdentifier); |
|
46
|
|
|
|
|
47
|
|
|
$this->channelRepository = $channelRepository; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function addConfigurationFields(FormInterface $form, $registryIdentifier, array $data = []) |
|
54
|
|
|
{ |
|
55
|
|
|
$model = $this->registry->get($registryIdentifier); |
|
56
|
|
|
|
|
57
|
|
|
$configuration = $model->getConfigurationFormType(); |
|
58
|
|
|
if (null === $configuration) { |
|
59
|
|
|
return; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if (!$model instanceof ChannelBasedRuleCheckerInterface) { |
|
63
|
|
|
$form->add($this->createConfigurationField($configuration, $data)); |
|
64
|
|
|
|
|
65
|
|
|
return; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$configurationCollection = $this->factory->createNamed('configuration', PromotionConfigurationType::class, [], [ |
|
|
|
|
|
|
69
|
|
|
'compound' => true, |
|
70
|
|
|
'auto_initialize' => false, |
|
71
|
|
|
]); |
|
72
|
|
|
|
|
73
|
|
|
/** @var ChannelInterface $channel */ |
|
74
|
|
|
foreach ($this->channelRepository->findAll() as $channel) { |
|
75
|
|
|
$configurationCollection->add($this->createConfigurationFieldForChannel($channel, $configuration, $data)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$form->add($configurationCollection); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param ChannelInterface $channel |
|
83
|
|
|
* @param string $configuration |
|
84
|
|
|
* @param array $data |
|
85
|
|
|
* |
|
86
|
|
|
* @return FormInterface |
|
87
|
|
|
*/ |
|
88
|
|
|
private function createConfigurationFieldForChannel( |
|
89
|
|
|
ChannelInterface $channel, |
|
90
|
|
|
$configuration, |
|
91
|
|
|
array $data |
|
92
|
|
|
) { |
|
93
|
|
|
$config = [ |
|
94
|
|
|
'auto_initialize' => false, |
|
95
|
|
|
'label' => $channel->getName(), |
|
96
|
|
|
'currency' => $channel->getBaseCurrency()->getCode(), |
|
97
|
|
|
]; |
|
98
|
|
|
|
|
99
|
|
|
return $this->factory->createNamed($channel->getCode(), $configuration, $data, $config); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param string $configuration |
|
104
|
|
|
* @param array $data |
|
105
|
|
|
* |
|
106
|
|
|
* @return FormInterface |
|
107
|
|
|
*/ |
|
108
|
|
|
private function createConfigurationField($configuration, array $data) |
|
109
|
|
|
{ |
|
110
|
|
|
return $this->factory->createNamed('configuration', $configuration, $data, [ |
|
111
|
|
|
'auto_initialize' => false, |
|
112
|
|
|
'label' => false, |
|
113
|
|
|
]); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.