FamilyNormalizer::normalize()   B
last analyzed

Complexity

Conditions 6
Paths 18

Size

Total Lines 38
Code Lines 25

Duplication

Lines 4
Ratio 10.53 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 4
loc 38
rs 8.439
cc 6
eloc 25
nc 18
nop 3
1
<?php
2
3
namespace Actualys\Bundle\DrupalCommerceConnectorBundle\Normalizer;
4
5
use Pim\Bundle\CatalogBundle\Entity\Family;
6
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
7
use Pim\Bundle\CatalogBundle\Entity\Repository\AssociationTypeRepository;
8
9
class FamilyNormalizer implements NormalizerInterface
10
{
11
    /**
12
     * @var AttributeNormalizer
13
     */
14
    protected $attributeNormalizer;
15
16
    /**
17
     * @param AttributeNormalizer $attributeNormalizer The entity manager
18
     */
19
    public function __construct(
20
      AttributeNormalizer $attributeNormalizer,
21
      AssociationTypeRepository $associationTypeRepository
22
)
23
    {
24
        $this->attributeNormalizer = $attributeNormalizer;
25
        $this->associationTypeRepository = $associationTypeRepository;
0 ignored issues
show
Bug introduced by
The property associationTypeRepository does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
26
    }
27
28
    /**
29
     * @param  object                                                $family
30
     * @param  null                                                  $format
31
     * @param  array                                                 $context
32
     * @return array|\Symfony\Component\Serializer\Normalizer\scalar
33
     * @throws Exception\NormalizeException
34
     */
35
    public function normalize($family, $format = null, array $context = [])
36
    {
37
        /**@var Family $family * */
38
        $normalizedFamily = [
39
          'code'             => $family->getCode(),
40
          'labels'           => [],
41
          'attribute_groups' => [],
42
        ];
43
        foreach ($family->getTranslations() as $trans) {
44
            $normalizedFamily['labels'][$trans->getLocale()] = $trans->getLabel(
45
            );
46
        }
47
48
        $associations = $this->associationTypeRepository->findAll();
49
        $normalizedFamily['associations'] = array();
50
        foreach($associations as $association) {
51
            $normalizedFamily['associations'][$association->getCode()] = array();
52 View Code Duplication
            foreach ($association->getTranslations() as $assocTrans) {
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...
53
                $normalizedFamily['associations'][$association->getCode()]['labels'][$assocTrans->getLocale(
54
                )] = $assocTrans->getLabel();
55
            }
56
        }
57
58
        foreach ($family->getAttributes() as $attr) {
59
            $normalizedFamily['attribute_groups'][$attr->getGroup()->getCode(
60
            )]['code'] = $attr->getGroup()->getCode();
61
            foreach ($attr->getGroup()->getTranslations() as $groupTrans) {
62
                $normalizedFamily['attribute_groups'][$attr->getGroup()
63
                  ->getCode()]['labels'][$groupTrans->getLocale(
64
                )] = $groupTrans->getLabel();
65
            }
66
            $normalizedFamily['attribute_groups'][$attr->getGroup()->getCode(
67
            )]['attributes'][$attr->getCode(
68
            )] = $this->attributeNormalizer->normalize($attr, null, $context);
69
        }
70
71
        return $normalizedFamily;
72
    }
73
74
    /**
75
     * Checks whether the given class is supported for normalization by this normalizer
76
     *
77
     * @param mixed  $data   Data to normalize.
78
     * @param string $format The format being (de-)serialized from or into.
79
     *
80
     * @return boolean
81
     */
82
    public function supportsNormalization($data, $format = null)
83
    {
84
        return $data instanceof Family;
85
    }
86
}
87