Passed
Push — develop ( 7ae740...59441d )
by Jens
03:01
created

DocumentComponent   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 6
Bugs 2 Features 2
Metric Value
c 6
b 2
f 2
dl 0
loc 52
rs 10
wmc 11
lcom 1
cbo 2

1 Method

Rating   Name   Duplication   Size   Complexity  
C run() 0 47 11
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
		public function run(Storage $storage)
21
		{
22
			parent::run($storage);
23
24
			if (isset($this->parameters['documentParameterName'])) {
25
				$this->documentParameterName = $this->parameters['documentParameterName'];
26
			}
27
28
			if ($this->matchedSitemapItem === null) { // If no sitemapitem, its an application component
29
				if (isset($this->parameters['document'])) {
30
					$this->parameters[$this->documentParameterName] = $storage->getDocumentBySlug($this->parameters['document']);
31
				} else {
32
					throw new \Exception('When used as application component, you need to specify a document.');
33
				}
34
			} else {
35
				if ($this->matchedSitemapItem->regex == false) {
36
					if (isset($this->parameters['document'])) {
37
						$this->parameters[$this->documentParameterName] = $storage->getDocumentBySlug($this->parameters['document']);
38
					} else {
39
						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);
40
					}
41
				} else {
42
					if (isset($this->parameters['document'])) {
43
						$this->parameters[$this->documentParameterName] = $storage->getDocumentBySlug($this->parameters['document']);
44
					} else {
45
						$relativeDocumentUri = current($this->matchedSitemapItem->matches[1]);
46
						if (isset($this->parameters['folder'])) {
47
							if (substr($this->parameters['folder'], -1) !== '/') {
48
								$this->parameters['folder'] = $this->parameters['folder'] . '/';
49
							}
50
							$relativeDocumentUri = $this->parameters['folder'] . $relativeDocumentUri;
51
						}
52
53
						$document = $storage->getDocumentBySlug($relativeDocumentUri);
54
55
						if ($document->type == 'folder') {
56
							throw new \Exception('The found document is a folder.');
57
						}
58
59
						if ($document->state != 'published') {
60
							throw new \Exception('Found document is unpublished.');
61
						}
62
						$this->parameters[$this->documentParameterName] = $document;
63
					}
64
				}
65
			}
66
		}
67
	}
68
}