AssociationNormalizer   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 34.21 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 8
c 4
b 0
f 0
lcom 1
cbo 1
dl 26
loc 76
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B normalize() 26 41 6
A supportsNormalization() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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