Completed
Pull Request — master (#23)
by Claudio
09:06 queued 04:23
created

AbstractController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 16
c 1
b 0
f 0
dl 0
loc 39
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A normalizeProduct() 0 27 5
A getAttributeCodeBlacklist() 0 3 1
1
<?php
2
3
namespace Flagbit\Bundle\ProductClonerBundle\Controller;
4
5
use Akeneo\Pim\Enrichment\Component\Product\Model\EntityWithFamilyVariantInterface;
6
use Akeneo\Pim\Structure\Component\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
15
    abstract protected function getAttributeRepository() : AttributeRepositoryInterface;
16
17
    protected function getAttributeCodeBlacklist() : array
18
    {
19
        return [];
20
    }
21
22
    protected function normalizeProduct(EntityWithFamilyVariantInterface $product)
23
    {
24
        $normalizedProduct = $this->getNormalizer()->normalize($product, 'external_api');
25
26
        while ($parent = $product->getParent()) {
27
            foreach ($parent->getValuesForVariation() as $value) {
28
                //this workaround removes the attributes of all parent models, as the getValues() Method,
29
                // which is called by the normalizer, returns all Values including the values of the parent Model
30
                unset($normalizedProduct['values'][$value->getAttributeCode()]);
31
            }
32
            $product = $parent;
33
        }
34
35
        $ignoredAttributeCodes = array_merge(
36
            $this->getAttributeRepository()->findUniqueAttributeCodes(),
37
            $this->getAttributeCodeBlacklist()
38
        );
39
40
        foreach ($ignoredAttributeCodes as $attributeCode) {
41
            unset($normalizedProduct['values'][$attributeCode]);
42
        }
43
        unset($normalizedProduct['identifier']);
44
        if (empty((array)$normalizedProduct['associations'])) {
45
            unset($normalizedProduct['associations']);
46
        }
47
48
        return $normalizedProduct;
49
    }
50
}
51