AttributeNormalizer::normalize()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 13

Duplication

Lines 15
Ratio 71.43 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 15
loc 21
rs 9.3142
cc 2
eloc 13
nc 2
nop 3
1
<?php
2
3
namespace Actualys\Bundle\DrupalCommerceConnectorBundle\Normalizer;
4
5
use Pim\Bundle\CatalogBundle\Entity\Attribute;
6
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
7
use Actualys\Bundle\DrupalCommerceConnectorBundle\Normalizer\Exception\NormalizeException;
8
use Actualys\Bundle\DrupalCommerceConnectorBundle\Guesser\NormalizerGuesser;
9
10
class AttributeNormalizer implements NormalizerInterface
11
{
12
    /**
13
     * @var NormalizerGuesser $normalizerGuesser
14
     */
15
    protected $normalizerGuesser;
16
17
    /**
18
     * @param NormalizerGuesser $normalizerGuesser
19
     */
20
    public function __construct(
21
      NormalizerGuesser $normalizerGuesser
22
    ) {
23
        $this->normalizerGuesser = $normalizerGuesser;
24
    }
25
26
    /**
27
     * @param  object                                                                          $attribute
28
     * @param  null                                                                            $format
29
     * @param  array                                                                           $context
30
     * @return array|\Symfony\Component\Serializer\Normalizer\scalar
31
     * @throws NormalizeException
32
     * @throws \Actualys\Bundle\DrupalCommerceConnectorBundle\Guesser\Exception\GuessException
33
     */
34
    public function normalize($attribute, $format = null, array $context = [])
35
    {
36
        $type = $attribute->getAttributeType();
37 View Code Duplication
        if ($normalizer = $this->normalizerGuesser->guessNormalizer(
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...
38
          $type,
39
          'attribute'
40
        )
41
        ) {
42
            $normalizedAttribute = $normalizer->normalize(
43
              $attribute,
44
              null,
45
              $context
46
            );
47
        } else {
48
            throw new NormalizeException(
49
              'Type field not supported: "'.$type.'".'
50
            );
51
        }
52
53
        return $normalizedAttribute;
54
    }
55
56
    /**
57
     * Checks whether the given class is supported for normalization by this normalizer
58
     *
59
     * @param mixed  $data   Data to normalize.
60
     * @param string $format The format being (de-)serialized from or into.
61
     *
62
     * @return boolean
63
     */
64
    public function supportsNormalization($data, $format = null)
65
    {
66
        return $data instanceof Attribute;
67
    }
68
}
69