Completed
Push — master ( 5d51b4...87fdd9 )
by Paul
10s
created

buildViewReferenceRecursively()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 1
Metric Value
c 5
b 1
f 1
dl 0
loc 18
rs 9.2
cc 4
eloc 11
nc 5
nop 2
1
<?php
2
3
namespace Victoire\Bundle\ViewReferenceBundle\Helper;
4
5
use Doctrine\ORM\EntityManager;
6
use Victoire\Bundle\BusinessPageBundle\Entity\BusinessPage;
7
use Victoire\Bundle\CoreBundle\Entity\View;
8
use Victoire\Bundle\CoreBundle\Entity\WebViewInterface;
9
use Victoire\Bundle\ViewReferenceBundle\Builder\ViewReferenceBuilder;
10
11
/**
12
 * ref: victoire_view_reference.helper.
13
 */
14
class ViewReferenceHelper
15
{
16
    /**
17
     * @var ViewReferenceBuilder
18
     */
19
    private $viewReferenceBuilder;
20
21
    /**
22
     * Constructor.
23
     *
24
     * @param ViewReferenceBuilder $viewReferenceBuilder
25
     */
26
    public function __construct(ViewReferenceBuilder $viewReferenceBuilder)
27
    {
28
        $this->viewReferenceBuilder = $viewReferenceBuilder;
29
    }
30
31
    /**
32
     * @param View  $view
33
     * @param mixed $entity
34
     *
35
     * @return string
36
     */
37
    public static function generateViewReferenceId(View $view, $entity = null)
38
    {
39
        $id = $view->getId();
40
        if ($view instanceof BusinessPage) {
41
            $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...
42
            $entity = $view->getBusinessEntity();
43
        } elseif (!$view instanceof WebViewInterface) {
44
            return $view->getId();
45
        }
46
47
        $refId = sprintf('ref_%s', $id);
48
        if ($entity) {
49
            $refId .= '_'.$entity->getId();
50
        }
51
        if ($view->getCurrentLocale() != '') {
52
            $refId .= '_'.$view->getCurrentLocale();
53
        }
54
55
        return $refId;
56
    }
57
58
    /**
59
     * @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...
60
     */
61
    public function buildViewReferenceRecursively($tree, EntityManager $entityManager)
62
    {
63
        foreach ($tree as $branch) {
64
            /** @var WebViewInterface $view */
65
            $view = $branch['view'];
66
            $viewReferences = [];
67
            foreach ($view->getTranslations() as $translation) {
68
                $view->setCurrentLocale($translation->getLocale());
69
                $viewReferences[$translation->getLocale()] = $this->viewReferenceBuilder->buildViewReference($view, $entityManager);
0 ignored issues
show
Documentation introduced by
$view is of type object<Victoire\Bundle\C...ntity\WebViewInterface>, but the function expects a object<Victoire\Bundle\CoreBundle\Entity\View>.

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...
70
            }
71
            $view->setReferences($viewReferences);
72
            if (!empty($branch['children'])) {
73
                /** @var WebViewInterface $children */
74
                $children = $branch['children'];
75
                $this->buildViewReferenceRecursively($children, $entityManager);
76
            }
77
        }
78
    }
79
}
80