|
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
|
|
|
]; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param FormEvent $event |
|
60
|
|
|
*/ |
|
61
|
|
|
public function preSetData(FormEvent $event) |
|
62
|
|
|
{ |
|
63
|
|
|
/** @var ProductVariantInterface $productVariant */ |
|
64
|
|
|
$productVariant = $event->getData(); |
|
65
|
|
|
|
|
66
|
|
|
if (null === $productVariant) { |
|
67
|
|
|
return; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** @var ChannelInterface $channel */ |
|
71
|
|
|
foreach ($this->channelRepository->findAll() as $channel) { |
|
72
|
|
|
if ($productVariant->hasChannelPricingForChannel($channel)) { |
|
73
|
|
|
continue; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** @var ChannelPricingInterface $channelPricing */ |
|
77
|
|
|
$channelPricing = $this->channelPricingFactory->createNew(); |
|
78
|
|
|
$channelPricing->setChannel($channel); |
|
79
|
|
|
$productVariant->addChannelPricing($channelPricing); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.