Passed
Push — master ( 9c6499...c22bc5 )
by Jens
04:52 queued 02:21
created

DocumentComponent   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 115
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 3

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