Code Duplication    Length = 55-68 lines in 2 locations

Normalizer/Attribute/PimCatalogFileNormalizer.php 1 location

@@ 12-79 (lines=68) @@
9
 *
10
 * @package Actualys\Bundle\DrupalCommerceConnectorBundle\Normalizer
11
 */
12
class PimCatalogFileNormalizer extends AbstractMediaNormalizer
13
{
14
    /**
15
     * @param  object                                                $attribute
16
     * @param  null                                                  $format
17
     * @param  array                                                 $context
18
     * @return array|\Symfony\Component\Serializer\Normalizer\scalar
19
     */
20
    public function normalize($attribute, $format = null, array $context = [])
21
    {
22
        /**@var Attribute $attribute * */
23
        $normalizedAttribute       = [
24
          'code'       => null,
25
          'type'       => null,
26
          'required'   => null,
27
          'labels'     => null,
28
          'parameters' => null,
29
        ];
30
        $availableLocales          = [];
31
        $attributeAvailableLocales = $attribute->getAvailableLocales();
32
        if (!is_null($attributeAvailableLocales)) {
33
            foreach ($attribute->getAvailableLocales() as $availableLocale) {
34
                $availableLocales [] = $availableLocale;
35
            }
36
        }
37
38
        $allowed_extensions = [];
39
        foreach ($attribute->getAllowedExtensions() as $allowed_extension) {
40
            $allowed_extensions [] = $allowed_extension;
41
        }
42
43
        $normalizedAttribute['required']   = $attribute->isRequired();
44
        $normalizedAttribute['type']       = $attribute->getAttributeType();
45
        $normalizedAttribute['code']       = $attribute->getCode();
46
        $normalizedAttribute['parameters'] = [
47
          'scope'              => $attribute->isScopable(),
48
          'localizable'        => $attribute->isLocalizable(),
49
          'available_locales'  => $availableLocales,
50
          'max_file_size'      => $attribute->getMaxFileSize(),
51
          'allowed_extensions' => $allowed_extensions,
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

Normalizer/Attribute/PimCatalogImageNormalizer.php 1 location

@@ 12-66 (lines=55) @@
9
 *
10
 * @package Actualys\Bundle\DrupalCommerceConnectorBundle\Normalizer
11
 */
12
class PimCatalogImageNormalizer extends AbstractMediaNormalizer
13
{
14
    /**
15
     * @param  object                                                $attribute
16
     * @param  null                                                  $format
17
     * @param  array                                                 $context
18
     * @return array|\Symfony\Component\Serializer\Normalizer\scalar
19
     */
20
    public function normalize($attribute, $format = null, array $context = [])
21
    {
22
        /**@var Attribute $attribute * */
23
        $normalizedAttribute       = [
24
          'code'       => null,
25
          'type'       => null,
26
          'required'   => null,
27
          'labels'     => null,
28
          'parameters' => null,
29
        ];
30
        $availableLocales          = [];
31
        $attributeAvailableLocales = $attribute->getAvailableLocales();
32
        if (!is_null($attributeAvailableLocales)) {
33
            foreach ($attribute->getAvailableLocales() as $availableLocale) {
34
                $availableLocales [] = $availableLocale;
35
            }
36
        }
37
38
        $allowed_extensions = [];
39
        foreach ($attribute->getAllowedExtensions() as $allowed_extension) {
40
            $allowed_extensions [] = $allowed_extension;
41
        }
42
43
        $normalizedAttribute['required']   = $attribute->isRequired();
44
        $normalizedAttribute['type']       = $attribute->getAttributeType();
45
        $normalizedAttribute['code']       = $attribute->getCode();
46
        $normalizedAttribute['parameters'] = [
47
          'scope'              => $attribute->isScopable(),
48
          'localizable'        => $attribute->isLocalizable(),
49
          'available_locales'  => $availableLocales,
50
          'max_file_size'      => $attribute->getMaxFileSize(),
51
          'allowed_extensions' => $allowed_extensions,
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