Passed
Push — develop ( 3e7098...172c78 )
by Jens
02:50
created

DocumentComponent::run()   C

Complexity

Conditions 7
Paths 11

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
cc 7
eloc 19
c 3
b 2
f 0
nc 11
nop 1
dl 0
loc 31
rs 6.7272
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
	 *
11
	 * @package library\components
12
	 */
13
	class DocumentComponent extends BaseComponent
14
	{
15
		public function run(Storage $storage)
16
		{
17
			parent::run($storage);
18
19
			if ($this->matchedSitemapItem->regex == false) {
20
				if (isset($this->parameters['document'])) {
21
					$this->parameters['document'] = $storage->getDocumentBySlug($this->parameters['document']);
22
				} else {
23
					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);
24
				}
25
			} else {
26
				$relativeDocumentUri = current($this->matchedSitemapItem->matches[1]);
27
				if (isset($this->parameters['folder'])) {
28
					if (substr($this->parameters['folder'], -1) !== '/') {
29
						$this->parameters['folder'] = $this->parameters['folder'] . '/';
30
					}
31
					$relativeDocumentUri = $this->parameters['folder'] . $relativeDocumentUri;
32
				}
33
34
				$document = $storage->getDocumentBySlug($relativeDocumentUri);
35
36
				if ($document->type == 'folder') {
37
					throw new \Exception('The found document is a folder.');
38
				}
39
40
				if ($document->state != 'published') {
41
					throw new \Exception('Found document is unpublished.');
42
				}
43
				$this->parameters['document'] = $document;
44
			}
45
		}
46
	}
47
}