Completed
Push — master ( 9745cb...81e988 )
by Paul
06:43
created

Widget::generateCacheId()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 20
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 12
nc 2
nop 0
1
<?php
2
3
namespace Victoire\Bundle\WidgetBundle\Entity;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\ORM\Mapping as ORM;
7
use Gedmo\Timestampable\Traits\TimestampableEntity;
8
use Victoire\Bundle\CoreBundle\Entity\BaseEntityProxy;
9
use Victoire\Bundle\CriteriaBundle\Entity\Criteria;
10
use Victoire\Bundle\QueryBundle\Entity\QueryTrait;
11
use Victoire\Bundle\QueryBundle\Entity\VictoireQueryInterface;
12
use Victoire\Bundle\WidgetBundle\Entity\Traits\StyleTrait;
13
use Victoire\Bundle\WidgetBundle\Model\Widget as BaseWidget;
14
use Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap;
15
16
/**
17
 * Widget.
18
 *
19
 * @ORM\Table("vic_widget")
20
 * @ORM\Entity(repositoryClass="Victoire\Bundle\WidgetBundle\Repository\WidgetRepository")
21
 * @ORM\InheritanceType("JOINED")
22
 * @ORM\DiscriminatorColumn(name="type", type="string")
23
 */
24
class Widget extends BaseWidget implements VictoireQueryInterface
25
{
26
    use StyleTrait;
27
    use QueryTrait;
28
    use TimestampableEntity;
29
30
    public function __construct()
31
    {
32
        $this->childrenSlot = uniqid();
33
        $this->criterias = new ArrayCollection();
34
    }
35
36
    /**
37
     * @var int
38
     *
39
     * @ORM\Column(name="id", type="integer")
40
     * @ORM\Id
41
     * @ORM\GeneratedValue(strategy="AUTO")
42
     */
43
    protected $id;
44
45
    /**
46
     * @var string
47
     *
48
     * @ORM\Column(name="slot", type="string", length=255, nullable=true)
49
     */
50
    protected $slot;
51
52
    /**
53
     * @var string
54
     *
55
     * @ORM\Column(name="childrenSlot", type="string", length=100, nullable=true)
56
     */
57
    protected $childrenSlot;
58
59
    /**
60
     * @var string
61
     *
62
     * @ORM\Column(name="theme", type="string", length=255, nullable=true)
63
     */
64
    protected $theme;
65
66
    /**
67
     * @var string
68
     *
69
     * @ORM\Column(name="asynchronous", type="boolean", nullable=true)
70
     */
71
    protected $asynchronous;
72
73
    /**
74
     * @var string
75
     *
76
     * @ORM\Column(name="fields", type="array")
77
     */
78
    protected $fields = [];
79
80
    /**
81
     * @var string
82
     *
83
     * @ORM\Column(name="mode", type="string", length=255, nullable=false)
84
     */
85
    protected $mode = self::MODE_STATIC;
86
87
    /**
88
     * Auto simple mode: joined entity.
89
     *
90
     * @var EntityProxy
91
     *
92
     * @ORM\ManyToOne(targetEntity="\Victoire\Bundle\CoreBundle\Entity\EntityProxy", inversedBy="widgets", cascade={"persist"})
93
     * @ORM\JoinColumn(name="entityProxy_id", referencedColumnName="id", onDelete="CASCADE")
94
     */
95
    protected $entityProxy;
96
97
    /**
98
     * The entity linked to the widget.
99
     *
100
     * @var unknown
101
     */
102
    protected $entity;
103
104
    /**
105
     * @deprecated
106
     *
107
     * @var string
108
     *
109
     * @ORM\ManyToOne(targetEntity="\Victoire\Bundle\CoreBundle\Entity\View", inversedBy="widgets", cascade={"persist"})
110
     * @ORM\JoinColumn(name="view_id", referencedColumnName="id", onDelete="CASCADE")
111
     */
112
    protected $view;
113
    /**
114
     * @var WidgetMap
115
     *
116
     * @ORM\ManyToOne(targetEntity="\Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap", inversedBy="widgets")
117
     * @ORM\JoinColumn(name="widget_map_id", referencedColumnName="id", onDelete="SET NULL"))
118
     */
119
    protected $widgetMap;
120
121
    /**
122
     * @var [Criteria]
123
     *
124
     * @ORM\OneToMany(targetEntity="\Victoire\Bundle\CriteriaBundle\Entity\Criteria", mappedBy="widget", cascade={"persist", "remove"}, orphanRemoval=true)
125
     */
126
    protected $criterias;
127
128
    /**
129
     * @var string
130
     *
131
     * @ORM\Column(name="quantum", type="string", length=255, nullable=true)
132
     */
133
    protected $quantum;
134
135
    /**
136
     * @return string
137
     */
138
    public function isAsynchronous()
139
    {
140
        return $this->asynchronous;
141
    }
142
143
    /**
144
     * @param string $asynchronous
145
     */
146
    public function setAsynchronous($asynchronous)
147
    {
148
        $this->asynchronous = $asynchronous;
149
    }
150
151
    /**
152
     * Set the entity proxy.
153
     *
154
     * @param BaseEntityProxy $entityProxy
155
     */
156
    public function setEntityProxy(BaseEntityProxy $entityProxy)
157
    {
158
        $this->entityProxy = $entityProxy;
0 ignored issues
show
Documentation Bug introduced by
It seems like $entityProxy of type object<Victoire\Bundle\C...Entity\BaseEntityProxy> is incompatible with the declared type object<Victoire\Bundle\W...dle\Entity\EntityProxy> of property $entityProxy.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
159
    }
160
161
    /**
162
     * Get the entity proxy.
163
     *
164
     * @return EntityProxy
165
     */
166
    public function getEntityProxy()
167
    {
168
        return $this->entityProxy;
169
    }
170
171
    /**
172
     * to string.
173
     *
174
     * @return string
175
     */
176
    public function __toString()
177
    {
178
        return (string) $this->getId();
179
    }
180
181
    /**
182
     * Get id.
183
     *
184
     * @return int
185
     */
186
    public function getId()
187
    {
188
        return $this->id;
189
    }
190
191
    /**
192
     * Set fields.
193
     *
194
     * @param string $fields
195
     *
196
     * @return Widget
197
     */
198
    public function setFields($fields)
199
    {
200
        $this->fields = $fields;
201
202
        return $this;
203
    }
204
205
    /**
206
     * Get fields.
207
     *
208
     * @return string
209
     */
210
    public function getFields()
211
    {
212
        return $this->fields;
213
    }
214
215
    /**
216
     * Set The Id.
217
     *
218
     * @param int $id The id
219
     */
220
    public function setId($id)
221
    {
222
        $this->id = $id;
223
    }
224
225
    /**
226
     * Set slot.
227
     *
228
     * @param string $slot
229
     *
230
     * @return Widget
231
     */
232
    public function setSlot($slot)
233
    {
234
        $this->slot = $slot;
235
236
        return $this;
237
    }
238
239
    /**
240
     * Get slot.
241
     *
242
     * @return string
243
     */
244
    public function getSlot()
245
    {
246
        return $this->slot;
247
    }
248
249
    /**
250
     * Set theme.
251
     *
252
     * @param string $theme
253
     *
254
     * @return Widget
255
     */
256
    public function setTheme($theme)
257
    {
258
        $this->theme = $theme;
259
260
        return $this;
261
    }
262
263
    /**
264
     * Get theme.
265
     *
266
     * @return string
267
     */
268
    public function getTheme()
269
    {
270
        return $this->theme;
271
    }
272
273
    /**
274
     * Get the content.
275
     *
276
     * @return unknown
277
     */
278
    public function getValue()
279
    {
280
        //return $this->getContent();
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
281
    }
282
283
    /**
284
     * Get the type of the object.
285
     *
286
     * @return string The type
287
     */
288
    public function getType()
289
    {
290
        return $this->guessType();
291
    }
292
293
    /**
294
     * Guess the type of this by exploding and getting the last item.
295
     *
296
     * @return string The guessed type
297
     */
298
    protected function guessType()
299
    {
300
        $type = explode('\\', get_class($this));
301
302
        return strtolower(preg_replace('/Widget/', '', end($type)));
303
    }
304
305
    /**
306
     * Set the mode.
307
     *
308
     * @param string $mode
309
     */
310
    public function setMode($mode)
311
    {
312
        $this->mode = $mode;
313
    }
314
315
    /**
316
     * Get the mode.
317
     *
318
     * @return string
319
     */
320
    public function getMode()
321
    {
322
        return $this->mode;
323
    }
324
325
    /**
326
     * Get the view id.
327
     *
328
     * @return int The view id
329
     */
330
    public function getViewId()
331
    {
332
        $viewId = null;
333
334
        $view = $this->getView();
0 ignored issues
show
Deprecated Code introduced by
The method Victoire\Bundle\WidgetBu...ntity\Widget::getView() has been deprecated with message: Get view.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
335
336
        if ($view !== null) {
337
            $viewId = $view->getId();
0 ignored issues
show
Bug introduced by
The method getId cannot be called on $view (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...
338
        }
339
340
        return $viewId;
341
    }
342
343
    /**
344
     * @return string
345
     */
346
    public function getChildrenSlot()
347
    {
348
        return $this->childrenSlot ?: $this->getId();
349
    }
350
351
    /**
352
     * @param string $childrenSlot
353
     */
354
    public function setChildrenSlot($childrenSlot)
355
    {
356
        $this->childrenSlot = $childrenSlot;
357
    }
358
359
    /**
360
     * Set widgets.
361
     *
362
     * @param [WidgetMap] $widgetMaps
0 ignored issues
show
Documentation introduced by
The doc-type [WidgetMap] 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...
Documentation introduced by
There is no parameter named $widgetMaps. Did you maybe mean $widgetMap?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
363
     *
364
     * @return Widget
365
     */
366
    public function setWidgetMap(WidgetMap $widgetMap)
367
    {
368
        $this->widgetMap = $widgetMap;
369
        $widgetMap->addWidget($this);
370
371
        return $this;
372
    }
373
374
    /**
375
     * Get widgets.
376
     *
377
     * @return [WidgetMap]
0 ignored issues
show
Documentation introduced by
The doc-type [WidgetMap] 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...
378
     */
379
    public function getWidgetMap()
380
    {
381
        return $this->widgetMap;
382
    }
383
384
    /**
385
     * Set the entity.
386
     *
387
     * @param object $entity
388
     */
389
    public function setEntity($entity)
390
    {
391
        $this->entity = $entity;
392
    }
393
394
    /**
395
     * Get the entity.
396
     *
397
     * @return number
398
     */
399
    public function getEntity()
400
    {
401
        //if there is no entity
402
        if ($this->entity === null) {
403
            //we try to get one from the proxy
404
            $entityProxy = $this->getEntityProxy();
405
406
            //if there is a proxy
407
            if ($entityProxy !== null && $this->getBusinessEntityId()) {
408
                $entity = $entityProxy->getEntity($this->getBusinessEntityId());
409
                $this->entity = $entity;
410
            }
411
        }
412
413
        return $this->entity;
414
    }
415
416
    /**
417
     * Clone a widget.
418
     */
419
    public function __clone()
420
    {
421
        // if there is a proxy
422
        if ($this->entityProxy) {
423
            // we clone this one
424
            $this->entityProxy = clone $this->entityProxy;
425
        }
426
427
        // This check should be in the __constructor, but Doctrine does not use __constructor to
428
        // instanciate entites but __clone method.
429
        if (property_exists(get_called_class(), 'widget')) {
430
            throw new \Exception(sprintf('A property $widget was found in %s object.
431
                The $widget property is reserved for Victoire.
432
                You should chose a different property name.', get_called_class()));
433
        }
434
    }
435
436
    /**
437
     * @deprecated
438
     * Get view.
439
     *
440
     * @return string
441
     */
442
    public function getView()
443
    {
444
        return $this->view;
0 ignored issues
show
Deprecated Code introduced by
The property Victoire\Bundle\WidgetBundle\Entity\Widget::$view has been deprecated.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
445
    }
446
447
    /**
448
     * @return [Criteria]
0 ignored issues
show
Documentation introduced by
The doc-type [Criteria] 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...
449
     */
450
    public function getCriterias()
451
    {
452
        return $this->criterias;
453
    }
454
455
    /**
456
     * @param [Criteria] $criterias
0 ignored issues
show
Documentation introduced by
The doc-type [Criteria] 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...
457
     */
458
    public function setCriterias($criterias)
459
    {
460
        $this->criterias = $criterias;
461
    }
462
463
    /**
464
     * @param Criteria $criteria
465
     */
466
    public function addCriteria($criteria)
467
    {
468
        $criteria->setWidget($this);
469
        $this->criterias[] = $criteria;
470
    }
471
472
    /**
473
     * @param Criteria $criteria
474
     */
475
    public function removeCriteria(Criteria $criteria)
476
    {
477
        $criteria->setWidget(null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object<Victoire\Bundle\W...etBundle\Entity\Widget>.

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...
478
        $this->criterias->removeElement($criteria);
479
    }
480
481
    /**
482
     * @param Criteria $criteria
483
     *
484
     * @return bool
485
     */
486
    public function hasCriteria(Criteria $criteria)
487
    {
488
        return $this->criterias->contains($criteria);
489
    }
490
491
    /**
492
     * @param $criteriaAlias
493
     *
494
     * @return bool
495
     */
496
    public function hasCriteriaNamed($criteriaAlias)
497
    {
498
        return $this->criterias->exists(function ($key, $element) use ($criteriaAlias) {
499
            return $criteriaAlias === $element->getName();
500
        });
501
    }
502
503
    /**
504
     * @return string
505
     */
506
    public function getQuantum()
507
    {
508
        return $this->quantum;
509
    }
510
511
    /**
512
     * @param string $quantum
513
     */
514
    public function setQuantum($quantum)
515
    {
516
        $this->quantum = $quantum;
517
    }
518
519
    /**
520
     * Generate the CacheId, insert params that can invalid the cache.
521
     *
522
     * @return string
523
     */
524
    public function generateCacheId()
525
    {
526
        if (!$this->getCurrentView()) {
527
            throw new \Exception(sprintf('Cannot generate an hash for widget %s if currentView is not defined.',
528
                $this->getId()
529
            ));
530
            if (!$this->getCurrentView()->getReference()) {
0 ignored issues
show
Unused Code introduced by
if (!$this->getCurrentVi...entView()->getId())); } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
Bug introduced by
The variable $this seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
531
                throw new \Exception(sprintf('Cannot generate an hash for widget %s if currentView %s Reference is not defined.',
532
                    $this->getId(),
533
                    $widget->getCurrentView()->getId()
534
                ));
535
            }
536
        }
537
538
        return sprintf('%s-%s-%s',
539
            $this->getId(),
540
            $this->getUpdatedAt()->getTimestamp(),
541
            $this->getCurrentView()->getReference()->getId()
542
        );
543
    }
544
}
545