Passed
Push — develop ( e3baf2...b0eb3e )
by Jens
03:01
created

DocumentStorage::getDocuments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
65
	{
66
		$documentObject = DocumentFactory::createDocumentFromPostValues($postValues, $this);
0 ignored issues
show
Documentation introduced by
$this is of type this<library\storage\storage\DocumentStorage>, but the function expects a object<library\storage\s...e\DocumentTypesStorage>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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
}