Completed
Pull Request — master (#374)
by Leny
28:15 queued 21:44
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 Gedmo\Translatable\Entity\Repository\TranslationRepository;
7
use Gedmo\Translatable\Entity\Translation;
8
use Victoire\Bundle\BusinessPageBundle\Entity\BusinessPage;
9
use Victoire\Bundle\CoreBundle\Entity\View;
10
use Victoire\Bundle\CoreBundle\Entity\WebViewInterface;
11
use Victoire\Bundle\CoreBundle\Repository\ViewRepository;
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
            $viewReferences = [];
70
            /** @var TranslationRepository $viewRepo */
71
            $viewRepo = $entityManager->getRepository(Translation::class);
72
            foreach ($viewRepo->findTranslations($view) as $_locale => $translation) {
73
                $view->setTranslatableLocale($_locale);
74
                $entityManager->refresh($view);
75
                $viewReferences[$_locale] = $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...
76
            }
77
            $view->setReferences($viewReferences);
78
            if (!empty($branch['children'])) {
79
                /** @var WebViewInterface $children */
80
                $children = $branch['children'];
81
                $this->buildViewReferenceRecursively($children, $entityManager);
82
            }
83
        }
84
    }
85
}
86