Completed
Push — master ( 0891d0...500833 )
by Paweł
48:22 queued 28:23
created

createProductAttributeValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
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\ProductAttributeInterface;
15
use Sylius\Component\Product\Model\ProductAttributeValueInterface;
16
use Sylius\Component\Product\Model\ProductInterface;
17
use Sylius\Component\Resource\Factory\FactoryInterface;
18
use Sylius\Component\Resource\Translation\Provider\TranslationLocaleProviderInterface;
19
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
20
use Symfony\Component\Form\FormEvent;
21
use Symfony\Component\Form\FormEvents;
22
23
/**
24
 * @author Grzegorz Sadowski <[email protected]>
25
 */
26
final class BuildAttributesFormSubscriber implements EventSubscriberInterface
27
{
28
    /**
29
     * @var FactoryInterface
30
     */
31
    private $attributeValueFactory;
32
33
    /**
34
     * @var TranslationLocaleProviderInterface
35
     */
36
    private $localeProvider;
37
38
    /**
39
     * @param FactoryInterface $attributeValueFactory
40
     * @param TranslationLocaleProviderInterface $localeProvider
41
     */
42
    public function __construct(
43
        FactoryInterface $attributeValueFactory,
44
        TranslationLocaleProviderInterface $localeProvider
45
    ) {
46
        $this->attributeValueFactory = $attributeValueFactory;
47
        $this->localeProvider = $localeProvider;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public static function getSubscribedEvents()
54
    {
55
        return [
56
            FormEvents::PRE_SET_DATA => 'preSetData',
57
        ];
58
    }
59
60
    /**
61
     * @param FormEvent $event
62
     */
63
    public function preSetData(FormEvent $event)
64
    {
65
        /** @var ProductInterface $product */
66
        $product = $event->getData();
67
68
        $localeCodes = $this->localeProvider->getDefinedLocalesCodes();
69
        $defaultLocaleCode = $this->localeProvider->getDefaultLocaleCode();
70
71
        $attributes = $product->getAttributes()->filter(
72
            function (ProductAttributeValueInterface $attribute) use ($defaultLocaleCode) {
73
                return $attribute->getLocaleCode() === $defaultLocaleCode;
74
            }
75
        );
76
77
        foreach ($attributes as $attribute) {
78
            foreach ($localeCodes as $localeCode) {
79
                if (!$product->hasAttributeByCodeAndLocale($attribute->getCode(), $localeCode)) {
80
                    $attributeValue = $this->createProductAttributeValue($attribute->getAttribute(), $localeCode);
81
                    $product->addAttribute($attributeValue);
82
                }
83
            }
84
        }
85
    }
86
87
    /**
88
     * @param ProductAttributeInterface $attribute
89
     * @param string $localeCode
90
     *
91
     * @return ProductAttributeValueInterface
92
     */
93
    private function createProductAttributeValue(ProductAttributeInterface $attribute, $localeCode)
94
    {
95
        /** @var ProductAttributeValueInterface $attributeValue */
96
        $attributeValue = $this->attributeValueFactory->createNew();
97
        $attributeValue->setAttribute($attribute);
98
        $attributeValue->setLocaleCode($localeCode);
99
100
        return $attributeValue;
101
    }
102
}
103