Completed
Push — master ( 3d485f...407dff )
by Paweł
66:49
created

RevisionsSubscriber::publish()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.6845
c 0
b 0
f 0
cc 4
eloc 13
nc 5
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher Core Bundle.
5
 *
6
 * Copyright 2016 Sourcefabric z.ú. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2016 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\CoreBundle\EventSubscriber;
16
17
use SWP\Bundle\CoreBundle\Repository\RevisionAwareContainerRepositoryInterface;
18
use SWP\Bundle\RevisionBundle\Event\RevisionPublishedEvent;
19
use SWP\Bundle\RevisionBundle\Events;
20
use SWP\Component\Revision\Model\RevisionInterface;
21
use SWP\Bundle\RevisionBundle\Model\RevisionLogInterface;
22
use SWP\Component\Revision\RevisionAwareInterface;
23
use SWP\Component\Storage\Factory\FactoryInterface;
24
use SWP\Component\Storage\Model\PersistableInterface;
25
use SWP\Component\TemplatesSystem\Gimme\Model\ContainerInterface;
26
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
27
28
class RevisionsSubscriber implements EventSubscriberInterface
29
{
30
    /**
31
     * @var RevisionAwareContainerRepositoryInterface
32
     */
33
    protected $repository;
34
35
    /**
36
     * @var FactoryInterface
37
     */
38
    protected $revisionLogFactory;
39
40
    /**
41
     * RevisionsSubscriber constructor.
42
     *
43
     * @param RevisionAwareContainerRepositoryInterface $repository
44
     * @param FactoryInterface                          $revisionLogFactory
45
     */
46
    public function __construct(
47
        RevisionAwareContainerRepositoryInterface $repository,
48
        FactoryInterface $revisionLogFactory
49
    ) {
50
        $this->repository = $repository;
51
        $this->revisionLogFactory = $revisionLogFactory;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public static function getSubscribedEvents()
58
    {
59
        return [
60
            Events::REVISION_PUBLISH => 'publish',
61
        ];
62
    }
63
64
    /**
65
     * @param RevisionPublishedEvent $event
66
     */
67
    public function publish(RevisionPublishedEvent $event)
68
    {
69
        /** @var RevisionInterface $revision */
70
        $revision = $event->getRevision();
71
        if (null === $revision->getPrevious()) {
72
            return;
73
        }
74
75
        // new revision containers id's
76
        $newRevisionContainers = $this->repository->getIds($revision)->getQuery()->getResult();
77
        $ids = [];
78
        foreach ($newRevisionContainers as $container) {
79
            $ids[] = $container['uuid'];
80
        }
81
82
        $containers = $this->repository->getContainerWithoutProvidedIds($ids, $revision)->getQuery()->getResult();
83
        /** @var ContainerInterface|RevisionAwareInterface $container */
84
        foreach ($containers as $container) {
85
            $container->setRevision($revision);
86
            $this->log($container, $revision);
87
        }
88
89
        $this->repository->flush();
90
    }
91
92
    /**
93
     * @param $object
94
     * @param RevisionInterface $revision
95
     */
96
    private function log($object, $revision)
97
    {
98
        if (!$object instanceof PersistableInterface || !$object instanceof RevisionAwareInterface) {
99
            return;
100
        }
101
102
        /** @var RevisionLogInterface $revisionLog */
103
        $revisionLog = $this->revisionLogFactory->create();
104
        $revisionLog->setEvent(RevisionLogInterface::EVENT_UPDATE);
105
        $revisionLog->setObjectType(get_class($object));
106
        $revisionLog->setObjectId($object->getId());
107
        if (null !== $previousRevision = $revision->getPrevious()) {
108
            $revisionLog->setSourceRevision($previousRevision);
109
        }
110
        $revisionLog->setTargetRevision($revision);
111
112
        $this->repository->persist($revisionLog);
113
    }
114
}
115