CategoryNormalizer   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 6.06 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B normalize() 4 31 5
A supportsNormalization() 0 4 1
A normalizeChildren() 0 3 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\Entity\Category;
6
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
7
use Actualys\Bundle\DrupalCommerceConnectorBundle\Entity\Repository\CategoryRepository;
8
9
class CategoryNormalizer implements NormalizerInterface
10
{
11
12
    protected $categoryRepository;
13
14
    public function __construct(
15
      CategoryRepository $categoryRepository
16
    ) {
17
        $this->categoryRepository = $categoryRepository;
18
    }
19
20
    /**
21
     * @param  object                                                $category
22
     * @param  null                                                  $format
23
     * @param  array                                                 $context
24
     * @return array|\Symfony\Component\Serializer\Normalizer\scalar
25
     */
26
    public function normalize($category, $format = null, array $context = [])
27
    {
28
        $normalizedCategory = [
29
          'code'     => null,
30
          'labels'   => [],
31
          'children' => [],
32
        ];
33
34
        $normalizedCategory ['code'] = $category->getCode();
35
        /**@var Category $category * */
36
        foreach ($category->getTranslations() as $trans) {
37
            $normalizedCategory['labels'] [$trans->getLocale(
38
            )] = $trans->getLabel();
39
        }
40
41
        $childrenIds = $this->categoryRepository->getAllChildrenIds($category);
42
43
        foreach ($childrenIds as $childrenId) {
44
            $children = $this->categoryRepository->find($childrenId);
45 View Code Duplication
            foreach ($children->getTranslations() as $trans) {
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...
46
                $normalizedCategory['children'][$children->getCode(
47
                )]['labels'] [$trans->getLocale()] = $trans->getLabel();
48
            }
49
            $normalizedCategory['children'][$children->getCode(
50
            )]['parent'] = (!is_null(
51
              $children->getParent()
52
            ) ? $children->getParent()->getCode() : null);
53
        }
54
55
        return $normalizedCategory;
56
    }
57
58
    /**
59
     * Checks whether the given class is supported for normalization by this normalizer
60
     *
61
     * @param mixed  $data   Data to normalize.
62
     * @param string $format The format being (de-)serialized from or into.
63
     *
64
     * @return boolean
65
     */
66
    public function supportsNormalization($data, $format = null)
67
    {
68
        return $data instanceof Category;
69
    }
70
71
    public function normalizeChildren($children)
0 ignored issues
show
Unused Code introduced by
The parameter $children is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
72
    {
73
    }
74
}
75