Passed
Push — develop ( b69567...61a0d8 )
by Jens
04:03
created

DocumentComponent::runLikeApplicationComponent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 8
rs 9.4285
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
			} else {
58
				throw new \Exception('When used as application component, you need to specify a document.');
59
			}
60
		}
61
62
		/**
63
		 * Run as regular component
64
		 *
65
		 * @throws \Exception
66
		 */
67
		private function runLikeRegularComponent()
68
		{
69
			if ($this->matchedSitemapItem->regex == false) {
70
				$this->runWithoutRegex();
71
			} else {
72
				$this->runWithRegex();
73
			}
74
		}
75
76
		/**
77
		 * Run without regex
78
		 *
79
		 * @throws \Exception
80
		 */
81
		private function runWithoutRegex()
82
		{
83
			if (isset($this->parameters['document'])) {
84
				$this->runByDocumentParameter();
85
			} else {
86
				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);
87
			}
88
		}
89
90
		/**
91
		 * Run with regex
92
		 *
93
		 * @throws \Exception
94
		 */
95
		private function runWithRegex()
96
		{
97
			if (isset($this->parameters['document'])) {
98
				$this->runByDocumentParameter();
99
			} else {
100
				$relativeDocumentUri = current($this->matchedSitemapItem->matches[1]);
101
				if (isset($this->parameters['folder'])) {
102
					if (substr($this->parameters['folder'], -1) !== '/') {
103
						$this->parameters['folder'] = $this->parameters['folder'] . '/';
104
					}
105
					$relativeDocumentUri = $this->parameters['folder'] . $relativeDocumentUri;
106
				}
107
108
				$document = $this->storage->getDocumentBySlug($relativeDocumentUri);
109
110
				if ($document->type == 'folder') {
111
					throw new \Exception('The found document is a folder.');
112
				}
113
114
				if ($document->state != 'published') {
115
					throw new \Exception('Found document is unpublished.');
116
				}
117
				$this->parameters[$this->documentParameterName] = $document;
118
			}
119
		}
120
121
		/**
122
		 * Run using the given `document` parameter
123
		 */
124
		private function runByDocumentParameter()
125
		{
126
			$this->parameters[$this->documentParameterName] = $this->storage->getDocumentBySlug($this->parameters['document']);
127
		}
128
	}
129
}