Completed
Pull Request — master (#290)
by Leny
06:14
created

ViewReferenceProvider::getReferencableViews()   D

Complexity

Conditions 10
Paths 14

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 31
rs 4.8197
cc 10
eloc 19
nc 14
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Victoire\Bundle\ViewReferenceBundle\Provider;
4
5
use Doctrine\ORM\EntityManager;
6
use Victoire\Bundle\BusinessPageBundle\Builder\BusinessPageBuilder;
7
use Victoire\Bundle\BusinessPageBundle\Entity\BusinessPage;
8
use Victoire\Bundle\BusinessPageBundle\Entity\BusinessTemplate;
9
use Victoire\Bundle\BusinessPageBundle\Helper\BusinessPageHelper;
10
use Victoire\Bundle\CoreBundle\Entity\View;
11
use Victoire\Bundle\CoreBundle\Entity\WebViewInterface;
12
use Victoire\Bundle\ViewReferenceBundle\Helper\ViewReferenceHelper;
13
14
/**
15
 * @property BusinessPageHelper businessPageHelper
16
 * @property BusinessPageBuilder businessPageBuilder
17
 * ref. victoire_view_reference.provider
18
 */
19
class ViewReferenceProvider
20
{
21
    protected $businessPageHelper;
22
    protected $businessPageBuilder;
23
24
    /**
25
     * @param BusinessPageHelper  $businessPageHelper
26
     * @param BusinessPageBuilder $businessPageBuilder
27
     */
28
    public function __construct(BusinessPageHelper $businessPageHelper, BusinessPageBuilder $businessPageBuilder)
29
    {
30
        $this->businessPageHelper = $businessPageHelper;
31
        $this->businessPageBuilder = $businessPageBuilder;
32
    }
33
34
    /**
35
     * @param View[]        $views
36
     * @param EntityManager $em
37
     *
38
     * @return WebViewInterface[]
39
     */
40
    public function getReferencableViews($views, EntityManager $em)
41
    {
42
        $referencableViews = [];
43
        if (!$views instanceof \Traversable && !is_array($views)) {
44
            $views = [$views];
45
        }
46
47
        $businessPages = $this->findBusinessPages($views);
48
        foreach ($views as $key => $view) {
49
            if ($view instanceof BusinessTemplate) {
50
                $entities = $this->businessPageHelper->getEntitiesAllowed($view, $em);
51
52
                // for each business entity
53
                foreach ($entities as $k => $entity) {
54
                    $currentPattern = clone $view;
55
                    $page = $this->businessPageBuilder->generateEntityPageFromTemplate($currentPattern, $entity, $em);
56
                    $this->businessPageBuilder->updatePageParametersByEntity($page, $entity);
0 ignored issues
show
Documentation introduced by
$page is of type object|array, but the function expects a object<Victoire\Bundle\B...le\Entity\BusinessPage>.

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...
57
                    if (!array_key_exists(ViewReferenceHelper::generateViewReferenceId($page, $entity), $businessPages)) {
0 ignored issues
show
Documentation introduced by
$page is of type object|array, 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...
58
                        $referencableViews[] = ['view' => $page];
59
                    }
60
                }
61
            } elseif ($view instanceof WebViewInterface) {
62
                $referencableViews[$key] = ['view' => $view];
63
            }
64
            if (isset($referencableViews[$key]) && $view->hasChildren()) {
65
                $referencableViews[$key]['children'] = $this->getReferencableViews($view->getChildren(), $em);
66
            }
67
        }
68
69
        return $referencableViews;
70
    }
71
72
    public function findBusinessPages($tree)
73
    {
74
        $businessPages = [];
75
        foreach ($tree as $key => $view) {
76
            if ($view instanceof BusinessPage) {
77
                $businessPages[ViewReferenceHelper::generateViewReferenceId($view, $view->getBusinessEntity())] = $view;
78
            }
79
            if ($view->hasChildren()) {
80
                self::findBusinessPages($view->getChildren());
81
            }
82
        }
83
84
        return $businessPages;
85
    }
86
}
87