Passed
Push — develop ( 563d95...57c6aa )
by Jens
03:42
created

DocumentStorage::getDocumentsBySlug()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by jensk on 17-3-2017.
4
 */
5
6
namespace CloudControl\Cms\storage\storage;
7
8
use CloudControl\Cms\storage\entities\Document;
9
use CloudControl\Cms\storage\factories\DocumentFactory;
10
use CloudControl\Cms\storage\factories\DocumentFolderFactory;
11
12
class DocumentStorage extends AbstractStorage
13
{
14
    /**
15
     * Get documents
16
     *
17
     * @param string $state
18
     *
19
     * @return array
20
     * @throws \Exception
21
     */
22
    public function getDocuments($state = 'published')
23
    {
24
        if (!in_array($state, Document::$DOCUMENT_STATES, true)) {
25
            throw new \RuntimeException('Unsupported document state: ' . $state);
26
        }
27
        return $this->repository->getContentRepository()->getDocumentsByPath($this->repository, '/', $state);
28
    }
29
30
    public function getDocumentsWithState($folderPath = '/')
31
    {
32
        return $this->repository->getContentRepository()->getDocumentsWithState($this->repository, $folderPath);
33
    }
34
35
    /**
36
     * @return int
37
     * @throws \Exception
38
     */
39
    public function getTotalDocumentCount()
40
    {
41
        return $this->repository->getContentRepository()->getTotalDocumentCount();
42
    }
43
44
    /**
45
     * @param string $slug
46
     *
47
     * @param string $state
48
     *
49
     * @return mixed
50
     * @throws \Exception
51
     */
52
    public function getDocumentBySlug($slug, $state = 'published')
53
    {
54
        if (!in_array($state, Document::$DOCUMENT_STATES, true)) {
55
            throw new \RuntimeException('Unsupported document state: ' . $state);
56
        }
57
        $path = '/' . $slug;
58
59
        return $this->repository->getContentRepository()->getDocumentByPath($this->repository, $path, $state);
60
    }
61
62
    /**
63
     * @param $postValues
64
     * @param $state
65
     *
66
     * @return string path
67
     * @throws \Exception
68
     */
69
    public function saveDocument($postValues, $state = 'unpublished')
70
    {
71
        if (!in_array($state, Document::$DOCUMENT_STATES, true)) {
72
            throw new \RuntimeException('Unsupported document state: ' . $state);
73
        }
74
        $oldPath = '/' . $postValues['path'];
75
76
        $container = $this->getDocumentContainerByPath($oldPath);
77
        $documentObject = DocumentFactory::createDocumentFromPostValues($postValues,
78
            new DocumentTypesStorage($this->repository));
79
        if ($container->path === '/') {
80
            $newPath = $container->path . $documentObject->slug;
81
        } else {
82
            $newPath = $container->path . '/' . $documentObject->slug;
83
        }
84
        $documentObject->path = $newPath;
85
        $this->repository->getContentRepository()->saveDocument($documentObject, $state);
86
        return $newPath;
87
    }
88
89
    /**
90
     * @param        $postValues
91
     * @param string $state
92
     * @return string path
93
     * @throws \Exception
94
     */
95
    public function addDocument($postValues, $state = 'unpublished')
96
    {
97
        $documentObject = DocumentFactory::createDocumentFromPostValues($postValues,
98
            new DocumentTypesStorage($this->repository));
99
        if ($postValues['path'] === '/') {
100
            $path = $postValues['path'] . $documentObject->slug;
101
        } else {
102
            $path = $postValues['path'] . '/' . $documentObject->slug;
103
        }
104
105
        $documentObject->path = $path;
106
        $this->repository->getContentRepository()->saveDocument($documentObject, $state);
107
        return $path;
108
    }
109
110
    /**
111
     * @param $slug
112
     * @throws \Exception
113
     */
114
    public function deleteDocumentBySlug($slug)
115
    {
116
        $path = '/' . $slug;
117
        $this->repository->getContentRepository()->deleteDocumentByPath($this->repository, $path);
118
    }
119
120
    /**
121
     * Returns the folder containing the document
122
     *
123
     * @param $path
124
     *
125
     * @return bool|\CloudControl\Cms\storage\entities\Document
126
     * @throws \Exception
127
     */
128
    private function getDocumentContainerByPath($path)
129
    {
130
        return $this->repository->getContentRepository()->getDocumentContainerByPath($this->repository, $path);
131
    }
132
133
    /**
134
     * @return array
135
     * @throws \Exception
136
     */
137
    public function getPublishedDocumentsNoFolders()
138
    {
139
        return $this->repository->getContentRepository()->getPublishedDocumentsNoFolders();
140
    }
141
142
    public function cleanPublishedDeletedDocuments()
143
    {
144
        $this->repository->getContentRepository()->cleanPublishedDeletedDocuments();
145
    }
146
147
    /**
148
     * Add new document in given path
149
     *
150
     * @param array $postValues
151
     *
152
     * @throws \Exception
153
     */
154
    public function addDocumentFolder($postValues)
155
    {
156
        $documentFolderObject = DocumentFolderFactory::createDocumentFolderFromPostValues($postValues);
157
        if ($postValues['path'] === '/') {
158
            $documentFolderObject->path = $postValues['path'] . $documentFolderObject->slug;
159
        } else {
160
            $documentFolderObject->path = $postValues['path'] . '/' . $documentFolderObject->slug;
161
        }
162
        $this->repository->getContentRepository()->saveDocument($documentFolderObject, 'published');
163
        $this->repository->getContentRepository()->saveDocument($documentFolderObject, 'unpublished');
164
    }
165
166
    /**
167
     * Delete a folder by its compound slug
168
     *
169
     * @param $slug
170
     *
171
     * @throws \Exception
172
     */
173
    public function deleteDocumentFolderBySlug($slug)
174
    {
175
        $path = '/' . $slug;
176
        $this->repository->getContentRepository()->deleteDocumentByPath($this->repository, $path);
177
        $this->repository->getContentRepository()->cleanPublishedDeletedDocuments();
178
    }
179
180
    /**
181
     * @param string $slug
182
     * @throws \Exception
183
     */
184
    public function publishDocumentBySlug($slug)
185
    {
186
        $path = '/' . $slug;
187
        $this->repository->getContentRepository()->publishDocumentByPath($path);
188
    }
189
190
    /**
191
     * @param string $slug
192
     * @throws \Exception
193
     */
194
    public function unpublishDocumentBySlug($slug)
195
    {
196
        $path = '/' . $slug;
197
        $this->repository->getContentRepository()->unpublishDocumentByPath($path);
198
    }
199
200
    /**
201
     * Retrieve a folder by its compound slug
202
     *
203
     * @param $slug
204
     *
205
     * @return mixed
206
     * @throws \Exception
207
     */
208
    public function getDocumentFolderBySlug($slug)
209
    {
210
        if ($slug === '/') {
211
            $path = $slug;
212
        } else {
213
            $path = '/' . $slug;
214
        }
215
216
        return $this->repository->getContentRepository()->getDocumentByPath($this->repository, $path, 'unpublished');
217
    }
218
219
    /**
220
     * @param $slug
221
     * @param string $state
222
     * @return array
223
     * @throws \Exception
224
     */
225
    public function getDocumentsBySlug($slug, $state = 'published')
226
    {
227
        if (!in_array($state, Document::$DOCUMENT_STATES, true)) {
228
            throw new \RuntimeException('Unsupported document state: ' . $state);
229
        }
230
        $path = '/' . $slug;
231
232
        return $this->repository->getContentRepository()->getDocumentsByPath($this->repository, $path, $state);
233
    }
234
235
}