Completed
Push — master ( fb754a...2a3626 )
by Antonio
03:49
created

AbstractController::normalizeProduct()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 18
rs 9.9666
c 0
b 0
f 0
cc 4
nc 6
nop 1
1
<?php
2
3
namespace Flagbit\Bundle\ProductClonerBundle\Controller;
4
5
use Pim\Component\Catalog\Model\EntityWithFamilyVariantInterface;
6
use Pim\Component\Catalog\Repository\AttributeRepositoryInterface;
7
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
8
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
9
10
abstract class AbstractController extends Controller
11
{
12
13
    abstract protected function getNormalizer(): NormalizerInterface;
14
    abstract protected function getAttributeRepository(): AttributeRepositoryInterface;
15
16
    protected function normalizeProduct(EntityWithFamilyVariantInterface $product)
17
    {
18
        $normalizedProduct = $this->getNormalizer()->normalize($product, 'standard');
19
20
        while ($parent = $product->getParent()) {
21
            foreach ($parent->getValuesForVariation() as $value) {
22
                //this workaround removes the attributes of all parent models, as the getValues() Method,
23
                // which is called by the normalizer, returns all Values including the values of the parent Model
24
                unset($normalizedProduct['values'][$value->getAttribute()->getCode()]);
25
            }
26
            $product = $parent;
27
        };
28
29
        foreach ($this->getAttributeRepository()->findUniqueAttributeCodes() as $attributeCode) {
30
            unset($normalizedProduct['values'][$attributeCode]);
31
        }
32
        unset($normalizedProduct['identifier']);
33
        return $normalizedProduct;
34
    }
35
}
36