Passed
Push — master ( fa28f5...070d6c )
by Jens
02:40
created

DocumentComponent   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 8
Bugs 2 Features 3
Metric Value
c 8
b 2
f 3
dl 0
loc 114
rs 10
wmc 17
lcom 1
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 12 2
A checkParameters() 0 6 2
A runLikeApplicationComponent() 0 9 2
A runLikeRegularComponent() 0 8 2
A runWithoutRegex() 0 8 2
B runWithRegex() 0 25 6
A runByDocumentParameter() 0 4 1
1
<?php
2
namespace library\components {
3
4
	use library\storage\Storage;
5
6
	/**
7
	 * Class DocumentComponent
8
	 *
9
	 * Has optional parameter `folder` to prefix the relative url with a folder
10
	 * Has optional parameter `document` to select a given document
11
	 * Has optional parameter `documentParameterName` to select the parametername to be used
12
	 * 		to set the found document to.
13
	 *
14
	 * @package library\components
15
	 */
16
	class DocumentComponent extends BaseComponent
17
	{
18
		protected $documentParameterName = 'document';
19
20
		/**
21
		 * @param Storage $storage
22
		 * @return mixed|void
23
		 * @throws \Exception
24
		 */
25
		public function run(Storage $storage)
26
		{
27
			parent::run($storage);
28
29
			$this->checkParameters();
30
31
			if ($this->matchedSitemapItem === null) { // If no sitemapitem, its an application component
32
				$this->runLikeApplicationComponent();
33
			} else {
34
				$this->runLikeRegularComponent();
35
			}
36
		}
37
38
		/**
39
		 * Checks to see if any parameters were defined in the cms and acts according
40
		 */
41
		private function checkParameters()
42
		{
43
			if (isset($this->parameters['documentParameterName'])) {
44
				$this->documentParameterName = $this->parameters['documentParameterName'];
45
			}
46
		}
47
48
		/**
49
		 * Run as application component
50
		 *
51
		 * @throws \Exception
52
		 */
53
		private function runLikeApplicationComponent()
54
		{
55
			if (isset($this->parameters['document'])) {
56
				$this->parameters[$this->documentParameterName] = $this->storage->getDocumentBySlug($this->parameters['document']);
57
				unset($this->parameters['document']);
58
			} else {
59
				throw new \Exception('When used as application component, you need to specify a document.');
60
			}
61
		}
62
63
		/**
64
		 * Run as regular component
65
		 *
66
		 * @throws \Exception
67
		 */
68
		private function runLikeRegularComponent()
69
		{
70
			if ($this->matchedSitemapItem->regex == false) {
71
				$this->runWithoutRegex();
72
			} else {
73
				$this->runWithRegex();
74
			}
75
		}
76
77
		/**
78
		 * Run without regex
79
		 *
80
		 * @throws \Exception
81
		 */
82
		private function runWithoutRegex()
83
		{
84
			if (isset($this->parameters['document'])) {
85
				$this->runByDocumentParameter();
86
			} else {
87
				throw new \Exception('When not using a regex, you need to set the parameter `document` with the path to the document in this sitemap item: ' . $this->matchedSitemapItem->title);
88
			}
89
		}
90
91
		/**
92
		 * Run with regex
93
		 *
94
		 * @throws \Exception
95
		 */
96
		private function runWithRegex()
97
		{
98
			if (isset($this->parameters['document'])) {
99
				$this->runByDocumentParameter();
100
			} else {
101
				$relativeDocumentUri = current($this->matchedSitemapItem->matches[1]);
102
				if (isset($this->parameters['folder'])) {
103
					if (substr($this->parameters['folder'], -1) !== '/') {
104
						$this->parameters['folder'] = $this->parameters['folder'] . '/';
105
					}
106
					$relativeDocumentUri = $this->parameters['folder'] . $relativeDocumentUri;
107
				}
108
109
				$document = $this->storage->getDocumentBySlug($relativeDocumentUri);
110
111
				if ($document->type == 'folder') {
112
					throw new \Exception('The found document is a folder.');
113
				}
114
115
				if ($document->state != 'published') {
116
					throw new \Exception('Found document is unpublished.');
117
				}
118
				$this->parameters[$this->documentParameterName] = $document;
119
			}
120
		}
121
122
		/**
123
		 * Run using the given `document` parameter
124
		 */
125
		private function runByDocumentParameter()
126
		{
127
			$this->parameters[$this->documentParameterName] = $this->storage->getDocumentBySlug($this->parameters['document']);
128
		}
129
	}
130
}