Passed
Pull Request — master (#13)
by Gordon
01:41
created

ReindexTask::run()   B

Complexity

Conditions 8
Paths 24

Size

Total Lines 69
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 41
c 4
b 1
f 0
dl 0
loc 69
rs 8.0195
cc 8
nc 24
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Factory\BulkIndexerFactory;
18
use Suilven\FreeTextSearch\Indexes;
19
use Suilven\ManticoreSearch\Service\BulkIndexer;
0 ignored issues
show
Bug introduced by
The type Suilven\ManticoreSearch\Service\BulkIndexer 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...
20
21
class ReindexTask extends BuildTask
22
{
23
24
    protected $title = 'Reindex';
25
26
    protected $description = 'Reindex all dataobjects referred to in indexes';
27
28
    protected $enabled = true;
29
30
    private static $segment = 'reindex';
0 ignored issues
show
introduced by
The private property $segment is not used, and could be removed.
Loading history...
31
32
    /**
33
     * Implement this method in the task subclass to
34
     * execute via the TaskRunner
35
     *
36
     * @param HTTPRequest $request
37
     * @return
38
     */
39
    public function run($request)
40
    {
41
        $climate = new CLImate();
42
43
        // check this script is being run by admin
44
        $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...
45
        if (!$canAccess) {
46
            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...
47
        }
48
49
        /** @var string $indexName */
50
        $indexName = $request->getVar('index');
51
        $indexes = new Indexes();
52
        $index = $indexes->getIndex($indexName);
53
54
        /** @var string $clazz */
55
        $clazz = $index->getClass();
56
57
58
        $startTime = microtime(true);
59
60
        $climate->border();
61
        $climate->green()->bold('Indexing sitetree');
62
        $climate->border();
63
64
        $nDocuments = SiteTree::get()->count();
65
        $bulkSize = 500; // @todo configurable
66
        $pages = 1+round($nDocuments / $bulkSize);
67
        $climate->green('Pages: ' . $pages);
68
        $climate->green()->info('Indexing ' . $nDocuments .' objects');
69
        $progress = $climate->progress()->total($nDocuments);
70
71
        $factory = new BulkIndexerFactory();
72
        $bulkIndexer = $factory->getBulkIndexer();
73
        $bulkIndexer->setIndex($indexName);
74
75
        for ($i = 0; $i < $pages; $i++)
76
        {
77
            $dataObjects = $clazz::get()->limit($bulkSize, $i*$bulkSize)->filter('ShowInSearch', true);
78
            foreach($dataObjects as $do) {
79
                // @hack @todo FIX
80
                if ($do->ID !== 6) {
81
                    $bulkIndexer->addDataObject($do);
82
                }
83
84
            }
85
            $bulkIndexer->indexDataObjects();
86
            $current = $bulkSize * ($i+1);
87
            if ($current > $nDocuments) {
88
                $current = $nDocuments;
89
            }
90
            $progress->current($current);
91
        }
92
93
94
95
        $endTime = microtime(true);
96
        $delta = $endTime-$startTime;
97
98
        $rate = round($nDocuments / $delta, 2);
99
100
        $elapsedStr = round($delta, 2);
101
102
        $climate->bold()->blue()->inline("{$nDocuments}");
103
        $climate->blue()->inline(' objects indexed in ');
104
        $climate->bold()->blue()->inline("{$elapsedStr}");
105
        $climate->blue()->inline('s, ');
106
        $climate->bold()->blue()->inline("{$rate}");
107
        $climate->blue(' per second ');
108
109
    }
110
}
111