Completed
Push — master ( 0eab77...b2f729 )
by Paul
05:35
created

ViewReferenceHelper   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 75
Duplicated Lines 6.67 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 4
dl 5
loc 75
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C generateViewReferenceId() 5 27 7
B buildViewReferenceRecursively() 0 20 6

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 Victoire\Bundle\ViewReferenceBundle\Helper;
4
5
use Doctrine\ORM\EntityManager;
6
use Symfony\Component\PropertyAccess\PropertyAccessor;
7
use Victoire\Bundle\BusinessPageBundle\Entity\BusinessPage;
8
use Victoire\Bundle\CoreBundle\Entity\View;
9
use Victoire\Bundle\CoreBundle\Entity\WebViewInterface;
10
use Victoire\Bundle\I18nBundle\Entity\ViewTranslation;
11
use Victoire\Bundle\ViewReferenceBundle\Builder\ViewReferenceBuilder;
12
13
/**
14
 * ref: victoire_view_reference.helper.
15
 */
16
class ViewReferenceHelper
17
{
18
    /**
19
     * @var ViewReferenceBuilder
20
     */
21
    private $viewReferenceBuilder;
22
23
    /**
24
     * Constructor.
25
     *
26
     * @param ViewReferenceBuilder $viewReferenceBuilder
27
     */
28
    public function __construct(ViewReferenceBuilder $viewReferenceBuilder)
29
    {
30
        $this->viewReferenceBuilder = $viewReferenceBuilder;
31
    }
32
33
    /**
34
     * @param View  $view
35
     * @param mixed $entity
0 ignored issues
show
Documentation introduced by
There is no parameter named $entity. Did you maybe mean $entityId?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
36
     *
37
     * @return string
38
     */
39
    public static function generateViewReferenceId(View $view, $entityId = null)
40
    {
41
        $id = $view->getId();
42
        if ($view instanceof BusinessPage && $view->getEntity()) {
43
            $id = $view->getTemplate()->getId();
0 ignored issues
show
Bug introduced by
The method getId cannot be called on $view->getTemplate() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
44
            $accessor = new PropertyAccessor();
45
            $entity = $view->getEntity();
46 View Code Duplication
            if (method_exists($entity, 'getId')) {
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...
47
                $entityId = $entity->getId();
48
            } else {
49
                $entityId = $accessor->getValue($entity, $view->getBusinessEntity()->getBusinessIdentifiers()->first()->getName());
0 ignored issues
show
Bug introduced by
The method getBusinessIdentifiers cannot be called on $view->getBusinessEntity() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
50
            }
51
        } elseif (!$view instanceof WebViewInterface) {
52
            return $view->getId();
53
        }
54
55
        $refId = sprintf('ref_%s', $id);
56
        if ($entityId) {
57
            $refId .= '_'.$entityId;
58
        }
59
        /** @var string $currentLocale */
60
        if ('' != $currentLocale = $view->getCurrentLocale()) {
61
            $refId .= '_'.$currentLocale;
62
        }
63
64
        return $refId;
65
    }
66
67
    /**
68
     * @param [] $tree
0 ignored issues
show
Documentation introduced by
The doc-type [] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
69
     */
70
    public function buildViewReferenceRecursively($tree, EntityManager $entityManager, $isRoot = true)
71
    {
72
        foreach ($tree as $branch) {
73
            /** @var View $view */
74
            $view = $branch['view'];
75
            /** @var ViewTranslation $translation */
76
            foreach ($view->getTranslations() as $translation) {
77
                if (true === $isRoot || $translation->getLocale() == $view->getParent()->getCurrentLocale()) {
78
                    $view->setCurrentLocale($translation->getLocale());
79
                    $viewReference = $this->viewReferenceBuilder->buildViewReference($view, $entityManager);
80
                    if (!empty($branch['children'])) {
81
                        /** @var WebViewInterface $children */
82
                        $children = $branch['children'];
83
                        $this->buildViewReferenceRecursively($children, $entityManager, false);
84
                    }
85
                    $view->setReference($viewReference, $translation->getLocale());
86
                }
87
            }
88
        }
89
    }
90
}
91