Passed
Push — develop ( 169afe...f2bd80 )
by Jens
02:39
created

SearchRouting   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 88
rs 10
c 2
b 0
f 0
wmc 16
lcom 0
cbo 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 13 5
A overviewRoute() 0 9 1
A updateIndexRoute() 0 5 1
C ajaxUpdateIndexRoute() 0 34 8
A showJson() 0 6 1
1
<?php
2
/**
3
 * User: jensk
4
 * Date: 21-2-2017
5
 * Time: 10:22
6
 */
7
8
namespace library\components\cms;
9
10
11
use library\components\CmsComponent;
12
use library\search\Indexer;
13
use library\search\Search;
14
15
class SearchRouting implements CmsRouting
16
{
17
18
	/**
19
	 * SearchRouting constructor.
20
	 *
21
	 * @param \library\cc\Request              $request
22
	 * @param                                  $relativeCmsUri
23
	 * @param \library\components\CmsComponent $cmsComponent
24
	 */
25
	public function __construct($request, $relativeCmsUri, $cmsComponent)
26
	{
27
		if ($relativeCmsUri === '/search') {
28
			$this->overviewRoute($cmsComponent);
29
		} elseif ($relativeCmsUri === '/search/update-index') {
30
			$this->updateIndexRoute($cmsComponent);
31
		} elseif ($relativeCmsUri === '/search/ajax-update-index') {
32
			$this->ajaxUpdateIndexRoute($request, $cmsComponent);
33
		} elseif ($relativeCmsUri === '/search/manual-update-index') {
34
			$indexer = new Indexer($cmsComponent->storage);
35
			$indexer->updateIndex();
36
		}
37
	}
38
39
	/**
40
	 * @param \library\components\CmsComponent $cmsComponent
41
	 */
42
	private function overviewRoute($cmsComponent)
43
	{
44
		$cmsComponent->subTemplate = 'cms/search';
45
		$cmsComponent->setParameter(CmsComponent::PARAMETER_MAIN_NAV_CLASS, CmsComponent::PARAMETER_SEARCH);
46
		$documentCount = $cmsComponent->storage->getTotalDocumentCount();
47
		$indexer = new Search($cmsComponent->storage);
48
		$indexedDocuments = $indexer->getIndexedDocuments();
49
		$cmsComponent->setParameter(CmsComponent::PARAMETER_SEARCH_NEEDS_UPDATE, $documentCount !== $indexedDocuments);
50
	}
51
52
	/**
53
	 * @param \library\components\CmsComponent $cmsComponent
54
	 */
55
	private function updateIndexRoute($cmsComponent)
56
	{
57
		$cmsComponent->subTemplate = 'cms/search/update-index';
58
		$cmsComponent->setParameter(CmsComponent::PARAMETER_MAIN_NAV_CLASS, CmsComponent::PARAMETER_SEARCH);
59
	}
60
61
	private function ajaxUpdateIndexRoute($request, $cmsComponent)
62
	{
63
		$cmsComponent->subTemplate = 'cms/search/update-index';
64
		if (isset($request::$get['step'])) {
65
			\set_time_limit(0); // Set max excecution time infinite
66
			\session_write_close(); // Close the session, so it doesnt create a lock on the sessionstorage, block other requests.
67
			$indexer = new Indexer($cmsComponent->storage);
68
			$step = $request::$get['step'];
69
			if ($step == 'resetIndex') {
70
				$indexer->resetIndex();
71
				$this->showJson('done');
72
			} elseif ($step == 'createDocumentTermCount') {
73
				$documents = $cmsComponent->storage->getDocuments();
74
				$indexer->createDocumentTermCount($documents);
75
				$this->showJson('done');
76
			} else if ($step == 'createDocumentTermFrequency') {
77
				$indexer->createDocumentTermFrequency();
78
				$this->showJson('done');
79
			} else if ($step == 'createTermFieldLengthNorm') {
80
				$indexer->createTermFieldLengthNorm();
81
				$this->showJson('done');
82
			} else if ($step == 'createInverseDocumentFrequency') {
83
				$indexer->createInverseDocumentFrequency();
84
				$this->showJson('done');
85
			} else if ($step == 'replaceOldIndex') {
86
				$indexer->replaceOldIndex();
87
				$this->showJson('done');
88
			} else {
89
				$this->showJson('Invalid step: ' . $step . '.', 'HTTP/1.0 500 Internal Server Error');
90
			}
91
		} else {
92
			$this->showJson('No step defined.', 'HTTP/1.0 500 Internal Server Error');
93
		}
94
	}
95
96
	private function showJson($obj, $httpHeader = 'HTTP/1.0 200 OK') {
97
		header($_SERVER['SERVER_PROTOCOL'] . $httpHeader, true);
98
		header('Content-type: application/json');
99
		die(json_encode($obj));
100
		exit;
0 ignored issues
show
Unused Code introduced by
die; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
101
	}
102
}