Completed
Pull Request — master (#290)
by Leny
07:15
created

BusinessEntitySubscriber   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 118
Duplicated Lines 3.39 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 8
Bugs 1 Features 0
Metric Value
wmc 14
c 8
b 1
f 0
lcom 1
cbo 6
dl 4
loc 118
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getSubscribedEvents() 0 6 1
A postUpdate() 0 19 3
C updateBusinessPages() 4 48 9

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Victoire\Bundle\BusinessEntityBundle\EventSubscriber;
4
5
use Doctrine\Common\EventSubscriber;
6
use Doctrine\ORM\EntityManager;
7
use Doctrine\ORM\Event\LifecycleEventArgs;
8
use Doctrine\ORM\UnitOfWork;
9
use Victoire\Bundle\BusinessEntityBundle\Entity\BusinessEntity;
10
use Victoire\Bundle\BusinessEntityBundle\Helper\BusinessEntityHelper;
11
use Victoire\Bundle\BusinessPageBundle\Builder\BusinessPageBuilder;
12
use Victoire\Bundle\BusinessPageBundle\Entity\BusinessPage;
13
use Victoire\Bundle\BusinessPageBundle\Helper\BusinessPageHelper;
14
use Victoire\Bundle\BusinessPageBundle\Repository\BusinessPageRepository;
15
16
class BusinessEntitySubscriber implements EventSubscriber
17
{
18
    protected $viewCacheManager;
19
    protected $viewCacheDriver;
20
    protected $businessPageBuilder;
21
22
    /**
23
     * @param BusinessPageBuilder  $businessPageBuilder
24
     * @param BusinessEntityHelper $businessEntityHelper
25
     * @param BusinessPageHelper   $businessPageHelper
26
     */
27
    public function __construct(BusinessPageBuilder          $businessPageBuilder,
28
                                BusinessEntityHelper         $businessEntityHelper,
29
                                BusinessPageHelper           $businessPageHelper
30
    ) {
31
        $this->businessPageBuilder = $businessPageBuilder;
32
        $this->businessEntityHelper = $businessEntityHelper;
0 ignored issues
show
Bug introduced by
The property businessEntityHelper does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
33
        $this->businessPageHelper = $businessPageHelper;
0 ignored issues
show
Bug introduced by
The property businessPageHelper does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
34
    }
35
36
    /**
37
     * bind to LoadClassMetadata method.
38
     *
39
     * @return string[] The subscribed events
40
     */
41
    public function getSubscribedEvents()
42
    {
43
        return [
44
            'postUpdate',
45
        ];
46
    }
47
48
    /**
49
     * @param LifecycleEventArgs $eventArgs
50
     */
51
    public function postUpdate(LifecycleEventArgs $eventArgs)
52
    {
53
        /** @var EntityManager $entityManager */
54
        $entityManager = $eventArgs->getEntityManager();
55
        /** @var UnitOfWork $uow */
56
        $uow = $entityManager->getUnitOfWork();
57
58
        foreach ($uow->getScheduledEntityInsertions() as $entity) {
59
            $businessEntity = $this->businessEntityHelper->findByEntityInstance($entity);
60
            if ($businessEntity) {
61
                $this->updateBusinessPages(
62
                    $entity,
63
                    $businessEntity,
64
                    $entityManager,
65
                    $uow->getScheduledEntityDeletions()
66
                );
67
            }
68
        }
69
    }
70
71
    /**
72
     * get BusinessTemplate concerned by this entity (if so)
73
     * then get BusinessPages
74
     * for each BusinessPage, update its slug according to the new slug (if so).
75
     *
76
     * @param $entity
77
     * @param BusinessEntity $businessEntity
78
     * @param EntityManager  $entityManager
79
     * @param array          $deletions
80
     *
81
     * @throws \Exception
82
     *
83
     * @internal param LifecycleEventArgs $eventArgs
84
     */
85
    public function updateBusinessPages($entity, BusinessEntity $businessEntity, EntityManager $entityManager, $deletions)
86
    {
87
        $businessTemplates = $entityManager->getRepository('VictoireBusinessPageBundle:BusinessTemplate')->findPagePatternByBusinessEntity($businessEntity);
88
        foreach ($businessTemplates as $businessTemplate) {
89
            if ($this->businessPageHelper->isEntityAllowed($businessTemplate, $entity, $entityManager)) {
90
                /** @var BusinessPageRepository $bepRepo */
91
                $bepRepo = $entityManager->getRepository('VictoireBusinessPageBundle:BusinessPage');
92
                $virtualBusinessPage = $this->businessPageBuilder->generateEntityPageFromTemplate(
93
                    $businessTemplate,
94
                    $entity,
95
                    $entityManager
96
                );
97
                // Get the BusinessPage if exists for the given entity
98
                /** @var BusinessPage $businessPage */
99
                $businessPage = $bepRepo->findPageByBusinessEntityAndPattern(
100
                    $businessTemplate,
101
                    $entity,
102
                    $businessEntity
103
                );
104
                // If there is diff between persisted BEP and computed, persist the change
105
                $scheduledForRemove = false;
106
                foreach ($deletions as $deletion) {
107
                    if (get_class($deletion) == get_class($businessPage)
108
                        && $deletion->getId() === $businessPage->getId()
109
                    ) {
110
                        $scheduledForRemove = true;
111
                    }
112
                }
113
114
                if ($businessPage && !$scheduledForRemove) {
115
                    $oldSlug = $businessPage->getSlug();
116
                    $newSlug = $entity->getSlug();
117
                    $staticUrl = $businessPage->getStaticUrl();
118
119 View Code Duplication
                    if ($staticUrl) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
120
                        $staticUrl = preg_replace('/'.$oldSlug.'/', $newSlug, $staticUrl);
121
                        $businessPage->setStaticUrl($staticUrl);
0 ignored issues
show
Bug introduced by
It seems like $staticUrl defined by preg_replace('/' . $oldS..., $newSlug, $staticUrl) on line 120 can also be of type array<integer,string>; however, Victoire\Bundle\Business...essPage::setStaticUrl() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
122
                    }
123
124
                    $businessPage->setName($virtualBusinessPage->getName());
125
                    $businessPage->setSlug($virtualBusinessPage->getSlug());
126
127
                    $entityManager->persist($businessPage);
128
                    $entityManager->flush();
129
                }
130
            }
131
        }
132
    }
133
}
134