Code Duplication    Length = 67-68 lines in 2 locations

Normalizer/Attribute/PimCatalogMetricNormalizer.php 1 location

@@ 13-80 (lines=68) @@
10
 *
11
 * @package Actualys\Bundle\DrupalCommerceConnectorBundle\Normalizer
12
 */
13
class PimCatalogMetricNormalizer implements NormalizerInterface
14
{
15
    /**
16
     * @param  object                                                $attribute
17
     * @param  null                                                  $format
18
     * @param  array                                                 $context
19
     * @return array|\Symfony\Component\Serializer\Normalizer\scalar
20
     */
21
    public function normalize($attribute, $format = null, array $context = [])
22
    {
23
        /**@var Attribute $attribute * */
24
        $normalizedAttribute       = [
25
          'code'       => null,
26
          'type'       => null,
27
          'required'   => null,
28
          'labels'     => null,
29
          'parameters' => null,
30
        ];
31
        $availableLocales          = [];
32
        $attributeAvailableLocales = $attribute->getAvailableLocales();
33
        if (!is_null($attributeAvailableLocales)) {
34
            foreach ($attribute->getAvailableLocales() as $availableLocale) {
35
                $availableLocales [] = $availableLocale;
36
            }
37
        }
38
39
        $normalizedAttribute['required']   = $attribute->isRequired();
40
        $normalizedAttribute['type']       = $attribute->getAttributeType();
41
        $normalizedAttribute['code']       = $attribute->getCode();
42
        $normalizedAttribute['parameters'] = [
43
          'scope'               => $attribute->isScopable(),
44
          'localizable'         => $attribute->isLocalizable(),
45
          'available_locales'   => $availableLocales,
46
          'default_value'       => null,
47
          'negative_allowed'    => (bool) $attribute->isNegativeAllowed(),
48
          'decimale_allowed'    => (bool) $attribute->isDecimalsAllowed(),
49
          'number_min'          => (bool) $attribute->getNumberMin(),
50
          'number_max'          => (bool) $attribute->getNumberMax(),
51
          'metric_family'       => $attribute->getMetricFamily(),
52
          'default_metric_unit' => $attribute->getDefaultMetricUnit(),
53
        ];
54
55
        if ($attribute->isLocalizable()) {
56
            foreach ($attribute->getTranslations() as $trans) {
57
                $normalizedAttribute['labels'][$trans->getLocale(
58
                )] = $trans->getLabel();
59
            }
60
        } else {
61
            $normalizedAttribute['labels'][LANGUAGE_NONE] = $attribute->getLabel(
62
            );
63
        }
64
65
        return $normalizedAttribute;
66
    }
67
68
    /**
69
     * Checks whether the given class is supported for normalization by this normalizer
70
     *
71
     * @param mixed  $data   Data to normalize.
72
     * @param string $format The format being (de-)serialized from or into.
73
     *
74
     * @return boolean
75
     */
76
    public function supportsNormalization($data, $format = null)
77
    {
78
        return $data instanceof Attribute;
79
    }
80
}
81

Normalizer/Attribute/PimCatalogNumberNormalizer.php 1 location

@@ 13-79 (lines=67) @@
10
 *
11
 * @package Actualys\Bundle\DrupalCommerceConnectorBundle\Normalizer
12
 */
13
class PimCatalogNumberNormalizer implements NormalizerInterface
14
{
15
    /**
16
     * @param  object                                                $attribute
17
     * @param  null                                                  $format
18
     * @param  array                                                 $context
19
     * @return array|\Symfony\Component\Serializer\Normalizer\scalar
20
     */
21
    public function normalize($attribute, $format = null, array $context = [])
22
    {
23
        /**@var Attribute $attribute * */
24
        $normalizedAttribute       = [
25
          'code'       => null,
26
          'type'       => null,
27
          'required'   => null,
28
          'labels'     => null,
29
          'parameters' => null,
30
        ];
31
        $availableLocales          = [];
32
        $attributeAvailableLocales = $attribute->getAvailableLocales();
33
        if (!is_null($attributeAvailableLocales)) {
34
            foreach ($attribute->getAvailableLocales() as $availableLocale) {
35
                $availableLocales [] = $availableLocale;
36
            }
37
        }
38
39
        $normalizedAttribute['required']   = $attribute->isRequired();
40
        $normalizedAttribute['type']       = $attribute->getAttributeType();
41
        $normalizedAttribute['code']       = $attribute->getCode();
42
        $normalizedAttribute['parameters'] = [
43
          'scope'             => $attribute->isScopable(),
44
          'localizable'       => $attribute->isLocalizable(),
45
          'unique_value'      => $attribute->isUnique(),
46
          'available_locales' => $availableLocales,
47
          'default_value'     => null,
48
          'negative_allowed'  => $attribute->isNegativeAllowed(),
49
          'decimale_allowed'  => $attribute->isDecimalsAllowed(),
50
          'number_min'        => (bool) $attribute->getNumberMin(),
51
          'number_max'        => (bool) $attribute->getNumberMax(),
52
        ];
53
54
        if ($attribute->isLocalizable()) {
55
            foreach ($attribute->getTranslations() as $trans) {
56
                $normalizedAttribute['labels'][$trans->getLocale(
57
                )] = $trans->getLabel();
58
            }
59
        } else {
60
            $normalizedAttribute['labels'][LANGUAGE_NONE] = $attribute->getLabel(
61
            );
62
        }
63
64
        return $normalizedAttribute;
65
    }
66
67
    /**
68
     * Checks whether the given class is supported for normalization by this normalizer
69
     *
70
     * @param mixed  $data   Data to normalize.
71
     * @param string $format The format being (de-)serialized from or into.
72
     *
73
     * @return boolean
74
     */
75
    public function supportsNormalization($data, $format = null)
76
    {
77
        return $data instanceof Attribute;
78
    }
79
}
80