DocumentInspector   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 12
Bugs 0 Features 2
Metric Value
c 12
b 0
f 2
dl 0
loc 175
wmc 13
lcom 1
cbo 2
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getParent() 0 10 2
A getReferrers() 0 4 1
A getNode() 0 4 1
A getChildren() 0 4 1
A hasChildren() 0 4 1
A getLocale() 0 4 1
A getOriginalLocale() 0 4 1
A getDepth() 0 4 1
A getName() 0 4 1
A getPath() 0 6 1
A getUuid() 0 6 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\Component\DocumentManager;
13
14
use PHPCR\NodeInterface;
15
16
/**
17
 * This class infers information about documents, for example
18
 * the documents locale, webspace, path, etc.
19
 */
20
class DocumentInspector
21
{
22
    /**
23
     * @var DocumentRegistry
24
     */
25
    protected $documentRegistry;
26
27
    /**
28
     * @var PathSegmentRegistry
29
     */
30
    protected $pathSegmentRegistry;
31
32
    /**
33
     * @var ProxyFactory
34
     */
35
    protected $proxyFactory;
36
37
    /**
38
     * @param DocumentRegistry $documentRegistry
39
     * @param PathSegmentRegistry $pathSegmentRegistry
40
     * @param ProxyFactory $proxyFactory
41
     */
42
    public function __construct(
43
        DocumentRegistry $documentRegistry,
44
        PathSegmentRegistry $pathSegmentRegistry,
45
        ProxyFactory $proxyFactory
46
    ) {
47
        $this->documentRegistry = $documentRegistry;
48
        $this->pathSegmentRegistry = $pathSegmentRegistry;
49
        $this->proxyFactory = $proxyFactory;
50
    }
51
52
    /**
53
     * Return the parent document for the given document.
54
     *
55
     * @param object $document
56
     *
57
     * @return object|null
58
     */
59
    public function getParent($document)
60
    {
61
        $parentNode = $this->getNode($document)->getParent();
62
63
        if (!$parentNode) {
64
            return;
65
        }
66
67
        return $this->proxyFactory->createProxyForNode($document, $parentNode);
68
    }
69
70
    /**
71
     * Get referrers for the document.
72
     *
73
     * @param object $document
74
     *
75
     * @return Collection\ReferrerCollection
76
     */
77
    public function getReferrers($document)
78
    {
79
        return $this->proxyFactory->createReferrerCollection($document);
80
    }
81
82
    /**
83
     * Return the PHPCR node for the given document.
84
     *
85
     * @param object $document
86
     *
87
     * @return NodeInterface
88
     */
89
    public function getNode($document)
90
    {
91
        return $this->documentRegistry->getNodeForDocument($document);
92
    }
93
94
    /**
95
     * Returns lazy-loading children collection for given document.
96
     *
97
     * @param object $document
98
     * @param array $options
99
     *
100
     * @return Collection\ChildrenCollection
101
     */
102
    public function getChildren($document, array $options = [])
103
    {
104
        return $this->proxyFactory->createChildrenCollection($document, $options);
105
    }
106
107
    /**
108
     * Return true if the document has children.
109
     *
110
     * @param object $document
111
     *
112
     * @return bool
113
     */
114
    public function hasChildren($document)
115
    {
116
        return $this->getNode($document)->hasNodes();
117
    }
118
119
    /**
120
     * Return the locale for the given document.
121
     *
122
     * @param object $document
123
     *
124
     * @return string
125
     */
126
    public function getLocale($document)
127
    {
128
        return $this->documentRegistry->getLocaleForDocument($document);
129
    }
130
131
    /**
132
     * Return the original (requested) locale for the given document.
133
     *
134
     * @param object $document
135
     *
136
     * @return string
137
     */
138
    public function getOriginalLocale($document)
139
    {
140
        return $this->documentRegistry->getOriginalLocaleForDocument($document);
141
    }
142
143
    /**
144
     * Return the depth of the given document within the content repository.
145
     *
146
     * @param $document
147
     *
148
     * @return int
149
     */
150
    public function getDepth($document)
151
    {
152
        return $this->getNode($document)->getDepth();
153
    }
154
155
    /**
156
     * Return the name of the document.
157
     *
158
     * @param $document
159
     *
160
     * @return string
161
     */
162
    public function getName($document)
163
    {
164
        return $this->getNode($document)->getName();
165
    }
166
167
    /**
168
     * Return the path for the given document.
169
     *
170
     * @param object $document
171
     *
172
     * @return string
173
     */
174
    public function getPath($document)
175
    {
176
        return $this->documentRegistry
177
            ->getNodeForDocument($document)
178
            ->getPath();
179
    }
180
181
    /**
182
     * Return the UUID of the given document.
183
     *
184
     * @param object $document
185
     *
186
     * @return string
187
     */
188
    public function getUuid($document)
189
    {
190
        return $this->documentRegistry
191
            ->getNodeForDocument($document)
192
            ->getIdentifier();
193
    }
194
}
195