Passed
Pull Request — master (#5)
by Gordon
01:54
created

ReindexTask::run()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 18
c 4
b 0
f 0
dl 0
loc 30
rs 9.3554
cc 5
nc 9
nop 1
1
<?php declare(strict_types = 1);
2
3
/**
4
 * Created by PhpStorm.
5
 * User: gordon
6
 * Date: 25/3/2561
7
 * Time: 17:01 น.
8
 */
9
10
namespace Suilven\FreeTextSearch\Task;
11
12
use League\CLImate\CLImate;
0 ignored issues
show
Bug introduced by
The type League\CLImate\CLImate was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use SilverStripe\CMS\Model\SiteTree;
14
use SilverStripe\Control\Director;
15
use SilverStripe\Control\HTTPRequest;
16
use SilverStripe\Dev\BuildTask;
17
use Suilven\FreeTextSearch\Indexes;
18
19
class ReindexTask extends BuildTask
20
{
21
22
    protected $title = 'Reindex';
23
24
    protected $description = 'Reindex all dataobjects referred to in indexes';
25
26
    protected $enabled = true;
27
28
    private static $segment = 'reindex';
0 ignored issues
show
introduced by
The private property $segment is not used, and could be removed.
Loading history...
29
30
    /** @return */
31
    public function run(HTTPRequest $request)
32
    {
33
        $climate = new CLImate();
34
35
        // check this script is being run by admin
36
        $canAccess = (Director::isDev() || Director::is_cli() || Permission::check("ADMIN"));
0 ignored issues
show
Bug introduced by
The type Suilven\FreeTextSearch\Task\Permission was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
37
        if (!$canAccess) {
38
            return Security::permissionFailure($this);
0 ignored issues
show
Bug introduced by
The type Suilven\FreeTextSearch\Task\Security was not found. Did you mean Security? If so, make sure to prefix the type with \.
Loading history...
39
        }
40
41
        /** @var string $indexName */
42
        $indexName = $request->param('index');
43
        $indexes = new Indexes();
44
        $index = $indexes->getIndex($indexName);
45
        $clazz = $index->getClass();
46
47
48
        $climate->border();
49
        $climate->green()->bold('Indexing sitetree');
50
        $climate->border();
51
52
        $nDocuments = SiteTree::get()->count();
53
        $progress = $climate->progress()->total($nDocuments);
54
55
        $ctr = 0;
56
        foreach ($clazz::get() as $do) {
57
            $do->write();
58
59
            $ctr++;
60
            $progress->current($ctr);
61
        }
62
    }
63
}
64