VariantManager   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 5
dl 0
loc 91
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getAttributesCollectionForProduct() 0 13 2
A makeVariantOptionCollection() 0 12 2
A getVariantOption() 0 13 2
A findVariantOption() 0 8 1
A getAttribute() 0 4 1
A getAttributeValue() 0 4 1
A filterValues() 0 6 1
A getVariant() 0 21 2
1
<?php
2
/*
3
 * WellCommerce Open-Source E-Commerce Platform
4
 *
5
 * This file is part of the WellCommerce package.
6
 *
7
 * (c) Adam Piotrowski <[email protected]>
8
 *
9
 * For the full copyright and license information,
10
 * please view the LICENSE file that was distributed with this source code.
11
 */
12
13
namespace WellCommerce\Bundle\CatalogBundle\Manager;
14
15
use Doctrine\Common\Collections\ArrayCollection;
16
use Doctrine\Common\Collections\Collection;
17
use WellCommerce\Bundle\CatalogBundle\Entity\Attribute;
18
use WellCommerce\Bundle\CatalogBundle\Entity\AttributeValue;
19
use WellCommerce\Bundle\CatalogBundle\Entity\Product;
20
use WellCommerce\Bundle\CatalogBundle\Entity\Variant;
21
use WellCommerce\Bundle\CatalogBundle\Entity\VariantOption;
22
use WellCommerce\Bundle\CoreBundle\Manager\AbstractManager;
23
24
/**
25
 * Class VariantManager
26
 *
27
 * @author  Adam Piotrowski <[email protected]>
28
 */
29
class VariantManager extends AbstractManager
30
{
31
    public function getAttributesCollectionForProduct(Product $product, array $values): Collection
32
    {
33
        $values     = $this->filterValues($values);
34
        $collection = new ArrayCollection();
35
        
36
        foreach ($values as $id => $value) {
37
            $variant = $this->getVariant($id, $value);
38
            $variant->setProduct($product);
39
            $collection->add($variant);
40
        }
41
        
42
        return $collection;
43
    }
44
    
45
    protected function getVariant($id, $value): Variant
46
    {
47
        /** @var $variant Variant */
48
        $variant = $this->repository->find($id);
49
        if (null === $variant) {
50
            $variant = $this->initResource();
51
        }
52
        
53
        $variantOptions = $this->makeVariantOptionCollection($variant, $value['attributes']);
0 ignored issues
show
Compatibility introduced by
$variant of type object<WellCommerce\Bund...Entity\EntityInterface> is not a sub-type of object<WellCommerce\Bund...gBundle\Entity\Variant>. It seems like you assume a concrete implementation of the interface WellCommerce\Bundle\Core...\Entity\EntityInterface 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...
54
        
55
        $variant->setModifierType($value['suffix']);
56
        $variant->setModifierValue($value['modifier']);
57
        $variant->setStock($value['stock']);
58
        $variant->setSymbol($value['symbol']);
59
        $variant->setWeight($value['weight']);
60
        $variant->setHierarchy($value['hierarchy']);
61
        $variant->setPhoto($this->get('media.repository')->find($value['photo']));
62
        $variant->setOptions($variantOptions);
63
        
64
        return $variant;
65
    }
66
    
67
    protected function makeVariantOptionCollection(Variant $variant, $values): Collection
68
    {
69
        $collection = new ArrayCollection();
70
        foreach ($values as $attributeId => $attributeValueId) {
71
            $attribute      = $this->getAttribute($attributeId);
72
            $attributeValue = $this->getAttributeValue($attributeValueId);
73
            $variantOption  = $this->getVariantOption($variant, $attribute, $attributeValue);
74
            $collection->add($variantOption);
75
        }
76
        
77
        return $collection;
78
    }
79
    
80
    protected function getVariantOption(Variant $variant, Attribute $attribute, AttributeValue $attributeValue): VariantOption
81
    {
82
        $variantOption = $this->findVariantOption($variant, $attribute, $attributeValue);
83
        
84
        if (!$variantOption instanceof VariantOption) {
85
            $variantOption = new VariantOption();
86
            $variantOption->setVariant($variant);
87
            $variantOption->setAttribute($attribute);
88
            $variantOption->setAttributeValue($attributeValue);
89
        }
90
        
91
        return $variantOption;
92
    }
93
    
94
    protected function findVariantOption(Variant $variant, Attribute $attribute, AttributeValue $attributeValue)
95
    {
96
        return $this->get('variant_option.repository')->findOneBy([
97
            'variant'        => $variant,
98
            'attribute'      => $attribute,
99
            'attributeValue' => $attributeValue,
100
        ]);
101
    }
102
    
103
    protected function getAttribute(int $id): Attribute
104
    {
105
        return $this->get('attribute.repository')->find($id);
106
    }
107
    
108
    protected function getAttributeValue(int $id): AttributeValue
109
    {
110
        return $this->get('attribute_value.repository')->find($id);
111
    }
112
    
113
    private function filterValues(array $values): array
114
    {
115
        return array_filter($values, function ($value) {
116
            return is_array($value);
117
        });
118
    }
119
}
120