Completed
Push — master ( 2973d0...13f9fe )
by Michał
10:17
created

resolveLocalizedAttributes()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
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
use Webmozart\Assert\Assert;
23
24
/**
25
 * @author Grzegorz Sadowski <[email protected]>
26
 */
27
final class BuildAttributesFormSubscriber implements EventSubscriberInterface
28
{
29
    /**
30
     * @var FactoryInterface
31
     */
32
    private $attributeValueFactory;
33
34
    /**
35
     * @var TranslationLocaleProviderInterface
36
     */
37
    private $localeProvider;
38
39
    /**
40
     * @param FactoryInterface $attributeValueFactory
41
     * @param TranslationLocaleProviderInterface $localeProvider
42
     */
43
    public function __construct(
44
        FactoryInterface $attributeValueFactory,
45
        TranslationLocaleProviderInterface $localeProvider
46
    ) {
47
        $this->attributeValueFactory = $attributeValueFactory;
48
        $this->localeProvider = $localeProvider;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public static function getSubscribedEvents()
55
    {
56
        return [
57
            FormEvents::PRE_SET_DATA => 'preSetData',
58
        ];
59
    }
60
61
    /**
62
     * @param FormEvent $event
63
     *
64
     * @throws \InvalidArgumentException
65
     */
66
    public function preSetData(FormEvent $event)
67
    {
68
        /** @var ProductInterface $product */
69
        $product = $event->getData();
70
71
        Assert::isInstanceOf($product, ProductInterface::class);
72
73
        $defaultLocaleCode = $this->localeProvider->getDefaultLocaleCode();
74
75
        $attributes = $product->getAttributes()->filter(
76
            function (ProductAttributeValueInterface $attribute) use ($defaultLocaleCode) {
77
                return $attribute->getLocaleCode() === $defaultLocaleCode;
78
            }
79
        );
80
81
        foreach ($attributes as $attribute) {
82
            $this->resolveLocalizedAttributes($product, $attribute);
83
        }
84
    }
85
86
    /**
87
     * @param ProductInterface $product
88
     * @param ProductAttributeValueInterface $attribute
89
     */
90
    private function resolveLocalizedAttributes(ProductInterface $product, ProductAttributeValueInterface $attribute)
91
    {
92
        $localeCodes = $this->localeProvider->getDefinedLocalesCodes();
93
94
        foreach ($localeCodes as $localeCode) {
95
            if (!$product->hasAttributeByCodeAndLocale($attribute->getCode(), $localeCode)) {
96
                $attributeValue = $this->createProductAttributeValue($attribute->getAttribute(), $localeCode);
0 ignored issues
show
Compatibility introduced by
$attribute->getAttribute() of type object<Sylius\Component\...del\AttributeInterface> is not a sub-type of object<Sylius\Component\...ductAttributeInterface>. It seems like you assume a child interface of the interface Sylius\Component\Attribu...odel\AttributeInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
97
                $product->addAttribute($attributeValue);
98
            }
99
        }
100
    }
101
102
    /**
103
     * @param ProductAttributeInterface $attribute
104
     * @param string $localeCode
105
     *
106
     * @return ProductAttributeValueInterface
107
     */
108
    private function createProductAttributeValue(ProductAttributeInterface $attribute, $localeCode)
109
    {
110
        /** @var ProductAttributeValueInterface $attributeValue */
111
        $attributeValue = $this->attributeValueFactory->createNew();
112
        $attributeValue->setAttribute($attribute);
113
        $attributeValue->setLocaleCode($localeCode);
114
115
        return $attributeValue;
116
    }
117
}
118