IndexingHelper   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 53
rs 10
c 2
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getGroupSettings() 0 6 2
A setCores() 0 3 1
A setBatchLength() 0 3 1
A getBatchLength() 0 7 2
A getCores() 0 4 1
1
<?php
2
3
namespace Firesphere\SearchBackend\Helpers;
4
5
use SilverStripe\Core\Config\Configurable;
6
7
class IndexingHelper
8
{
9
    use Configurable;
10
11
    /**
12
     * @var int Length of each to-index batches
13
     */
14
    protected static $batch_length;
15
16
    /**
17
     * @var int amount of CPU cores
18
     */
19
    protected static $cores;
20
21
    /**
22
     * Check the amount of groups and the total against the isGroup check.
23
     *
24
     * @param bool $isGroup Is it a specific group
25
     * @param string $class Class to check
26
     * @param int $group Current group to index
27
     * @return array
28
     */
29
    public static function getGroupSettings(bool $isGroup, string $class, int $group): array
30
    {
31
        $totalGroups = (int)ceil($class::get()->count() / self::getBatchLength());
32
        $groups = $isGroup ? ($group + self::getCores() - 1) : $totalGroups;
33
34
        return [$totalGroups, $groups];
35
    }
36
37
    public static function getBatchLength(): int
38
    {
39
        if (!self::$batch_length) {
40
            self::$batch_length = self::config()->get('batchLength') ?? 10;
41
        }
42
43
        return self::$batch_length;
44
    }
45
46
    public static function setBatchLength(int $batch_length): void
47
    {
48
        self::$batch_length = $batch_length;
49
    }
50
51
    public static function getCores(): int
52
    {
53
        // Always be on the safe side, use only 1 core
54
        return self::$cores ?? 1;
55
    }
56
57
    public static function setCores(int $cores): void
58
    {
59
        self::$cores = $cores;
60
    }
61
}
62