1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by jensk on 17-3-2017. |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace library\storage\storage; |
7
|
|
|
|
8
|
|
|
use library\storage\factories\DocumentFactory; |
9
|
|
|
|
10
|
|
|
class DocumentStorage extends AbstractStorage |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Get documents |
14
|
|
|
* |
15
|
|
|
* @return array |
16
|
|
|
*/ |
17
|
|
|
public function getDocuments() |
18
|
|
|
{ |
19
|
|
|
return $this->repository->getDocuments(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @return int |
24
|
|
|
*/ |
25
|
|
|
public function getTotalDocumentCount() |
26
|
|
|
{ |
27
|
|
|
return $this->repository->getTotalDocumentCount(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string $slug |
32
|
|
|
* |
33
|
|
|
* @return mixed |
34
|
|
|
* @throws \Exception |
35
|
|
|
*/ |
36
|
|
|
public function getDocumentBySlug($slug) |
37
|
|
|
{ |
38
|
|
|
$path = '/' . $slug; |
39
|
|
|
|
40
|
|
|
return $this->repository->getDocumentByPath($path); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param $postValues |
45
|
|
|
*/ |
46
|
|
|
public function saveDocument($postValues) |
47
|
|
|
{ |
48
|
|
|
$oldPath = '/' . $postValues['path']; |
49
|
|
|
|
50
|
|
|
$container = $this->getDocumentContainerByPath($oldPath); |
51
|
|
|
$documentObject = DocumentFactory::createDocumentFromPostValues($postValues, new DocumentTypesStorage($this->repository)); |
52
|
|
|
if ($container->path === '/') { |
53
|
|
|
$newPath = $container->path . $documentObject->slug; |
54
|
|
|
} else { |
55
|
|
|
$newPath = $container->path . '/' . $documentObject->slug; |
56
|
|
|
} |
57
|
|
|
$documentObject->path = $newPath; |
58
|
|
|
$this->repository->saveDocument($documentObject); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param $postValues |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
public function addDocument($postValues) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$documentObject = DocumentFactory::createDocumentFromPostValues($postValues, $this); |
|
|
|
|
67
|
|
|
if ($postValues['path'] === '/') { |
68
|
|
|
$documentObject->path = $postValues['path'] . $documentObject->slug; |
69
|
|
|
} else { |
70
|
|
|
$documentObject->path = $postValues['path'] . '/' . $documentObject->slug; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->repository->saveDocument($documentObject); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param $slug |
78
|
|
|
*/ |
79
|
|
|
public function deleteDocumentBySlug($slug) |
80
|
|
|
{ |
81
|
|
|
$path = '/' . $slug; |
82
|
|
|
$this->repository->deleteDocumentByPath($path); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns the folder containing the document |
87
|
|
|
* |
88
|
|
|
* @param $path |
89
|
|
|
* |
90
|
|
|
* @return bool|\library\storage\Document |
91
|
|
|
* @throws \Exception |
92
|
|
|
*/ |
93
|
|
|
private function getDocumentContainerByPath($path) |
94
|
|
|
{ |
95
|
|
|
return $this->repository->getDocumentContainerByPath($path); |
96
|
|
|
} |
97
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.