Passed
Push — develop ( 4b65e1...8da09c )
by Jens
02:48
created

SearchRouting::manualUpdateRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * User: jensk
4
 * Date: 21-2-2017
5
 * Time: 10:22
6
 */
7
8
namespace CloudControl\Cms\components\cms;
9
10
11
use CloudControl\Cms\cc\Request;
12
use CloudControl\Cms\cc\ResponseHeaders;
13
use CloudControl\Cms\components\CmsComponent;
14
use CloudControl\Cms\search\Indexer;
15
use CloudControl\Cms\search\Search;
16
17
class SearchRouting extends CmsRouting
18
{
19
    protected static $routes = array(
20
        '/search' => 'overviewRoute',
21
        '/search/update-index' => 'updateIndexRoute',
22
        '/search/ajax-update-index' => 'ajaxUpdateIndexRoute',
23
        '/search/manual-update-index' => 'manualUpdateRoute',
24
    );
25
26
    /**
27
     * CmsRouting constructor.
28
     *
29
     * @param Request $request
30
     * @param string $relativeCmsUri
31
     * @param CmsComponent $cmsComponent
32
     * @throws \Exception
33
     */
34
    public function __construct(Request $request, $relativeCmsUri, CmsComponent $cmsComponent)
35
    {
36
        $this->doRouting($request, $relativeCmsUri, $cmsComponent);
37
    }
38
39
    /**
40
     * @param $request
41
     * @param \CloudControl\Cms\components\CmsComponent $cmsComponent
42
     * @throws \Exception
43
     */
44
    protected function overviewRoute($request, $cmsComponent)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

44
    protected function overviewRoute(/** @scrutinizer ignore-unused */ $request, $cmsComponent)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
45
    {
46
        $cmsComponent->subTemplate = 'search';
47
        $cmsComponent->setParameter(CmsConstants::PARAMETER_MAIN_NAV_CLASS, CmsConstants::PARAMETER_SEARCH);
48
        $documentCount = $cmsComponent->storage->getDocuments()->getTotalDocumentCount();
49
        $indexer = new Search($cmsComponent->storage);
50
        $indexedDocuments = $indexer->getIndexedDocuments();
51
        $cmsComponent->setParameter(CmsConstants::PARAMETER_SEARCH_NEEDS_UPDATE, $documentCount !== $indexedDocuments);
52
    }
53
54
    /**
55
     * @param \CloudControl\Cms\components\CmsComponent $cmsComponent
56
     * @param Request $request
57
     */
58
    protected function updateIndexRoute($request, $cmsComponent)
59
    {
60
        $cmsComponent->subTemplate = 'search/update-index';
61
        $cmsComponent->setParameter(CmsConstants::PARAMETER_MAIN_NAV_CLASS, CmsConstants::PARAMETER_SEARCH);
62
        if (isset($_GET['returnUrl'])) {
63
            $returnUrl = $_GET['returnUrl'];
64
        } else {
65
            $returnUrl = $request::$subfolders . $cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX) . '/search';
66
        }
67
        $cmsComponent->setParameter(CmsConstants::PARAMETER_RETURN_URL, $returnUrl);
68
    }
69
70
    protected function ajaxUpdateIndexRoute($request, $cmsComponent)
71
    {
72
        $cmsComponent->subTemplate = 'search/update-index';
73
        if (isset($request::$get['step'])) {
74
            \set_time_limit(0); // Set max excecution time infinite
75
            \session_write_close(); // Close the session, so it doesnt create a lock on the sessionstorage, block other requests.
76
            $indexer = new Indexer($cmsComponent->storage);
77
            $step = $request::$get['step'];
78
            $this->stepRouting($step, $cmsComponent, $indexer);
79
        } else {
80
            $this->showJson('No step defined.', 500);
81
        }
82
    }
83
84
    protected function manualUpdateRoute($request, $cmsComponent)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

84
    protected function manualUpdateRoute(/** @scrutinizer ignore-unused */ $request, $cmsComponent)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
85
    {
86
        $indexer = new Indexer($cmsComponent->storage);
87
        $indexer->updateIndex();
88
    }
89
90
    /**
91
     * @param $obj
92
     * @param int $httpHeader
93
     */
94
    private function showJson($obj, $httpHeader = 200)
95
    {
96
        http_response_code($httpHeader);
97
        ResponseHeaders::add(ResponseHeaders::HEADER_CONTENT_TYPE, ResponseHeaders::HEADER_CONTENT_TYPE_CONTENT_APPLICATION_JSON);
98
        ResponseHeaders::sendAllHeaders();
99
        die(json_encode($obj));
100
    }
101
102
    /**
103
     * @param CmsComponent $cmsComponent
104
     * @param string $step
105
     * @param Indexer $indexer
106
     * @throws \Exception
107
     */
108
    private function stepRouting($step, $cmsComponent, $indexer)
109
    {
110
        switch ($step) {
111
            case 'resetIndex': $indexer->resetIndex(); break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
112
            case 'cleanPublishedDeletedDocuments': $cmsComponent->storage->getDocuments()->cleanPublishedDeletedDocuments(); break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
113
            case 'createDocumentTermCount':
114
                $documents = $cmsComponent->storage->getDocuments()->getPublishedDocumentsNoFolders();
115
                $indexer->createDocumentTermCount($documents);
116
                break;
117
            case 'createDocumentTermFrequency': $indexer->createDocumentTermFrequency(); break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
118
            case 'createTermFieldLengthNorm': $indexer->createTermFieldLengthNorm(); break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
119
            case 'createInverseDocumentFrequency': $indexer->createInverseDocumentFrequency(); break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
120
            case 'replaceOldIndex':
121
                $indexer->replaceOldIndex();
122
                $cmsComponent->storage->getActivityLog()->add('updated search index', 'search');
123
                break;
124
            default : $this->showJson('Invalid step: ' . $step . '.', 500); break;
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a DEFAULT statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in the default statement.

switch ($expr) {
    default : //wrong
        doSomething();
        break;
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
The default body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a default statement must start on the line immediately following the statement.

switch ($expr) {
    default:
        doSomething(); //right
        break;
}


switch ($expr) {
    default:

        doSomething(); //wrong
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
125
        }
126
        $this->showJson('done');
127
    }
128
}