Completed
Push — master ( 0eab77...b2f729 )
by Paul
05:35
created

ViewReferenceProvider::getReferencableViews()   C

Complexity

Conditions 11
Paths 10

Size

Total Lines 39
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 26
nc 10
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 Symfony\Component\PropertyAccess\PropertyAccessor;
7
use Victoire\Bundle\APIBusinessEntityBundle\Entity\APIBusinessEntity;
8
use Victoire\Bundle\BusinessPageBundle\Builder\BusinessPageBuilder;
9
use Victoire\Bundle\BusinessPageBundle\Entity\BusinessPage;
10
use Victoire\Bundle\BusinessPageBundle\Entity\BusinessTemplate;
11
use Victoire\Bundle\BusinessPageBundle\Helper\BusinessPageHelper;
12
use Victoire\Bundle\CoreBundle\Entity\View;
13
use Victoire\Bundle\CoreBundle\Entity\WebViewInterface;
14
use Victoire\Bundle\ViewReferenceBundle\Helper\ViewReferenceHelper;
15
16
/**
17
 * @property BusinessPageHelper businessPageHelper
18
 * @property BusinessPageBuilder businessPageBuilder
19
 * ref. victoire_view_reference.provider
20
 */
21
class ViewReferenceProvider
22
{
23
    protected $businessPageHelper;
24
    protected $businessPageBuilder;
25
26
    /**
27
     * @param BusinessPageHelper  $businessPageHelper
28
     * @param BusinessPageBuilder $businessPageBuilder
29
     */
30
    public function __construct(BusinessPageHelper $businessPageHelper, BusinessPageBuilder $businessPageBuilder)
31
    {
32
        $this->businessPageHelper = $businessPageHelper;
33
        $this->businessPageBuilder = $businessPageBuilder;
34
    }
35
36
    /**
37
     * @param View[]        $views
38
     * @param EntityManager $em
39
     *
40
     * @return WebViewInterface[]
41
     */
42
    public function getReferencableViews($views, EntityManager $em)
43
    {
44
        $referencableViews = [];
45
        if (!$views instanceof \Traversable && !is_array($views)) {
46
            $views = [$views];
47
        }
48
49
        $businessPages = $this->findBusinessPages($views);
50
        foreach ($views as $view) {
51
            if ($view instanceof BusinessTemplate) {
52
                $entities = $this->businessPageHelper->getEntitiesAllowed($view, $em);
53
54
                // for each business entity
55
                foreach ($entities as $k => $entity) {
0 ignored issues
show
Bug introduced by
The expression $entities of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
56
                    $currentTemplate = clone $view;
57
                    $page = $this->businessPageBuilder->generateEntityPageFromTemplate($currentTemplate, $entity, $em);
58
                    $this->businessPageBuilder->updatePageParametersByEntity($page, $entity);
59
                    $entityId = null;
60
                    if (method_exists($entity, 'getId')) {
61
                        $entityId = $entity->getId();
62
                    } elseif ($page->getBusinessEntity()->getType() === APIBusinessEntity::TYPE) {
0 ignored issues
show
Bug introduced by
The method getType cannot be called on $page->getBusinessEntity() (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...
63
                        $accessor = new PropertyAccessor();
64
                        $entityId = $accessor->getValue($entity, $page->getBusinessEntity()->getBusinessIdentifiers()->first()->getName());
0 ignored issues
show
Bug introduced by
The method getBusinessIdentifiers cannot be called on $page->getBusinessEntity() (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...
65
                    }
66
                    if (!array_key_exists(ViewReferenceHelper::generateViewReferenceId($page, $entityId), $businessPages)) {
67
                        $referencableViews[] = ['view' => $page];
68
                    }
69
                }
70
            } elseif ($view instanceof WebViewInterface) {
71
                $referencableView = ['view' => $view];
72
                if ($view->getChildren()) {
73
                    $referencableView['children'] = $this->getReferencableViews($view->getChildren(), $em);
74
                }
75
                $referencableViews[] = $referencableView;
76
            }
77
        }
78
79
        return $referencableViews;
80
    }
81
82
    public function findBusinessPages($tree)
83
    {
84
        $businessPages = [];
85
        foreach ($tree as $view) {
86
            if ($view instanceof BusinessPage) {
87
                $businessPages[ViewReferenceHelper::generateViewReferenceId($view, $view->getEntity())] = $view;
88
            }
89
            if ($view->hasChildren()) {
90
                self::findBusinessPages($view->getChildren());
91
            }
92
        }
93
94
        return $businessPages;
95
    }
96
}
97