Completed
Push — master ( 1ecbe4...2ec63c )
by Kamil
21:52
created

ConfigurableProductSubscriber::preSetData()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 3
eloc 13
nc 3
nop 1
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\ProductBundle\Form\EventSubscriber;
13
14
use Sylius\Component\Product\Model\ProductInterface;
15
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16
use Symfony\Component\Form\FormEvent;
17
use Symfony\Component\Form\FormEvents;
18
use Webmozart\Assert\Assert;
19
20
/**
21
 * @author Łukasz Chruściel <[email protected]>
22
 */
23
class ConfigurableProductSubscriber implements EventSubscriberInterface
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public static function getSubscribedEvents()
29
    {
30
        return [
31
            FormEvents::PRE_SET_DATA => 'preSetData',
32
        ];
33
    }
34
35
    /**
36
     * @param FormEvent $event
37
     */
38
    public function preSetData(FormEvent $event)
39
    {
40
        /** @var ProductInterface $product */
41
        $product = $event->getData();
42
43
        Assert::isInstanceOf($product, ProductInterface::class);
44
45
        if ($product->isSimple()) {
46
            return;
47
        }
48
49
        $form = $event->getForm();
50
        $form->remove('masterVariant');
51
52
        /** Options should be disabled for configurable product if it has at least one defined variant */
53
        $disableOptions = (null !== $product->getMasterVariant()) && (false === $product->hasVariants());
54
55
        $form->add('options', 'sylius_product_option_choice', [
56
            'required' => false,
57
            'disabled' => $disableOptions,
58
            'multiple' => true,
59
            'label' => 'sylius.form.product.options',
60
        ]);
61
    }
62
}
63