Completed
Push — master ( 08af59...47a612 )
by Leny
07:46
created

WidgetSubscriber::onFlush()   C

Complexity

Conditions 7
Paths 27

Size

Total Lines 41
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

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