NodeManager   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 11
Bugs 0 Features 3
Metric Value
c 11
b 0
f 3
dl 0
loc 174
wmc 17
lcom 1
cbo 3
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A find() 0 14 3
A has() 0 11 2
A remove() 0 5 1
A move() 0 8 1
A copy() 0 11 1
A save() 0 4 1
A clear() 0 4 1
A createPath() 0 17 3
A purgeWorkspace() 0 4 1
A normalizeToPath() 0 8 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\Component\DocumentManager;
13
14
use PHPCR\NodeInterface;
15
use PHPCR\RepositoryException;
16
use PHPCR\SessionInterface;
17
use PHPCR\Util\NodeHelper;
18
use PHPCR\Util\UUIDHelper;
19
use Sulu\Component\DocumentManager\Exception\DocumentNotFoundException;
20
21
/**
22
 * The node manager is responsible for talking to the PHPCR
23
 * implementation.
24
 */
25
class NodeManager
26
{
27
    /**
28
     * @var SessionInterface
29
     */
30
    private $session;
31
32
    /**
33
     * @param SessionInterface $session
34
     */
35
    public function __construct(SessionInterface $session)
36
    {
37
        $this->session = $session;
38
    }
39
40
    /**
41
     * Find a document with the given path or UUID.
42
     *
43
     * @param string $identifier UUID or path
44
     *
45
     * @return NodeInterface
46
     *
47
     * @throws DocumentNotFoundException
48
     */
49
    public function find($identifier)
50
    {
51
        try {
52
            if (UUIDHelper::isUUID($identifier)) {
53
                return $this->session->getNodeByIdentifier($identifier);
54
            }
55
56
            return $this->session->getNode($identifier);
57
        } catch (RepositoryException $e) {
58
            throw new DocumentNotFoundException(sprintf(
59
                'Could not find document with ID or path "%s"', $identifier
60
            ), null, $e);
61
        }
62
    }
63
64
    /**
65
     * Determine if a node exists at the specified path or if a UUID is given,
66
     * then if a node with the UUID exists.
67
     *
68
     * @param string $identifier
69
     *
70
     * @return bool
71
     */
72
    public function has($identifier)
73
    {
74
        $this->normalizeToPath($identifier);
75
        try {
76
            $this->find($identifier);
77
78
            return true;
79
        } catch (DocumentNotFoundException $e) {
80
            return false;
81
        }
82
    }
83
84
    /**
85
     * Remove the document with the given path or UUID.
86
     *
87
     * @param string $identifier ID or path
88
     */
89
    public function remove($identifier)
90
    {
91
        $identifier = $this->normalizeToPath($identifier);
92
        $this->session->removeItem($identifier);
93
    }
94
95
    /**
96
     * Move the document with the given path or ID to the path
97
     * of the destination document (as a child).
98
     *
99
     * @param string $srcId
100
     * @param string $destId
101
     * @param string $name
102
     */
103
    public function move($srcId, $destId, $name)
104
    {
105
        $srcPath = $this->normalizeToPath($srcId);
106
        $parentDestPath = $this->normalizeToPath($destId);
107
        $destPath = $parentDestPath . '/' . $name;
108
109
        $this->session->move($srcPath, $destPath);
110
    }
111
112
    /**
113
     * Copy the document with the given path or ID to the path
114
     * of the destination document (as a child).
115
     *
116
     * @param string $srcId
117
     * @param string $destId
118
     * @param string $name
119
     *
120
     * @return string
121
     */
122
    public function copy($srcId, $destId, $name)
123
    {
124
        $workspace = $this->session->getWorkspace();
125
        $srcPath = $this->normalizeToPath($srcId);
126
        $parentDestPath = $this->normalizeToPath($destId);
127
        $destPath = $parentDestPath . '/' . $name;
128
129
        $workspace->copy($srcPath, $destPath);
130
131
        return $destPath;
132
    }
133
134
    /**
135
     * Save all pending changes currently recorded in this Session.
136
     */
137
    public function save()
138
    {
139
        $this->session->save();
140
    }
141
142
    /**
143
     * Clear the current session.
144
     */
145
    public function clear()
146
    {
147
        $this->session->refresh(false);
148
    }
149
150
    /**
151
     * Create a path.
152
     *
153
     * @param string $path
154
     *
155
     * @return NodeInterface
156
     */
157
    public function createPath($path)
158
    {
159
        $current = $this->session->getRootNode();
160
161
        $segments = preg_split('#/#', $path, null, PREG_SPLIT_NO_EMPTY);
162
        foreach ($segments as $segment) {
163
            if ($current->hasNode($segment)) {
164
                $current = $current->getNode($segment);
165
            } else {
166
                $current = $current->addNode($segment);
167
                $current->addMixin('mix:referenceable');
168
                $current->setProperty('jcr:uuid', UUIDHelper::generateUUID());
169
            }
170
        }
171
172
        return $current;
173
    }
174
175
    /**
176
     * Purge the workspace.
177
     */
178
    public function purgeWorkspace()
179
    {
180
        NodeHelper::purgeWorkspace($this->session);
181
    }
182
183
    /**
184
     * Normalize the given path or ID to a path.
185
     *
186
     * @param string $identifier
187
     *
188
     * @return string
189
     */
190
    private function normalizeToPath($identifier)
191
    {
192
        if (UUIDHelper::isUUID($identifier)) {
193
            $identifier = $this->session->getNodeByIdentifier($identifier)->getPath();
194
        }
195
196
        return $identifier;
197
    }
198
}
199