Completed
Pull Request — master (#374)
by Leny
32:39 queued 25:27
created

ViewReferenceHelper   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
lcom 1
cbo 6
dl 0
loc 69
rs 10
c 2
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B generateViewReferenceId() 0 20 5
A buildViewReferenceRecursively() 0 21 4
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\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 $entity
36
     *
37
     * @return string
38
     */
39
    public static function generateViewReferenceId(View $view, $entity = null)
40
    {
41
        $id = $view->getId();
42
        if ($view instanceof BusinessPage) {
43
            $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...
44
            $entity = $view->getBusinessEntity();
45
        } elseif (!$view instanceof WebViewInterface) {
46
            return $view->getId();
47
        }
48
49
        $refId = sprintf('ref_%s', $id);
50
        if ($entity) {
51
            $refId .= '_'.$entity->getId();
52
        }
53
        if ($view->getLocale() != '') {
54
            $refId .= '_'.$view->getLocale();
55
        }
56
57
        return $refId;
58
    }
59
60
    /**
61
     * @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...
62
     */
63
    public function buildViewReferenceRecursively($tree, EntityManager $entityManager)
64
    {
65
        foreach ($tree as $branch) {
66
            /** @var WebViewInterface $view */
67
            $view = $branch['view'];
68
            $viewReferences = [];
69
            /** @var TranslationRepository $viewRepo */
70
            $viewRepo = $entityManager->getRepository(Translation::class);
71
            foreach ($viewRepo->findTranslations($view) as $_locale => $translation) {
72
                $view->setTranslatableLocale($_locale);
73
                $entityManager->refresh($view);
74
                $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...
75
            }
76
            $view->setReferences($viewReferences);
77
            if (!empty($branch['children'])) {
78
                /** @var WebViewInterface $children */
79
                $children = $branch['children'];
80
                $this->buildViewReferenceRecursively($children, $entityManager);
81
            }
82
        }
83
    }
84
}
85