Completed
Push — master ( 9745cb...81e988 )
by Paul
06:43
created

buildViewReferenceRecursively()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 1 Features 2
Metric Value
c 6
b 1
f 2
dl 0
loc 23
rs 8.5906
cc 5
eloc 14
nc 5
nop 2
1
<?php
2
3
namespace Victoire\Bundle\ViewReferenceBundle\Helper;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\EntityRepository;
7
use Victoire\Bundle\BusinessPageBundle\Entity\BusinessPage;
8
use Victoire\Bundle\BusinessPageBundle\Entity\VirtualBusinessPage;
9
use Victoire\Bundle\CoreBundle\Entity\View;
10
use Victoire\Bundle\CoreBundle\Entity\WebViewInterface;
11
use Victoire\Bundle\I18nBundle\Entity\ViewTranslation;
12
use Victoire\Bundle\ViewReferenceBundle\Builder\ViewReferenceBuilder;
13
14
/**
15
 * ref: victoire_view_reference.helper.
16
 */
17
class ViewReferenceHelper
18
{
19
    /**
20
     * @var ViewReferenceBuilder
21
     */
22
    private $viewReferenceBuilder;
23
24
    /**
25
     * Constructor.
26
     *
27
     * @param ViewReferenceBuilder $viewReferenceBuilder
28
     */
29
    public function __construct(ViewReferenceBuilder $viewReferenceBuilder)
30
    {
31
        $this->viewReferenceBuilder = $viewReferenceBuilder;
32
    }
33
34
    /**
35
     * @param View  $view
36
     * @param mixed $entity
37
     *
38
     * @return string
39
     */
40
    public static function generateViewReferenceId(View $view, $entity = null)
41
    {
42
        $id = $view->getId();
43
        if ($view instanceof BusinessPage) {
44
            $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...
45
            $entity = $view->getBusinessEntity();
46
        } elseif (!$view instanceof WebViewInterface) {
47
            return $view->getId();
48
        }
49
50
        $refId = sprintf('ref_%s', $id);
51
        if ($entity) {
52
            $refId .= '_'.$entity->getId();
53
        }
54
        if ($view->getLocale() != '') {
55
            $refId .= '_'.$view->getLocale();
56
        }
57
58
        return $refId;
59
    }
60
61
    /**
62
     * @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...
63
     */
64
    public function buildViewReferenceRecursively($tree, EntityManager $entityManager)
65
    {
66
        foreach ($tree as $branch) {
67
            /** @var WebViewInterface $view */
68
            $view = $branch['view'];
69
            if (!$view instanceof VirtualBusinessPage) {
70
                $viewReferences = [];
71
                /* @var EntityRepository $viewRepo */
72
                $viewTranslationRepo = $entityManager->getRepository(ViewTranslation::class);
73
                foreach ($viewTranslationRepo->findByObject($view) as $translation) {
74
                    $view->setTranslatableLocale($translation->getLocale());
75
                    $entityManager->refresh($view);
76
                    $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...
77
                }
78
                $view->setReferences($viewReferences);
79
            }
80
            if (!empty($branch['children'])) {
81
                /** @var WebViewInterface $children */
82
                $children = $branch['children'];
83
                $this->buildViewReferenceRecursively($children, $entityManager);
84
            }
85
        }
86
    }
87
}
88