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\Component\Channel\Repository\ChannelRepositoryInterface; |
15
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
16
|
|
|
use Sylius\Component\Core\Model\ChannelPricingInterface; |
17
|
|
|
use Sylius\Component\Core\Model\ProductVariantInterface; |
18
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
19
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
20
|
|
|
use Symfony\Component\Form\FormEvent; |
21
|
|
|
use Symfony\Component\Form\FormEvents; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @author Mateusz Zalewski <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
final class ChannelPricingsFormSubscriber implements EventSubscriberInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var ChannelRepositoryInterface |
30
|
|
|
*/ |
31
|
|
|
private $channelRepository; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var FactoryInterface |
35
|
|
|
*/ |
36
|
|
|
private $channelPricingFactory; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param ChannelRepositoryInterface $channelRepository |
40
|
|
|
* @param FactoryInterface $channelPricingFactory |
41
|
|
|
*/ |
42
|
|
|
public function __construct(ChannelRepositoryInterface $channelRepository, FactoryInterface $channelPricingFactory) |
43
|
|
|
{ |
44
|
|
|
$this->channelRepository = $channelRepository; |
45
|
|
|
$this->channelPricingFactory = $channelPricingFactory; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public static function getSubscribedEvents() |
52
|
|
|
{ |
53
|
|
|
return [ |
54
|
|
|
FormEvents::PRE_SET_DATA => 'preSetData', |
55
|
|
|
FormEvents::SUBMIT => 'submit', |
56
|
|
|
]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param FormEvent $event |
61
|
|
|
*/ |
62
|
|
|
public function preSetData(FormEvent $event) |
63
|
|
|
{ |
64
|
|
|
/** @var ProductVariantInterface $productVariant */ |
65
|
|
|
$productVariant = $event->getData(); |
66
|
|
|
|
67
|
|
|
if (null === $productVariant) { |
68
|
|
|
return; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** @var ChannelInterface $channel */ |
72
|
|
|
foreach ($this->channelRepository->findAll() as $channel) { |
73
|
|
|
if ($productVariant->hasChannelPricingForChannel($channel)) { |
74
|
|
|
continue; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** @var ChannelPricingInterface $channelPricing */ |
78
|
|
|
$channelPricing = $this->channelPricingFactory->createNew(); |
79
|
|
|
$channelPricing->setChannel($channel); |
80
|
|
|
$productVariant->addChannelPricing($channelPricing); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param FormEvent $event |
86
|
|
|
*/ |
87
|
|
|
public function submit(FormEvent $event) |
88
|
|
|
{ |
89
|
|
|
/** @var ProductVariantInterface $productVariant */ |
90
|
|
|
$productVariant = $event->getData(); |
91
|
|
|
foreach ($productVariant->getChannelPricings() as $channelPricing) { |
92
|
|
|
if (null === $channelPricing->getPrice()) { |
93
|
|
|
$productVariant->removeChannelPricing($channelPricing); |
94
|
|
|
$channelPricing->setProductVariant(null); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|