GeneralSubscriber::handleFlush()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of Sulu.
5
 *
6
 * (c) MASSIVE ART WebServices GmbH
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Sulu\Component\DocumentManager\Subscriber\Phpcr;
13
14
use Sulu\Component\DocumentManager\DocumentRegistry;
15
use Sulu\Component\DocumentManager\Event\ClearEvent;
16
use Sulu\Component\DocumentManager\Event\CopyEvent;
17
use Sulu\Component\DocumentManager\Event\FlushEvent;
18
use Sulu\Component\DocumentManager\Event\HydrateEvent;
19
use Sulu\Component\DocumentManager\Event\MoveEvent;
20
use Sulu\Component\DocumentManager\Event\RefreshEvent;
21
use Sulu\Component\DocumentManager\Events;
22
use Sulu\Component\DocumentManager\NodeManager;
23
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
24
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
25
26
/**
27
 * This class aggregates some basic repository operations.
28
 *
29
 * NOTE: If any of these methods need to become more complicated, and
30
 *       the changes cannot be done by implementing ANOTHER subscriber, then
31
 *       the individual operations should be broken out into individual subscribers.
32
 *
33
 * NOTE: The event dispatcher is added here for the "refresh" method. This is a clear
34
 *       sign that this should be refactored. The hydration, at least, should be outsourced.
35
 */
36
class GeneralSubscriber implements EventSubscriberInterface
37
{
38
    /**
39
     * @var DocumentRegistry
40
     */
41
    private $documentRegistry;
42
43
    /**
44
     * @var NodeManager
45
     */
46
    private $nodeManager;
47
48
    /**
49
     * @var EventDispatcherInterface
50
     */
51
    private $eventDispatcher;
52
53
    public function __construct(
54
        DocumentRegistry $documentRegistry,
55
        NodeManager $nodeManager,
56
        EventDispatcherInterface $eventDispatcher
57
    ) {
58
        $this->documentRegistry = $documentRegistry;
59
        $this->nodeManager = $nodeManager;
60
        $this->eventDispatcher = $eventDispatcher;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public static function getSubscribedEvents()
67
    {
68
        return [
69
            Events::MOVE => ['handleMove', 400],
70
            Events::COPY => ['handleCopy', 400],
71
            Events::CLEAR => ['handleClear', 500],
72
            Events::FLUSH => ['handleFlush', 500],
73
            Events::REFRESH => ['handleRefresh', 500],
74
        ];
75
    }
76
77
    /**
78
     * @param MoveEvent $event
79
     */
80
    public function handleMove(MoveEvent $event)
81
    {
82
        $document = $event->getDocument();
83
        $node = $this->documentRegistry->getNodeForDocument($document);
84
        $this->nodeManager->move($node->getPath(), $event->getDestId(), $event->getDestName());
85
    }
86
87
    /**
88
     * @param CopyEvent $event
89
     */
90
    public function handleCopy(CopyEvent $event)
91
    {
92
        $document = $event->getDocument();
93
        $node = $this->documentRegistry->getNodeForDocument($document);
94
        $newPath = $this->nodeManager->copy($node->getPath(), $event->getDestId(), $event->getDestName());
95
        $event->setCopiedPath($newPath);
96
    }
97
98
    /**
99
     * @param RefreshEvent $event
100
     */
101
    public function handleRefresh(RefreshEvent $event)
102
    {
103
        $document = $event->getDocument();
104
        $node = $this->documentRegistry->getNodeForDocument($document);
105
        $locale = $this->documentRegistry->getLocaleForDocument($document);
106
107
        // revert/reload the node to the persisted state
108
        $node->revert();
109
110
        // rehydrate the document
111
        $hydrateEvent = new HydrateEvent($node, $locale);
112
        $hydrateEvent->setDocument($document);
113
        $this->eventDispatcher->dispatch(Events::HYDRATE, $hydrateEvent);
114
    }
115
116
    /**
117
     * @param ClearEvent $event
118
     */
119
    public function handleClear(ClearEvent $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
120
    {
121
        $this->nodeManager->clear();
122
    }
123
124
    /**
125
     * @param FlushEvent $event
126
     */
127
    public function handleFlush(FlushEvent $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
128
    {
129
        $this->nodeManager->save();
130
    }
131
}
132