Completed
Pull Request — develop (#423)
by
unknown
13:26
created

WebspaceSubscriber::getDocumentInspector()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of Sulu.
5
 *
6
 * (c) Sulu 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\DocumentManagerBundle\Bridge\DocumentInspector;
17
use Sulu\Bundle\DocumentManagerBundle\Bridge\PropertyEncoder;
18
use Sulu\Bundle\RouteBundle\PageTree\PageTreeTrait;
19
use Sulu\Component\Content\Document\LocalizationState;
20
use Sulu\Component\DocumentManager\DocumentManagerInterface;
21
use Sulu\Component\DocumentManager\Event\AbstractMappingEvent;
22
use Sulu\Component\DocumentManager\Events;
23
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
24
25
/**
26
 * Handles document-manager events to set webspace settings.
27
 */
28
class WebspaceSubscriber implements EventSubscriberInterface
29
{
30
    use PageTreeTrait;
31
32
    const MAIN_WEBSPACE_PROPERTY = 'mainWebspace';
33
34
    const ADDITIONAL_WEBSPACES_PROPERTY = 'additionalWebspaces';
35
36
    /**
37
     * @var DocumentManagerInterface
38
     */
39
    protected $documentManager;
40
41
    /**
42
     * @var DocumentInspector
43
     */
44
    protected $documentInspector;
45
46
    /**
47
     * @var PropertyEncoder
48
     */
49
    protected $propertyEncoder;
50
51
    public function __construct(
52
        DocumentManagerInterface $documentManager,
53
        DocumentInspector $documentInspector,
54
        PropertyEncoder $propertyEncoder
55
    ) {
56
        $this->documentManager = $documentManager;
57
        $this->documentInspector = $documentInspector;
58
        $this->propertyEncoder = $propertyEncoder;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public static function getSubscribedEvents()
65
    {
66
        return [
67
            Events::HYDRATE => ['loadProperties'],
68
            Events::PERSIST => ['saveProperties'],
69
            Events::PUBLISH => ['saveProperties'],
70
        ];
71
    }
72
73
    /**
74
     * @param AbstractMappingEvent $event
75
     */
76
    public function loadProperties(AbstractMappingEvent $event)
77
    {
78
        $document = $event->getDocument();
79
        if (!$document instanceof WebspaceBehavior) {
80
            return;
81
        }
82
83
        $locale = $event->getLocale();
84
        if (LocalizationState::GHOST === $this->documentInspector->getLocalizationState($document)) {
85
            $locale = $document->getOriginalLocale();
86
        }
87
88
        $mainWebspace = $event->getNode()->getPropertyValueWithDefault(
89
            $this->getMainWebspacePropertyName($locale),
90
            null
91
        );
92
        $additionalWebspaces = $event->getNode()->getPropertyValueWithDefault(
93
            $this->getAdditionalWebspacesPropertyName($locale),
94
            null
95
        );
96
97
        $document->setMainWebspace($mainWebspace);
98
        $document->setAdditionalWebspaces($additionalWebspaces);
99
    }
100
101
    /**
102
     * @param AbstractMappingEvent $event
103
     */
104
    public function saveProperties(AbstractMappingEvent $event)
105
    {
106
        $document = $event->getDocument();
107
        if (!$document instanceof ArticleInterface || !$document instanceof WebspaceBehavior) {
108
            return;
109
        }
110
111
        $parentPageUuid = $this->getParentPageUuidFromPageTree($document);
112
        if ($parentPageUuid) {
113
            // we know now it's a `page_tree_route` route
114
            // so load the parent and find out the webspace
115
            $parentDocument = $this->documentManager->find($parentPageUuid, $event->getLocale());
116
            $mainWebspace = $this->documentInspector->getWebspace($parentDocument);
117
            $document->setMainWebspace($mainWebspace);
118
            $document->setAdditionalWebspaces([]);
119
        }
120
121
        $mainWebspace = $document->getMainWebspace();
122
        $additionalWebspaces = null;
123
        if ($mainWebspace) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $mainWebspace 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...
124
            $mainWebspace = $document->getMainWebspace();
125
            $additionalWebspaces = $document->getAdditionalWebspaces();
126
        }
127
128
        $event->getNode()->setProperty(
129
            $this->getMainWebspacePropertyName($document->getLocale()),
130
            $mainWebspace
131
        );
132
        $event->getNode()->setProperty(
133
            $this->getAdditionalWebspacesPropertyName($document->getLocale()),
134
            $additionalWebspaces
135
        );
136
    }
137
138
    /**
139
     * Returns encoded "mainWebspace" property-name.
140
     *
141
     * @param string $locale
142
     *
143
     * @return string
144
     */
145
    private function getMainWebspacePropertyName($locale)
146
    {
147
        return $this->propertyEncoder->localizedSystemName(self::MAIN_WEBSPACE_PROPERTY, $locale);
148
    }
149
150
    /**
151
     * Returns encoded "additionalWebspaces" property-name.
152
     *
153
     * @param string $locale
154
     *
155
     * @return string
156
     */
157
    private function getAdditionalWebspacesPropertyName($locale)
158
    {
159
        return $this->propertyEncoder->localizedSystemName(self::ADDITIONAL_WEBSPACES_PROPERTY, $locale);
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165
    protected function getDocumentInspector()
166
    {
167
        return $this->documentInspector;
168
    }
169
}
170