Completed
Pull Request — develop (#351)
by
unknown
13:38
created

WebspaceSubscriber::saveProperties()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 9.0808
c 0
b 0
f 0
cc 5
nc 5
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\Bundle\ArticleBundle\Document\Subscriber;
13
14
use Sulu\Bundle\ArticleBundle\Document\ArticleInterface;
15
use Sulu\Bundle\ArticleBundle\Document\Behavior\WebspaceBehavior;
16
use Sulu\Bundle\ArticleBundle\Metadata\PageTreeTrait;
17
use Sulu\Bundle\DocumentManagerBundle\Bridge\DocumentInspector;
18
use Sulu\Bundle\DocumentManagerBundle\Bridge\PropertyEncoder;
19
use Sulu\Component\Content\Document\LocalizationState;
20
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface;
21
use Sulu\Component\DocumentManager\DocumentManagerInterface;
22
use Sulu\Component\DocumentManager\Event\AbstractMappingEvent;
23
use Sulu\Component\DocumentManager\Events;
24
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
25
26
/**
27
 * Handles document-manager events to set webspace settings.
28
 */
29
class WebspaceSubscriber implements EventSubscriberInterface
30
{
31
    use PageTreeTrait;
32
33
    const MAIN_WEBSPACE_PROPERTY = 'mainWebspace';
34
35
    const ADDITIONAL_WEBSPACES_PROPERTY = 'additionalWebspaces';
36
37
    /**
38
     * @var StructureMetadataFactoryInterface
39
     */
40
    protected $structureMetadataFactory;
41
42
    /**
43
     * @var DocumentManagerInterface
44
     */
45
    protected $documentManager;
46
47
    /**
48
     * @var DocumentInspector
49
     */
50
    protected $documentInspector;
51
52
    /**
53
     * @var PropertyEncoder
54
     */
55
    protected $propertyEncoder;
56
57
    /**
58
     * @param StructureMetadataFactoryInterface $structureMetadataFactory
59
     */
60
    public function __construct(
61
        StructureMetadataFactoryInterface $structureMetadataFactory,
62
        DocumentManagerInterface $documentManager,
63
        DocumentInspector $documentInspector,
64
        PropertyEncoder $propertyEncoder
65
    ) {
66
        $this->structureMetadataFactory = $structureMetadataFactory;
67
        $this->documentManager = $documentManager;
68
        $this->documentInspector = $documentInspector;
69
        $this->propertyEncoder = $propertyEncoder;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    protected function getStructureMetadataFactory()
76
    {
77
        return $this->structureMetadataFactory;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public static function getSubscribedEvents()
84
    {
85
        return [
86
            Events::HYDRATE => ['loadProperties'],
87
            Events::PERSIST => ['saveProperties'],
88
            Events::PUBLISH => ['saveProperties'],
89
        ];
90
    }
91
92
    /**
93
     * @param AbstractMappingEvent $event
94
     */
95
    public function loadProperties(AbstractMappingEvent $event)
96
    {
97
        $document = $event->getDocument();
98
        if (!$document instanceof WebspaceBehavior) {
99
            return;
100
        }
101
102
        $locale = $event->getLocale();
103
        if (LocalizationState::GHOST === $this->documentInspector->getLocalizationState($document)) {
104
            $locale = $document->getOriginalLocale();
105
        }
106
107
        $mainWebspace = $event->getNode()->getPropertyValueWithDefault(
108
            $this->getMainWebspacePropertyName($locale),
109
            null
110
        );
111
        $additionalWebspaces = $event->getNode()->getPropertyValueWithDefault(
112
            $this->getAdditionalWebspacesPropertyName($locale),
113
            null
114
        );
115
116
        $document->setMainWebspace($mainWebspace);
117
        $document->setAdditionalWebspaces($additionalWebspaces);
118
    }
119
120
    /**
121
     * @param AbstractMappingEvent $event
122
     */
123
    public function saveProperties(AbstractMappingEvent $event)
124
    {
125
        $document = $event->getDocument();
126
        if (!$document instanceof ArticleInterface || !$document instanceof WebspaceBehavior) {
127
            return;
128
        }
129
130
        $parentPageUuid = $this->getParentPageUuidFromPageTree($document);
131
        if ($parentPageUuid) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $parentPageUuid of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
132
            // we know now it's a `page_tree_route` route
133
            // so load the parent and find out the webspace
134
            $parentDocument = $this->documentManager->find($parentPageUuid, $event->getLocale());
135
            $mainWebspace = $this->documentInspector->getWebspace($parentDocument);
136
            $document->setMainWebspace($mainWebspace);
137
            $document->setAdditionalWebspaces([]);
138
        }
139
140
        $mainWebspace = $document->getMainWebspace();
141
        $additionalWebspaces = null;
142
        if ($mainWebspace) {
143
            $mainWebspace = $document->getMainWebspace();
144
            $additionalWebspaces = $document->getAdditionalWebspaces();
145
        }
146
147
        $event->getNode()->setProperty(
148
            $this->getMainWebspacePropertyName($document->getLocale()),
149
            $mainWebspace
150
        );
151
        $event->getNode()->setProperty(
152
            $this->getAdditionalWebspacesPropertyName($document->getLocale()),
153
            $additionalWebspaces
154
        );
155
    }
156
157
    /**
158
     * Returns encoded "mainWebspace" property-name.
159
     *
160
     * @param string $locale
161
     *
162
     * @return string
163
     */
164
    private function getMainWebspacePropertyName($locale)
165
    {
166
        return $this->propertyEncoder->localizedSystemName(self::MAIN_WEBSPACE_PROPERTY, $locale);
167
    }
168
169
    /**
170
     * Returns encoded "additionalWebspaces" property-name.
171
     *
172
     * @param string $locale
173
     *
174
     * @return string
175
     */
176
    private function getAdditionalWebspacesPropertyName($locale)
177
    {
178
        return $this->propertyEncoder->localizedSystemName(self::ADDITIONAL_WEBSPACES_PROPERTY, $locale);
179
    }
180
}
181