1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Omatech\Enigma\Commands; |
4
|
|
|
|
5
|
|
|
use HaydenPierce\ClassFinder\ClassFinder; |
6
|
|
|
use Illuminate\Console\Command; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Omatech\Enigma\Database\Eloquent\HasEnigma; |
9
|
|
|
use Omatech\Enigma\Enigma; |
10
|
|
|
use Omatech\Enigma\Exceptions\InvalidClassException; |
11
|
|
|
|
12
|
|
|
class IndexHydrateCommand extends Command |
13
|
|
|
{ |
14
|
|
|
private $enigma; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The name and signature of the console command. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $signature = 'enigma:hydrate { namespace : Fully qualified namespace }'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The console command description. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $description = 'Rehydrate the indexes given a namespace'; |
29
|
|
|
|
30
|
63 |
|
public function __construct(Enigma $enigma) |
31
|
|
|
{ |
32
|
63 |
|
parent::__construct(); |
33
|
63 |
|
$this->enigma = $enigma; |
34
|
63 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Execute the console command. |
38
|
|
|
* |
39
|
|
|
* @param Enigma $enigma |
40
|
|
|
* @return mixed |
41
|
|
|
* @throws InvalidClassException |
42
|
|
|
*/ |
43
|
8 |
|
public function handle() |
44
|
|
|
{ |
45
|
8 |
|
$namespace = $this->argument('namespace'); |
46
|
8 |
|
$foundClasses = ClassFinder::getClassesInNamespace($namespace, ClassFinder::RECURSIVE_MODE); |
|
|
|
|
47
|
|
|
|
48
|
8 |
|
$classes = []; |
49
|
8 |
|
foreach ($foundClasses as $class) { |
50
|
8 |
|
if (isset(class_uses($class)[HasEnigma::class]) === true) { |
51
|
8 |
|
$classes[] = $class; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
8 |
|
$choice = (array) $this->choice( |
56
|
8 |
|
'Which models would you like to hydrate?', |
57
|
8 |
|
array_merge([0 => 'All'], $classes), |
58
|
8 |
|
0, |
59
|
8 |
|
null, |
60
|
8 |
|
true |
61
|
|
|
); |
62
|
|
|
|
63
|
8 |
|
if (array_search('All', $choice) !== false) { |
64
|
4 |
|
$choice = $classes; |
65
|
|
|
} |
66
|
|
|
|
67
|
8 |
|
foreach ($choice as $class) { |
68
|
8 |
|
$this->hydrate(new $class); |
69
|
|
|
} |
70
|
|
|
|
71
|
4 |
|
$this->info('The index hydratation has been finished.'); |
72
|
4 |
|
} |
73
|
|
|
|
74
|
8 |
|
private function hydrate(Model $model) |
75
|
|
|
{ |
76
|
8 |
|
$enigmaEncryptable = $model->getEnigmaEncryptable(); |
77
|
|
|
|
78
|
8 |
|
$this->info(get_class($model)."\n"); |
79
|
8 |
|
$bar = $this->output->createProgressBar($model::count()); |
|
|
|
|
80
|
8 |
|
$bar->start(); |
81
|
|
|
|
82
|
|
|
$model::chunk(100, function ($rows) use ($enigmaEncryptable, $bar) { |
83
|
8 |
|
foreach ($rows as $row) { |
84
|
8 |
|
foreach ($enigmaEncryptable as $column) { |
85
|
8 |
|
if ($row->{$column} !== null) { |
86
|
8 |
|
$this->enigma->hydrateAsModel($row, $column, $row->{$column}); |
87
|
|
|
} |
88
|
|
|
} |
89
|
8 |
|
$bar->advance(); |
90
|
|
|
} |
91
|
8 |
|
}); |
92
|
8 |
|
$bar->finish(); |
93
|
8 |
|
$this->info("\n"); |
94
|
8 |
|
} |
95
|
|
|
} |
96
|
|
|
|