Completed
Push — master ( cba6b5...9256c7 )
by Paul
06:29
created

setApiBusinessEntityResolver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Victoire\Bundle\WidgetBundle\Resolver;
4
5
use Doctrine\ORM\EntityManager;
6
use Symfony\Component\PropertyAccess\PropertyAccess;
7
use Symfony\Component\PropertyAccess\PropertyAccessor;
8
use Victoire\Bundle\APIBusinessEntityBundle\Entity\APIBusinessEntity;
9
use Victoire\Bundle\APIBusinessEntityBundle\Resolver\APIBusinessEntityResolver;
10
use Victoire\Bundle\QueryBundle\Helper\QueryHelper;
11
use Victoire\Bundle\WidgetBundle\Model\Widget;
12
13
class BaseWidgetContentResolver
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
14
{
15
    /**
16
     * @var QueryHelper
17
     */
18
    protected $queryHelper;
19
20
    /**
21
     * @var EntityManager
22
     */
23
    protected $entityManager;
24
25
    /**
26
     * @var APIBusinessEntityResolver
27
     */
28
    protected $apiResolver;
29
30
    /**
31
     * Get the static content of the widget.
32
     *
33
     * @param Widget $widget
34
     *
35
     * @return string
36
     */
37
    public function getWidgetStaticContent(Widget $widget)
38
    {
39
        $reflect = new \ReflectionClass($widget);
40
        $widgetProperties = $reflect->getProperties();
41
        $parameters = ['widget' => $widget];
42
        $accessor = PropertyAccess::createPropertyAccessor();
43
44
        foreach ($widgetProperties as $property) {
45
            if (!$property->isStatic()) {
46
                $value = $accessor->getValue($widget, $property->getName());
47
                $parameters[$property->getName()] = $value;
48
            }
49
        }
50
51
        return $parameters;
52
    }
53
54
    /**
55
     * Get the business entity content.
56
     *
57
     * @param Widget $widget
58
     *
59
     * @return string
60
     */
61 View Code Duplication
    public function getWidgetBusinessEntityContent(Widget $widget)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
    {
63
        $entity = $widget->getEntity();
64
        $parameters = $this->getWidgetStaticContent($widget);
65
66
        $this->populateParametersWithWidgetFields($widget, $entity, $parameters);
67
68
        return $parameters;
69
    }
70
71
    /**
72
     * Get the content of the widget by the entity linked to it.
73
     *
74
     * @param Widget $widget
75
     *
76
     * @return string
77
     */
78 View Code Duplication
    public function getWidgetEntityContent(Widget $widget)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
    {
80
        $entity = $widget->getEntity();
81
82
        $parameters = $this->getWidgetStaticContent($widget);
83
84
        $this->populateParametersWithWidgetFields($widget, $entity, $parameters);
85
86
        return $parameters;
87
    }
88
89
    /**
90
     * Get the content of the widget for the query mode.
91
     *
92
     * @param Widget $widget
93
     *
94
     * @return string
95
     */
96
    public function getWidgetQueryContent(Widget $widget)
97
    {
98
        $parameters = $this->getWidgetStaticContent($widget);
99
100
        if (APIBusinessEntity::TYPE === $widget->getBusinessEntity()->getType()) {
101
            $entity = $this->apiResolver->getBusinessEntities($widget->getBusinessEntity());
102
        } else {
103
            $entity = $this->getWidgetQueryBuilder($widget)
104
                            ->setMaxResults(1)
105
                            ->getQuery()
106
                            ->getOneOrNullResult();
107
        }
108
109
        $this->populateParametersWithWidgetFields($widget, $entity, $parameters);
110
111
        return $parameters;
112
    }
113
114
    /**
115
     * Get the widget query result.
116
     *
117
     * @param Widget $widget The widget
118
     *
119
     * @return \Doctrine\ORM\QueryBuilder The list of entities
120
     */
121
    public function getWidgetQueryBuilder(Widget $widget)
122
    {
123
        //get the base query
124
        $itemsQueryBuilder = $this->queryHelper->getQueryBuilder($widget, $this->entityManager);
0 ignored issues
show
Documentation introduced by
$widget is of type object<Victoire\Bundle\WidgetBundle\Model\Widget>, but the function expects a object<Victoire\Bundle\Q...VictoireQueryInterface>.

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...
125
126
        // Filter only visibleOnFront
127
        $itemsQueryBuilder->andWhere('main_item.visibleOnFront = true');
128
129
        //add the query of the widget
130
        return $this->queryHelper->buildWithSubQuery($widget, $itemsQueryBuilder, $this->entityManager);
0 ignored issues
show
Documentation introduced by
$widget is of type object<Victoire\Bundle\WidgetBundle\Model\Widget>, but the function expects a object<Victoire\Bundle\Q...VictoireQueryInterface>.

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...
131
    }
132
133
    protected function populateParametersWithWidgetFields(Widget $widget, $entity, &$parameters)
134
    {
135
        $fields = $widget->getFields();
136
        //parse the field
137
        foreach ($fields as $widgetField => $field) {
138
            //get the value of the field
139
            if ($entity !== null) {
140
                $accessor = new PropertyAccessor();
141
                $attributeValue = $accessor->getValue($entity, $field);
142
            } else {
143
                $attributeValue = $widget->getBusinessEntityName().' -> '.$field;
144
            }
145
146
            $parameters[$widgetField] = $attributeValue;
147
        }
148
149
        $widget->setEntity($entity);
150
    }
151
152
    /**
153
     * @param QueryHelper $queryHelper
154
     */
155
    public function setQueryHelper(QueryHelper $queryHelper)
0 ignored issues
show
introduced by
Declare public methods first,then protected ones and finally private ones
Loading history...
156
    {
157
        $this->queryHelper = $queryHelper;
158
    }
159
160
    /**
161
     * @param EntityManager $entityManager
162
     */
163
    public function setEntityManager(EntityManager $entityManager)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $entityManager. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
164
    {
165
        $this->entityManager = $entityManager;
166
    }
167
168
    /**
169
     * @param APIBusinessEntityResolver $resolver
170
     */
171
    public function setApiBusinessEntityResolver(APIBusinessEntityResolver $resolver)
172
    {
173
        $this->apiResolver = $resolver;
174
    }
175
}
176