buildViewReferenceRecursively()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
rs 8.8571
cc 6
eloc 11
nc 5
nop 3
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 $entityId
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();
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());
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
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$entityManager" missing
Loading history...
introduced by
Doc comment for parameter "$isRoot" missing
Loading history...
68
     * @param WebViewInterface[] $tree
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);
0 ignored issues
show
Documentation introduced by
$children is of type object<Victoire\Bundle\C...ntity\WebViewInterface>, but the function expects a array<integer,object<Vic...tity\WebViewInterface>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
84
                    }
85
                    $view->setReference($viewReference, $translation->getLocale());
86
                }
87
            }
88
        }
89
    }
90
}
91