Completed
Pull Request — master (#374)
by Leny
06:07
created

ViewReferenceHelper::generateViewReferenceId()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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