1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BestServedCold\LaravelZendSearch\Laravel; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
7
|
|
|
use Symfony\Component\Console\Output\NullOutput; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class RebuildModels |
12
|
|
|
* @package BestServedCold\LaravelZendSearch\Laravel |
13
|
|
|
*/ |
14
|
|
|
class RebuildModels |
15
|
|
|
{ |
16
|
|
|
private $progressBar; |
17
|
|
|
private $store; |
18
|
|
|
private $output; |
19
|
|
|
private $models = []; |
20
|
|
|
|
21
|
5 |
|
public function __construct(ProgressBar $progressBar, Store $store, OutputInterface $output) |
22
|
|
|
{ |
23
|
5 |
|
$this->progressBar = $progressBar; |
24
|
5 |
|
$this->store = $store; |
25
|
5 |
|
$this->output = $output; |
26
|
5 |
|
$this->models = $this->getModels(); |
|
|
|
|
27
|
5 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return array|bool |
31
|
|
|
*/ |
32
|
5 |
|
private function getModels() |
33
|
|
|
{ |
34
|
5 |
|
$models = []; |
35
|
|
|
|
36
|
5 |
|
foreach (get_declared_classes() as $class) { |
37
|
5 |
|
if (is_subclass_of($class, Model::class) && method_exists($class, 'searchFields')) { |
|
|
|
|
38
|
5 |
|
$models[] = $class; |
39
|
5 |
|
} |
40
|
5 |
|
} |
41
|
|
|
|
42
|
5 |
|
return empty($models) ? [] : $models; |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
public function setModels(array $models = []) |
46
|
|
|
{ |
47
|
1 |
|
$this->models = $models; |
48
|
1 |
|
} |
49
|
|
|
|
50
|
5 |
|
private function output($type, $string) |
51
|
|
|
{ |
52
|
5 |
|
if (! $this->output instanceof NullOutput) { |
53
|
4 |
|
$this->output->$type($string); |
54
|
4 |
|
} |
55
|
5 |
|
} |
56
|
|
|
|
57
|
5 |
|
public function rebuild() |
58
|
|
|
{ |
59
|
5 |
|
if (empty($this->models)) { |
60
|
1 |
|
$this->output('error', 'No models configured for search.'); |
61
|
1 |
|
} |
62
|
|
|
|
63
|
5 |
|
$this->loopModels(); |
64
|
|
|
|
65
|
5 |
|
$this->output('comment', PHP_EOL . 'Search engine rebuild complete.'); |
66
|
5 |
|
return $this->output; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param array $models |
|
|
|
|
71
|
|
|
*/ |
72
|
5 |
|
private function loopModels() |
73
|
|
|
{ |
74
|
5 |
|
foreach ($this->models as $modelName) { |
75
|
4 |
|
$this->rebuildModel(new $modelName); |
76
|
5 |
|
} |
77
|
5 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param Model $model |
81
|
|
|
*/ |
82
|
4 |
|
private function rebuildModel(Model $model) |
83
|
|
|
{ |
84
|
4 |
|
$this->output('comment', 'Creating index for model [' . $model->getTable() . ']'); |
85
|
|
|
|
86
|
4 |
|
if ($model->count() === 0) { |
87
|
2 |
|
$this->output( |
88
|
2 |
|
'comment', |
89
|
2 |
|
'No records for model [' . $model->getTable() . '].' |
90
|
2 |
|
); |
91
|
2 |
|
return; |
92
|
|
|
} |
93
|
|
|
|
94
|
2 |
|
$this->progressBar->start($model->count()); |
95
|
2 |
|
$this->chunk($model); |
96
|
2 |
|
$this->progressBar->finish(); |
97
|
2 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param Model $object |
101
|
|
|
*/ |
102
|
|
|
private function chunk(Model $object) |
103
|
|
|
{ |
104
|
2 |
|
$object->chunk(1000, function ($chunk) { |
105
|
1 |
|
foreach ($chunk as $record) { |
106
|
1 |
|
$this->store->insertModel($record, false); |
107
|
1 |
|
$this->progressBar->advance(); |
108
|
1 |
|
} |
109
|
2 |
|
}); |
110
|
2 |
|
} |
111
|
|
|
} |
112
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.