Passed
Push — develop ( 6e8f17...2975bd )
by Jens
03:53
created

DocumentStorage::unpublishDocumentBySlug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 1
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\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)) {
25
            throw new \Exception('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
     */
38
    public function getTotalDocumentCount()
39
    {
40
        return $this->repository->getContentRepository()->getTotalDocumentCount();
41
    }
42
43
    /**
44
     * @param string $slug
45
     *
46
     * @param string $state
47
     *
48
     * @return mixed
49
     * @throws \Exception
50
     */
51
    public function getDocumentBySlug($slug, $state = 'published')
52
    {
53
        if (!in_array($state, Document::$DOCUMENT_STATES)) {
54
            throw new \Exception('Unsupported document state: ' . $state);
55
        }
56
        $path = '/' . $slug;
57
58
        return $this->repository->getContentRepository()->getDocumentByPath($this->repository, $path, $state);
59
    }
60
61
    /**
62
     * @param $postValues
63
     * @param $state
64
     *
65
     * @return string path
66
     * @throws \Exception
67
     */
68
    public function saveDocument($postValues, $state = 'unpublished')
69
    {
70
        if (!in_array($state, Document::$DOCUMENT_STATES)) {
71
            throw new \Exception('Unsupported document state: ' . $state);
72
        }
73
        $oldPath = '/' . $postValues['path'];
74
75
        $container = $this->getDocumentContainerByPath($oldPath);
76
        $documentObject = DocumentFactory::createDocumentFromPostValues($postValues, new DocumentTypesStorage($this->repository));
77
        if ($container->path === '/') {
78
            $newPath = $container->path . $documentObject->slug;
79
        } else {
80
            $newPath = $container->path . '/' . $documentObject->slug;
81
        }
82
        $documentObject->path = $newPath;
83
        $this->repository->getContentRepository()->saveDocument($documentObject, $state);
84
        return $newPath;
85
    }
86
87
    /**
88
     * @param        $postValues
89
     * @param string $state
90
     * @return string path
91
     */
92
    public function addDocument($postValues, $state = 'unpublished')
93
    {
94
        $documentObject = DocumentFactory::createDocumentFromPostValues($postValues, new DocumentTypesStorage($this->repository));
95
        if ($postValues['path'] === '/') {
96
            $path = $postValues['path'] . $documentObject->slug;
97
        } else {
98
            $path = $postValues['path'] . '/' . $documentObject->slug;
99
        }
100
101
        $documentObject->path = $path;
102
        $this->repository->getContentRepository()->saveDocument($documentObject, $state);
103
        return $path;
104
    }
105
106
    /**
107
     * @param $slug
108
     */
109
    public function deleteDocumentBySlug($slug)
110
    {
111
        $path = '/' . $slug;
112
        $this->repository->getContentRepository()->deleteDocumentByPath($this->repository, $path);
113
    }
114
115
    /**
116
     * Returns the folder containing the document
117
     *
118
     * @param $path
119
     *
120
     * @return bool|\CloudControl\Cms\storage\Document
121
     * @throws \Exception
122
     */
123
    private function getDocumentContainerByPath($path)
124
    {
125
        return $this->repository->getContentRepository()->getDocumentContainerByPath($this->repository, $path);
126
    }
127
128
    public function getPublishedDocumentsNoFolders()
129
    {
130
        return $this->repository->getContentRepository()->getPublishedDocumentsNoFolders();
131
    }
132
133
    public function cleanPublishedDeletedDocuments()
134
    {
135
        $this->repository->getContentRepository()->cleanPublishedDeletedDocuments();
136
    }
137
138
    /**
139
     * Add new document in given path
140
     *
141
     * @param array $postValues
142
     *
143
     * @throws \Exception
144
     */
145
    public function addDocumentFolder($postValues)
146
    {
147
        $documentFolderObject = DocumentFolderFactory::createDocumentFolderFromPostValues($postValues);
148
        if ($postValues['path'] === '/') {
149
            $documentFolderObject->path = $postValues['path'] . $documentFolderObject->slug;
150
        } else {
151
            $documentFolderObject->path = $postValues['path'] . '/' . $documentFolderObject->slug;
152
        }
153
        $this->repository->getContentRepository()->saveDocument($documentFolderObject, 'published');
154
        $this->repository->getContentRepository()->saveDocument($documentFolderObject, 'unpublished');
155
    }
156
157
    /**
158
     * Delete a folder by its compound slug
159
     *
160
     * @param $slug
161
     *
162
     * @throws \Exception
163
     */
164
    public function deleteDocumentFolderBySlug($slug)
165
    {
166
        $path = '/' . $slug;
167
        $this->repository->getContentRepository()->deleteDocumentByPath($this->repository, $path);
168
        $this->repository->getContentRepository()->cleanPublishedDeletedDocuments();
169
    }
170
171
    /**
172
     * @param string $slug
173
     */
174
    public function publishDocumentBySlug($slug)
175
    {
176
        $path = '/' . $slug;
177
        $this->repository->getContentRepository()->publishDocumentByPath($path);
178
    }
179
180
    /**
181
     * @param string $slug
182
     */
183
    public function unpublishDocumentBySlug($slug)
184
    {
185
        $path = '/' . $slug;
186
        $this->repository->getContentRepository()->unpublishDocumentByPath($path);
187
    }
188
189
    /**
190
     * Retrieve a folder by its compound slug
191
     *
192
     * @param $slug
193
     *
194
     * @return mixed
195
     * @throws \Exception
196
     */
197
    public function getDocumentFolderBySlug($slug)
198
    {
199
        $path = '/' . $slug;
200
201
        return $this->repository->getContentRepository()->getDocumentByPath($this->repository, $path);
202
    }
203
204
}