Completed
Push — dev ( ec7424...fd1b27 )
by Darko
09:50
created

populate_rt()   B

Complexity

Conditions 11
Paths 15

Size

Total Lines 78
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 43
dl 0
loc 78
rs 7.3166
c 1
b 0
f 0
cc 11
nc 15
nop 2

How to fix   Long Method    Complexity   

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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 24 and the first side effect is on line 3.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
require_once dirname(__DIR__, 2).DIRECTORY_SEPARATOR.'bootstrap/autoload.php';
4
5
use App\Models\Predb;
6
use App\Models\Release;
7
use Blacklight\SphinxSearch;
8
use Illuminate\Support\Facades\DB;
9
10
if (! isset($argv[1]) && ($argv[1] !== 'releases_rt' || $argv[1] !== 'predb_rt')) {
11
    exit(
12
            "Argument 1 is the index name, releases_rt and predb_rt are the only supported ones currently.\n".
13
            "Argument 2 is optional, max number of rows to send to sphinx at a time, 10,000 is the default if not set.\n".
14
            "              The value of 10,000 is good for the default sphinx.conf max_packet_size of 8M, if you want\n".
15
            "              to raise this higher than 10,000, raise the sphinx.conf max_packet_size higher than  8M.\n".
16
            "              If you have many releases, raise the sphinx.conf max_packet_size to 128M (the maximum), restart sphinx and\n".
17
            "              and set Argument 2 to 250,000. This will speed up the script tremendously.\n"
18
    );
19
}
20
21
populate_rt($argv[1], (isset($argv[2]) && is_numeric($argv[2]) && $argv[2] > 0 ? $argv[2] : 10000));
22
23
// Bulk insert releases into sphinx RT index.
24
function populate_rt($table, $max)
25
{
26
    if ($table === 'releases_rt') {
27
        DB::statement('SET SESSION group_concat_max_len=16384;');
28
        $query = 'SELECT r.id, r.name, r.searchname, r.fromname, IFNULL(GROUP_CONCAT(rf.name SEPARATOR " "),"") filename
29
				FROM releases r
30
				LEFT JOIN release_files rf ON r.id = rf.releases_id
31
				WHERE r.id > %d
32
				GROUP BY r.id
33
				ORDER BY r.id ASC
34
				LIMIT %d';
35
36
        $totals = Release::fromQuery('SELECT COUNT(id) AS c, MIN(id) AS min FROM releases')->first();
37
        if (! $totals) {
38
            exit("Could not get database information for releases table.\n");
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
39
        }
40
        $total = $totals->c;
0 ignored issues
show
Bug introduced by
The property c does not seem to exist on App\Models\Release. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
41
        $minId = $totals->min;
0 ignored issues
show
Bug introduced by
The property min does not seem to exist on App\Models\Release. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
42
    }
43
44
    if ($table === 'predb_rt') {
45
        DB::statement('SET SESSION group_concat_max_len=16384;');
46
        $query = 'SELECT id, title, filename
47
				FROM predb
48
				WHERE id > %d
49
				GROUP BY id
50
				ORDER BY id ASC
51
				LIMIT %d';
52
53
        $totals = Predb::fromQuery('SELECT COUNT(id) AS c, MIN(id) AS min FROM predb')->first();
54
        if (! $totals) {
55
            exit("Could not get database information for predb table.\n");
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
56
        }
57
        $total = $totals->c;
0 ignored issues
show
Bug introduced by
The property c does not seem to exist on App\Models\Predb. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
58
        $minId = $totals->min;
0 ignored issues
show
Bug introduced by
The property min does not seem to exist on App\Models\Predb. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
59
    }
60
61
    $sphinx = new SphinxSearch();
62
    $lastId = $minId - 1;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $minId does not seem to be defined for all execution paths leading up to this point.
Loading history...
63
    echo "[Starting to populate sphinx RT index $table with $total releases.]".PHP_EOL;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $total does not seem to be defined for all execution paths leading up to this point.
Loading history...
64
    for ($i = $minId; $i <= ($total + $max + $minId); $i += $max) {
65
        $rows = DB::select(sprintf($query, $lastId, $max));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $query does not seem to be defined for all execution paths leading up to this point.
Loading history...
66
        if ($rows === 0) {
67
            continue;
68
        }
69
70
        foreach ($rows as $row) {
71
            if ($row->id > $lastId) {
72
                $lastId = $row->id;
73
            }
74
            switch ($table) {
75
                    case 'releases_rt':
76
                        $sphinx->insertRelease(
77
                            [
78
                                'id' => $row->id,
79
                                'name' => $row->name,
80
                                'searchname' => $row->searchname,
81
                                'fromname' => $row->fromname,
82
                                'filename' => $row->filename,
83
                            ]
84
                        );
85
                        break;
86
87
                    case 'predb_rt':
88
                        $sphinx->insertPredb(
89
                            [
90
                                'id' => $row->id,
91
                                'title' => $row->title,
92
                                'filename' => $row->filename,
93
                            ]
94
                        );
95
                        break;
96
                }
97
        }
98
99
        echo '.';
100
    }
101
    echo "\n[Done]\n";
102
}
103