AssociationNormalizer::normalize()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 41
Code Lines 26

Duplication

Lines 26
Ratio 63.41 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 26
loc 41
rs 8.439
cc 6
eloc 26
nc 6
nop 3
1
<?php
2
3
namespace Actualys\Bundle\DrupalCommerceConnectorBundle\Normalizer;
4
5
use Pim\Bundle\CatalogBundle\Model\Product;
6
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
7
use Pim\Bundle\CatalogBundle\Manager\ProductManager;
8
9
class AssociationNormalizer implements NormalizerInterface
10
{
11
    /**
12
     * @var ProductManager $productManager
13
     */
14
    protected $productManager;
15
16
    /**
17
     * @param ProductManager $productManager
18
     */
19
    public function __construct(ProductManager $productManager)
20
    {
21
        $this->productManager = $productManager;
22
    }
23
24
    /**
25
     * @param  object                                                $product
26
     * @param  null                                                  $format
27
     * @param  array                                                 $context
28
     * @return array|\Symfony\Component\Serializer\Normalizer\scalar
29
     */
30
    public function normalize($product, $format = null, array $context = [])
31
    {
32
        $normalizedAssociations  = [];
33
        $identifierAttributeCode = $this->productManager->getIdentifierAttribute(
34
        )->getCode();
35
        $sku = $product->getValue(
36
          $this->productManager->getIdentifierAttribute()->getCode()
37
        )->getData();
38
39
        $normalizedAssociations['sku'] = $sku;
40
        $normalizedAssociations['family'] = $product->getFamily()->getCode();
41
        $normalizedAssociations['associations'] = [];
42 View Code Duplication
        foreach ($product->getAssociations() as $association) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
            $associationCode = $association->getAssociationType()->getCode();
44
45
            if ($association->getGroups()->count(
46
              ) > 0 || $association->getProducts()->count() > 0
47
            ) {
48
49
                /**@var Product $product * */
50
                $normalizedAssociations['associations'][$associationCode] = [
51
                  'type'     => null,
52
                  'groups'   => [],
53
                  'products' => [],
54
                ];
55
56
                $normalizedAssociations['associations'][$associationCode]['type'] = $associationCode;
57
                foreach ($association->getGroups() as $group) {
58
                    $normalizedAssociations['associations'][$associationCode]['groups'][] = $group->getCode(
59
                    );
60
                }
61
                foreach ($association->getProducts() as $product) {
62
                    $normalizedAssociations['associations'][$associationCode]['products'][] = $product->getValue(
63
                      $identifierAttributeCode
64
                    )->getData();
65
                }
66
            }
67
        }
68
69
        return $normalizedAssociations;
70
    }
71
72
    /**
73
     * Checks whether the given class is supported for normalization by this normalizer
74
     *
75
     * @param mixed  $data   Data to normalize.
76
     * @param string $format The format being (de-)serialized from or into.
77
     *
78
     * @return boolean
79
     */
80
    public function supportsNormalization($data, $format = null)
81
    {
82
        return $data instanceof Product;
83
    }
84
}
85