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

BusinessEntityHelper::createBusinessEntity()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 15
nc 3
nop 2
1
<?php
2
3
namespace Victoire\Bundle\BusinessEntityBundle\Helper;
4
5
use Doctrine\ORM\EntityRepository;
6
use Victoire\Bundle\BusinessEntityBundle\Entity\BusinessEntity;
7
use Victoire\Bundle\BusinessEntityBundle\Entity\BusinessEntityRepository;
8
use Victoire\Bundle\BusinessEntityBundle\Entity\BusinessProperty;
9
use Victoire\Bundle\BusinessEntityBundle\Reader\BusinessEntityCacheReader;
10
use Victoire\Bundle\CoreBundle\Cache\Builder\CacheBuilder;
11
use Victoire\Bundle\ORMBusinessEntityBundle\Entity\ORMBusinessEntityRepository;
12
13
/**
14
 * The BusinessEntityHelper.
15
 *
16
 * ref: victoire_core.helper.business_entity_helper
17
 */
18
class BusinessEntityHelper
19
{
20
    /**
21
     * @var BusinessEntityRepository
22
     */
23
    private $businessEntityRepository;
24
    /**
25
     * @var ORMBusinessEntityRepository
26
     */
27
    private $ormBusinessEntityRepository;
28
    /**
29
     * @var BusinessEntityCacheReader
30
     */
31
    private $cacheReader;
32
33
    /**
34
     * Constructor.
35
     *
36
     * @param EntityRepository|BusinessEntityRepository $businessEntityRepository
37
     * @param EntityRepository                          $ormBusinessEntityRepository
38
     * @param BusinessEntityCacheReader                 $cacheReader
39
     *
40
     * @internal param BusinessEntityCacheReader $reader
41
     * @internal param CacheBuilder $builder
42
     */
43
    public function __construct(EntityRepository $businessEntityRepository, EntityRepository $ormBusinessEntityRepository, BusinessEntityCacheReader $cacheReader)
44
    {
45
        $this->ormBusinessEntityRepository = $ormBusinessEntityRepository;
0 ignored issues
show
Documentation Bug introduced by
$ormBusinessEntityRepository is of type object<Doctrine\ORM\EntityRepository>, but the property $ormBusinessEntityRepository was declared to be of type object<Victoire\Bundle\O...sinessEntityRepository>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
46
        $this->businessEntityRepository = $businessEntityRepository;
0 ignored issues
show
Documentation Bug introduced by
$businessEntityRepository is of type object<Doctrine\ORM\EntityRepository>, but the property $businessEntityRepository was declared to be of type object<Victoire\Bundle\B...sinessEntityRepository>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
47
        $this->cacheReader = $cacheReader;
48
    }
49
50
    /**
51
     * Get a business entity.q.
52
     *
53
     * @param mixed $entity
54
     *
55
     * @return BusinessEntity
56
     */
57
    public function findByEntityInstance($entity)
58
    {
59
        $businessEntity = null;
60
        $class = new \ReflectionClass($entity);
61
        while (!$businessEntity && $class && $class->name !== null) {
62
            $businessEntity = $this->ormBusinessEntityRepository->findOneBy(['class' => $class->name]);
63
            $class = $class->getParentClass();
64
        }
65
66
        return $businessEntity;
67
    }
68
69
    /**
70
     * Get a business entity by classname.
71
     *
72
     * @param string $classname
73
     *
74
     * @return BusinessEntity
75
     */
76
    public function findByEntityClassname($classname)
77
    {
78
        return $this->ormBusinessEntityRepository->findOneBy(['class' => $classname]);
79
    }
80
81
    public function getAvailableForWidget($widgetName)
82
    {
83
        $classes = $this->businessEntityRepository->getByAvailableWidgets($widgetName);
84
85
        $receiverProperties = $this->cacheReader->getReceiverProperties($widgetName);
86
        foreach ($classes as $businessEntity) {
87
            $businessProperties = $businessEntity->getBusinessProperties();
88
            foreach ($receiverProperties as $receiverType => $receiverProperty) {
89
                $businessEntity->setDisable(true);
90
                if (count($businessProperties) > 0) {
91
                    /** @var BusinessProperty $businessProperty */
92
                    foreach ($businessProperties as $businessProperty) {
93
                        if (in_array($receiverType, $businessProperty->getTypes())) {
94
                            $businessEntity->setDisable(false);
95
                        }
96
                    }
97
                }
98
            }
99
        }
100
101
        return $classes;
102
    }
103
}
104