Skip to content

Commit f879069

Browse files
authored
Merge branch 'main' into feature/safer_environments
2 parents d61f64f + 70c5132 commit f879069

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

config/anonymizer.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,13 @@
1616
*/
1717
'blocked_env' => ['production'],
1818
'force_blocked_env' => true,
19+
20+
/*
21+
| Model Ordering
22+
|--------------------------------------------------------------------------
23+
|
24+
| Optionally specify the order of anonymization, these Models will be anonymized first.
25+
|
26+
*/
27+
'ordered_models' => []
1928
];

src/Anonymizable.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ trait Anonymizable
1010
{
1111
public function anonymizableCondition(): Builder
1212
{
13-
return static::withTrashed();
13+
return static::hasMacro('withTrashed') ? static::withTrashed() : static::query();
1414
}
15+
1516
public function anonymizableAttributes(Generator $faker): array
1617
{
1718
throw new LogicException('Please implement the anonymizable method on your model.');

src/Commands/AnonymizerCommand.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,26 @@ public function handle(): int
5656

5757
$this->warn('Anonymization started');
5858

59+
$anonymizableClassesOrdered = collect(config('anonymizer.ordered_models') ?? [])
60+
->map(fn($ac) => '\\'.$ac);
61+
62+
$anonymizableClasses = collect($anonymizableClasses)
63+
->diff($anonymizableClassesOrdered)
64+
->toArray();
65+
66+
if (!empty($anonymizableClassesOrdered)) {
67+
$this->warn('Order dependent models anonymizing.');
68+
}
69+
foreach ($anonymizableClassesOrdered as $anonymizableClass) {
70+
$this->anonymizeTable(
71+
new $anonymizableClass()
72+
);
73+
}
74+
75+
if (!empty($anonymizableClassesOrdered)) {
76+
$this->warn('Remaining models anonymizing.');
77+
}
78+
$anonymizableClasses = collect($anonymizableClasses)->diff($anonymizableClassesOrdered)->all();
5979
foreach ($anonymizableClasses as $anonymizableClass) {
6080
$this->anonymizeTable(
6181
new $anonymizableClass()
@@ -75,7 +95,10 @@ private function anonymizeTable(Model $model): void
7595

7696
$progressBar = $this->output->createProgressBar($this->service->getCount($model));
7797

78-
$progressBar->setFormat('%current%/%max% [%bar%] %percent:3s%% | Remaining: %remaining:6s%');
98+
$progressBar->setFormat('%current%/%max% [%bar%] %percent:3s%');
99+
if($this->service->getCount($model)>0) {
100+
$progressBar->setFormat('%current%/%max% [%bar%] %percent:3s%% | Remaining: %remaining:6s%');
101+
}
79102

80103
$this->service->getChunk($model, function (Collection $chunkItems) use ($progressBar) {
81104
DB::beginTransaction();

0 commit comments

Comments
 (0)