Completed
Pull Request — develop (#147)
by Wachter
14:07
created

PageSubscriber   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 128
Duplicated Lines 7.03 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 17
c 0
b 0
f 0
lcom 1
cbo 7
dl 9
loc 128
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getSubscribedEvents() 9 9 1
A handleHydrate() 0 12 3
B handlePersist() 0 27 5
A handlePublishPageNumber() 0 11 2
B handleRemove() 0 17 5

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
/*
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\Bundle\ArticleBundle\Document\Subscriber;
13
14
use Sulu\Bundle\ArticleBundle\Document\Behavior\PageBehavior;
15
use Sulu\Component\DocumentManager\DocumentInspector;
16
use Sulu\Component\DocumentManager\Event\HydrateEvent;
17
use Sulu\Component\DocumentManager\Event\PersistEvent;
18
use Sulu\Component\DocumentManager\Event\PublishEvent;
19
use Sulu\Component\DocumentManager\Event\RemoveEvent;
20
use Sulu\Component\DocumentManager\Events;
21
use Sulu\Component\DocumentManager\PropertyEncoder;
22
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
23
24
/**
25
 * Handles document-manager events to set and update page-numbers.
26
 */
27
class PageSubscriber implements EventSubscriberInterface
28
{
29
    const FIELD = 'pageNumber';
30
31
    /**
32
     * @var DocumentInspector
33
     */
34
    private $documentInspector;
35
36
    /**
37
     * @var PropertyEncoder
38
     */
39
    private $propertyEncoder;
40
41
    /**
42
     * @param DocumentInspector $documentInspector
43
     * @param PropertyEncoder $propertyEncoder
44
     */
45
    public function __construct(DocumentInspector $documentInspector, PropertyEncoder $propertyEncoder)
46
    {
47
        $this->documentInspector = $documentInspector;
48
        $this->propertyEncoder = $propertyEncoder;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 View Code Duplication
    public static function getSubscribedEvents()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
55
    {
56
        return [
57
            Events::HYDRATE => ['handleHydrate'],
58
            Events::PERSIST => [['handlePersist', -1024]],
59
            Events::REMOVE => [['handleRemove', 5]],
60
            Events::PUBLISH => [['handlePublishPageNumber', -1024]],
61
        ];
62
    }
63
64
    /**
65
     * Set the page-number to existing pages.
66
     *
67
     * @param HydrateEvent $event
68
     */
69
    public function handleHydrate(HydrateEvent $event)
70
    {
71
        $document = $event->getDocument();
72
        $node = $event->getNode();
73
        $propertyName = $this->propertyEncoder->systemName(static::FIELD);
74
        if (!$document instanceof PageBehavior || !$node->hasProperty($propertyName)) {
75
            return;
76
        }
77
78
        $node = $event->getNode();
79
        $document->setPageNumber($node->getPropertyValue($this->propertyEncoder->systemName(static::FIELD)));
80
    }
81
82
    /**
83
     * Set the page-number to new pages.
84
     *
85
     * @param PersistEvent $event
86
     */
87
    public function handlePersist(PersistEvent $event)
88
    {
89
        $document = $event->getDocument();
90
        $node = $event->getNode();
91
        $propertyName = $this->propertyEncoder->systemName(static::FIELD);
92
        if (!$document instanceof PageBehavior) {
93
            return;
94
        }
95
96
        $parentDocument = $document->getParent();
97
98
        $page = 1;
99
        foreach ($parentDocument->getChildren() as $child) {
100
            if (!$child instanceof PageBehavior) {
101
                continue;
102
            }
103
104
            ++$page;
105
106
            if ($child === $document) {
107
                break;
108
            }
109
        }
110
111
        $node->setProperty($propertyName, $page);
112
        $document->setPageNumber($page);
113
    }
114
115
    /**
116
     * Copy page-number to live workspace.
117
     *
118
     * @param PublishEvent $event
119
     */
120
    public function handlePublishPageNumber(PublishEvent $event)
121
    {
122
        $document = $event->getDocument();
123
        $node = $event->getNode();
124
        $propertyName = $this->propertyEncoder->systemName(static::FIELD);
125
        if (!$document instanceof PageBehavior) {
126
            return;
127
        }
128
129
        $node->setProperty($propertyName, $document->getPageNumber());
130
    }
131
132
    /**
133
     * Adjust the page-numbers of siblings when removing a page.
134
     *
135
     * @param RemoveEvent $event
136
     */
137
    public function handleRemove(RemoveEvent $event)
138
    {
139
        $document = $event->getDocument();
140
        if (!$document instanceof PageBehavior) {
141
            return;
142
        }
143
144
        $page = 1;
145
        foreach ($document->getParent()->getChildren() as $child) {
146
            if (!$child instanceof PageBehavior || $child->getUuid() === $document->getUuid()) {
147
                continue;
148
            }
149
150
            $childNode = $this->documentInspector->getNode($child);
151
            $childNode->setProperty($this->propertyEncoder->systemName(static::FIELD), ++$page);
152
        }
153
    }
154
}
155