Completed
Pull Request — develop (#161)
by Wachter
22:54 queued 09:56
created

PageTreeRouteContentType::getTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 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\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 11
    public function __construct($template, PropertyEncoder $propertyEncoder)
44
    {
45 11
        parent::__construct('PageTreeRoute');
46
47 11
        $this->template = $template;
48 11
        $this->propertyEncoder = $propertyEncoder;
49 11
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 7
    public function read(NodeInterface $node, PropertyInterface $property, $webspaceKey, $languageCode, $segmentKey)
55
    {
56 7
        $propertyName = $property->getName();
57
        $value = [
58 7
            'page' => $this->readPage($propertyName, $node),
59 7
            'path' => $node->getPropertyValueWithDefault($propertyName, ''),
60 7
            'suffix' => $node->getPropertyValueWithDefault($propertyName . '-suffix', ''),
61
        ];
62
63 7
        $property->setValue($value);
64
65 7
        return $value;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 8
    public function write(
72
        NodeInterface $node,
73
        PropertyInterface $property,
74
        $userId,
75
        $webspaceKey,
76
        $languageCode,
77
        $segmentKey
78
    ) {
79 8
        $value = $property->getValue();
80 8
        if (!$value) {
81
            return $this->remove($node, $property, $webspaceKey, $languageCode, $segmentKey);
82
        }
83
84 8
        $titlePropertyName = $this->propertyEncoder->localizedContentName('title', $languageCode);
85 8
        $title = $node->getPropertyValueWithDefault($titlePropertyName, null);
86
87 8
        $path = $this->getAttribute('path', $value, '');
88 8
        $page = $this->getAttribute('page', $value, ['uuid' => null, 'path' => '/']);
89 8
        $suffix = $this->getAttribute('suffix', $value, Urlizer::urlize($title));
90
91
        // generate url if not set
92 8
        if (!$path) {
93 4
            $path = rtrim($page['path'], '/') . '/' . $suffix;
94
        }
95
96 8
        $propertyName = $property->getName();
97 8
        $node->setProperty($propertyName, $path);
98 8
        $node->setProperty($propertyName . '-suffix', $suffix);
99
100 8
        $pagePropertyName = $propertyName . '-page';
101 8
        if ($node->hasProperty($pagePropertyName)) {
102 2
            $node->getProperty($pagePropertyName)->remove();
103
        }
104 8
        $node->setProperty($pagePropertyName, $page['uuid'], PropertyType::WEAKREFERENCE);
105 8
        $node->setProperty($pagePropertyName . '-path', $page['path']);
106 8
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 1
    public function getTemplate()
112
    {
113 1
        return $this->template;
114
    }
115
116
    /**
117
     * Read page-information from given node.
118
     *
119
     * @param string $propertyName
120
     * @param NodeInterface $node
121
     *
122
     * @return array
123
     */
124 7
    private function readPage($propertyName, NodeInterface $node)
125
    {
126 7
        $pagePropertyName = $propertyName . '-page';
127 7
        if (!$node->hasProperty($pagePropertyName)) {
128 1
            return;
129
        }
130
131
        try {
132 6
            $pageUuid = $node->getPropertyValue($pagePropertyName, PropertyType::STRING);
133 1
        } catch (ItemNotFoundException $exception) {
134 1
            return;
135
        }
136
137
        return [
138 6
            'uuid' => $pageUuid,
139 6
            'path' => $node->getPropertyValueWithDefault($pagePropertyName . '-path', ''),
140
        ];
141
    }
142
143
    /**
144
     * Returns value of array or default.
145
     *
146
     * @param string $name
147
     * @param array $value
148
     * @param mixed $default
149
     *
150
     * @return mixed
151
     */
152 8
    private function getAttribute($name, array $value, $default = null)
153
    {
154 8
        if (!array_key_exists($name, $value)) {
155 4
            return $default;
156
        }
157
158 8
        return $value[$name];
159
    }
160
}
161