Completed
Pull Request — develop (#161)
by Wachter
15:19
created

PageTreeRouteContentType::readPage()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 3
eloc 11
nc 3
nop 2
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\Content;
13
14
use Ferrandini\Urlizer;
15
use PHPCR\ItemNotFoundException;
16
use PHPCR\NodeInterface;
17
use PHPCR\PropertyType;
18
use Sulu\Bundle\DocumentManagerBundle\Bridge\PropertyEncoder;
19
use Sulu\Component\Content\Compat\PropertyInterface;
20
use Sulu\Component\Content\SimpleContentType;
21
22
/**
23
 * Provides page_tree_route content-type.
24
 */
25
class PageTreeRouteContentType extends SimpleContentType
26
{
27
    const NAME = 'page_tree_route';
28
29
    /**
30
     * @var string
31
     */
32
    private $template;
33
34
    /**
35
     * @var PropertyEncoder
36
     */
37
    private $propertyEncoder;
38
39
    /**
40
     * @param string $template
41
     * @param PropertyEncoder $propertyEncoder
42
     */
43
    public function __construct($template, PropertyEncoder $propertyEncoder)
44
    {
45
        parent::__construct('PageTreeRoute');
46
47
        $this->template = $template;
48
        $this->propertyEncoder = $propertyEncoder;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function read(NodeInterface $node, PropertyInterface $property, $webspaceKey, $languageCode, $segmentKey)
55
    {
56
        $propertyName = $property->getName();
57
        $value = [
58
            'page' => $this->readPage($propertyName, $node),
59
            'path' => $node->getPropertyValueWithDefault($propertyName, ''),
60
            'suffix' => $node->getPropertyValueWithDefault($propertyName . '-suffix', ''),
61
        ];
62
63
        $property->setValue($value);
64
65
        return $value;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function write(
72
        NodeInterface $node,
73
        PropertyInterface $property,
74
        $userId,
75
        $webspaceKey,
76
        $languageCode,
77
        $segmentKey
78
    ) {
79
        $value = $property->getValue();
80
        if (!$value) {
81
            return $this->remove($node, $property, $webspaceKey, $languageCode, $segmentKey);
82
        }
83
84
        $titlePropertyName = $this->propertyEncoder->localizedContentName('title', $languageCode);
85
        $title = $node->getPropertyValueWithDefault($titlePropertyName, null);
86
87
        $path = $this->getAttribute('path', $value, '');
88
        $page = $this->getAttribute('page', $value, ['uuid' => null, 'path' => '/']);
89
        $suffix = $this->getAttribute('suffix', $value, Urlizer::urlize($title));
90
91
        // generate url if not set
92
        if (!$path) {
93
            $path = rtrim($page['path'], '/') . '/' . $suffix;
94
        }
95
96
        $propertyName = $property->getName();
97
        $node->setProperty($propertyName, $path);
98
        $node->setProperty($propertyName . '-suffix', $suffix);
99
100
        $pagePropertyName = $propertyName . '-page';
101
        if ($node->hasProperty($pagePropertyName)) {
102
            $node->getProperty($pagePropertyName)->remove();
103
        }
104
        $node->setProperty($pagePropertyName, $page['uuid'], PropertyType::WEAKREFERENCE);
105
        $node->setProperty($pagePropertyName . '-path', $page['path']);
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function getTemplate()
112
    {
113
        return $this->template;
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function getContentData(PropertyInterface $property)
120
    {
121
        $value = parent::getContentData($property);
122
123
        return $value['path'];
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function getViewData(PropertyInterface $property)
130
    {
131
        return $property->getValue();
132
    }
133
134
    /**
135
     * Read page-information from given node.
136
     *
137
     * @param string $propertyName
138
     * @param NodeInterface $node
139
     *
140
     * @return array
141
     */
142
    private function readPage($propertyName, NodeInterface $node)
143
    {
144
        $pagePropertyName = $propertyName . '-page';
145
        if (!$node->hasProperty($pagePropertyName)) {
146
            return;
147
        }
148
149
        try {
150
            $pageUuid = $node->getPropertyValue($pagePropertyName, PropertyType::STRING);
151
        } catch (ItemNotFoundException $exception) {
152
            return;
153
        }
154
155
        return [
156
            'uuid' => $pageUuid,
157
            'path' => $node->getPropertyValueWithDefault($pagePropertyName . '-path', ''),
158
        ];
159
    }
160
161
    /**
162
     * Returns value of array or default.
163
     *
164
     * @param string $name
165
     * @param array $value
166
     * @param mixed $default
167
     *
168
     * @return mixed
169
     */
170
    private function getAttribute($name, array $value, $default = null)
171
    {
172
        if (!array_key_exists($name, $value)) {
173
            return $default;
174
        }
175
176
        return $value[$name];
177
    }
178
}
179