Completed
Pull Request — master (#325)
by Paul
09:55
created

WidgetSubscriber   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 15
c 4
b 1
f 0
lcom 1
cbo 10
dl 0
loc 122
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getSubscribedEvents() 0 6 1
D onFlush() 0 44 9
A updateViewCss() 0 14 1
A updateTemplateInheritorsCss() 0 10 3
1
<?php
2
3
namespace Victoire\Bundle\CoreBundle\EventSubscriber;
4
5
use Doctrine\Common\EventSubscriber;
6
use Doctrine\ORM\EntityManager;
7
use Doctrine\ORM\Event\OnFlushEventArgs;
8
use Doctrine\ORM\UnitOfWork;
9
use Victoire\Bundle\CoreBundle\Builder\ViewCssBuilder;
10
use Victoire\Bundle\CoreBundle\Entity\View;
11
use Victoire\Bundle\CoreBundle\Repository\ViewRepository;
12
use Victoire\Bundle\TemplateBundle\Entity\Template;
13
use Victoire\Bundle\WidgetBundle\Entity\Widget;
14
use Victoire\Bundle\WidgetBundle\Repository\WidgetRepository;
15
use Victoire\Bundle\WidgetMapBundle\Builder\WidgetMapBuilder;
16
use Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap;
17
18
class WidgetSubscriber implements EventSubscriber
19
{
20
    private $viewCssBuilder;
21
    private $widgetMapBuilder;
22
    /* @var UnitOfWork $uow */
23
    private $uow;
24
    /* @var EntityManager $em */
25
    private $em;
26
    /* @var WidgetRepository $widgetRepo */
27
    private $widgetRepo;
28
    /* @var ViewRepository $viewRepo */
29
    private $viewRepo;
30
31
    /**
32
     * Construct.
33
     *
34
     * @param ViewCssBuilder $viewCssBuilder
35
     */
36
    public function __construct(ViewCssBuilder $viewCssBuilder, WidgetMapBuilder $widgetMapBuilder)
37
    {
38
        $this->viewCssBuilder = $viewCssBuilder;
39
        $this->widgetMapBuilder = $widgetMapBuilder;
40
    }
41
42
    /**
43
     * Get SubscribedEvents.
44
     *
45
     * @return array
46
     */
47
    public function getSubscribedEvents()
48
    {
49
        return [
50
            'onFlush',
51
        ];
52
    }
53
54
    /**
55
     * Change cssHash of views when a widget is updated or deleted.
56
     *
57
     * @param OnFlushEventArgs $args
58
     */
59
    public function onFlush(OnFlushEventArgs $args)
60
    {
61
        $this->em = $args->getEntityManager();
62
        $this->uow = $this->em->getUnitOfWork();
63
        $this->widgetRepo = $this->em->getRepository('Victoire\Bundle\WidgetBundle\Entity\Widget');
64
        $this->viewRepo = $this->em->getRepository('Victoire\Bundle\CoreBundle\Entity\View');
65
66
        $updatedEntities = $this->uow->getScheduledEntityUpdates();
67
        $deletedEntities = $this->uow->getScheduledEntityDeletions();
68
        //Update View's CSS and inheritors of updated and deleted widgets
69
        foreach (array_merge($updatedEntities) as $entity) {
70
            if (!($entity instanceof Widget)) {
71
                continue;
72
            }
73
74
            /** @var Widget $entity */
75
            foreach ($entity->getWidgetMaps() as $widgetMap) {
76
                if ($widgetMap->getAction() !== WidgetMap::ACTION_DELETE) {
77
                    $view = $widgetMap->getView();
78
                    $this->updateViewCss($view);
79
                    $this->updateTemplateInheritorsCss($view);
80
                }
81
            }
82
        }
83
84
        //Remove CSS of deleted View and update its inheritors
85
        foreach ($deletedEntities as $entity) {
86
            if (!($entity instanceof View)) {
87
                continue;
88
            }
89
90
            $this->viewCssBuilder->removeCssFile($entity->getCssHash());
91
            $this->updateTemplateInheritorsCss($entity);
92
        }
93
        //Update CSS of updated View and its inheritors
94
        foreach ($updatedEntities as $entity) {
95
            if (!($entity instanceof View)) {
96
                continue;
97
            }
98
99
            $this->updateViewCss($entity);
100
            $this->updateTemplateInheritorsCss($entity);
101
        }
102
    }
103
104
    /**
105
     * Change view cssHash, update css file and persist new cssHash.
106
     *
107
     * @param View $view
108
     */
109
    public function updateViewCss(View $view)
110
    {
111
        $oldHash = $view->getCssHash();
112
        $view->changeCssHash();
113
114
        //Update css file
115
        $this->widgetMapBuilder->build($view, $this->em, true);
116
        $widgets = $this->widgetRepo->findAllWidgetsForView($view);
117
        $this->viewCssBuilder->updateViewCss($oldHash, $view, $widgets);
118
119
        //Update hash in database
120
        $metadata = $this->em->getClassMetadata(get_class($view));
121
        $this->uow->recomputeSingleEntityChangeSet($metadata, $view);
122
    }
123
124
    /**
125
     * Update a Template inheritors (View) if necessary.
126
     *
127
     * @param View $view
128
     */
129
    public function updateTemplateInheritorsCss(View $view)
130
    {
131
        if (!($view instanceof Template)) {
132
            return;
133
        }
134
        foreach ($view->getInheritors() as $inheritor) {
0 ignored issues
show
Bug introduced by
The expression $view->getInheritors() of type string is not traversable.
Loading history...
135
            $this->updateViewCss($inheritor);
136
            $this->updateTemplateInheritorsCss($inheritor);
137
        }
138
    }
139
}
140