Completed
Pull Request — master (#290)
by Leny
07:15
created

ViewReferenceHelper   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 10
c 5
b 1
f 0
lcom 1
cbo 4
dl 0
loc 73
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A generateViewReferenceId() 0 17 4
A buildXpath() 0 9 2
A buildViewReferenceRecursively() 0 13 3
1
<?php
2
3
namespace Victoire\Bundle\ViewReferenceBundle\Helper;
4
5
use Victoire\Bundle\BusinessPageBundle\Entity\BusinessPage;
6
use Victoire\Bundle\CoreBundle\Entity\View;
7
use Victoire\Bundle\CoreBundle\Entity\WebViewInterface;
8
use Victoire\Bundle\ViewReferenceBundle\Builder\ViewReferenceBuilder;
9
10
/**
11
 * ref: victoire_view_reference.helper.
12
 */
13
class ViewReferenceHelper
14
{
15
    /**
16
     * @var ViewReferenceBuilder
17
     */
18
    private $viewReferenceBuilder;
19
20
    /**
21
     * Constructor.
22
     *
23
     * @param ViewReferenceBuilder $viewReferenceBuilder
24
     */
25
    public function __construct(ViewReferenceBuilder $viewReferenceBuilder)
26
    {
27
        $this->viewReferenceBuilder = $viewReferenceBuilder;
28
    }
29
30
    /**
31
     * @param View  $view
32
     * @param mixed $entity
33
     *
34
     * @return string
35
     */
36
    public static function generateViewReferenceId(View $view, $entity = null)
37
    {
38
        $id = $view->getId();
39
        if ($view instanceof BusinessPage) {
40
            $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...
41
            $entity = $view->getBusinessEntity();
42
        } elseif (!$view instanceof WebViewInterface) {
43
            return $view->getId();
44
        }
45
46
        $refId = sprintf('ref_%s', $id);
47
        if ($entity) {
48
            $refId .= '_'.$entity->getId();
49
        }
50
51
        return $refId;
52
    }
53
54
    /**
55
     * @param array $parameters
56
     *
57
     * @return string
58
     */
59
    public static function buildXpath(array $parameters)
60
    {
61
        $arguments = [];
62
        foreach ($parameters as $key => $value) {
63
            $arguments[$key] = '@'.$key.'="'.$value.'"';
64
        }
65
66
        return '//viewReference['.implode(' and ', $arguments).']';
67
    }
68
69
    /**
70
     * @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...
71
     */
72
    public function buildViewReferenceRecursively($tree, $entityManager)
73
    {
74
        foreach ($tree as $branch) {
75
            /** @var WebViewInterface $view */
76
            $view = $branch['view'];
77
            $view->setReference($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...
78
            if (!empty($branch['children'])) {
79
                /** @var WebViewInterface $children */
80
                $children = $branch['children'];
81
                $this->buildViewReferenceRecursively($children, $entityManager);
82
            }
83
        }
84
    }
85
}
86