Completed
Push — master ( f8c9ff...80eb7e )
by Adam
03:35
created

Rebuild::getModels()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 4
nc 3
nop 0
crap 20
1
<?php
2
3
namespace BestServedCold\LaravelZendSearch\Laravel\Console;
4
5
use BestServedCold\LaravelZendSearch\Laravel\Store;
6
use Illuminate\Console\Command;
7
use Illuminate\Database\Eloquent\Model;
8
use Symfony\Component\Console\Helper\ProgressBar;
9
use Symfony\Component\Console\Output\NullOutput;
10
use Illuminate\Support\Facades\App;
11
12
/**
13
 * Class RebuildCommand
14
 * @package BestServedCold\LaravelZendSearch\Laravel\Console
15
 */
16
class Rebuild extends Command
17
{
18
    protected $name = 'search:rebuild';
19
    protected $description = 'Rebuild the search index';
20
21
    private $models = [];
22
23
    public function getModels()
24
    {
25
        foreach (get_declared_classes() as $class) {
26
            if (is_subclass_of($class, Model::class) && method_exists($class, 'searchFields')) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Illuminate\Database\Eloquent\Model::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
27
                $this->models[] = $class;
28
            }
29
        }
30
    }
31
32
    public function handle()
33
    {
34
        $this->getModels();
35
36
        if (!$this->option('verbose')) {
37
            $this->output = new NullOutput;
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Symfony\Component\Console\Output\NullOutput() of type object<Symfony\Component...sole\Output\NullOutput> is incompatible with the declared type object<Illuminate\Console\OutputStyle> of property $output.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
38
        }
39
40
        $this->call('search:destroy', $this->getArguments());
41
42
        $store = App::make(Store::class);
43
44
        if (empty($this->models)) {
45
            $this->error('No models configured for search.');
46
        } else {
47
            foreach ($this->models as $model) {
48
                $object = App::make($model);
49
50
                $this->info('Creating index for model [' . $model . ']');
51
52
                $count = $object->count();
53
54
                if ($object->count() === 0) {
55
                    $this->comment('No records for model [' . $model . ']');
56
                    continue;
57
                }
58
59
                $progress = new ProgressBar($this->getOutput(), $count);
60
                $progress->start();
61
62
                $object->chunk(1000, function($chunk) use ($progress, $store) {
63
                        foreach ($chunk as $record) {
64
                            $store->insertModel($record, false);
65
                            $progress->advance();
66
                        }
67
                    }
68
                );
69
70
                $progress->finish();
71
            }
72
73
            $this->call('search:optimise', $this->getArguments());
74
75
            $this->info(PHP_EOL . 'Search engine rebuild complete.');
76
        }
77
    }
78
}
79