Skip to content

Commit e8b8df1

Browse files
committed
Ability to run anonymization on specific models
1 parent 4b284eb commit e8b8df1

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class User extends Authenticatable
5858
}
5959

6060
// optional
61-
public function anonymizableCondition()
61+
public function anonymizableCondition(): Builder
6262
{
6363
return self::withTrashed()->where('something', '=>', '...');
6464
}
@@ -70,6 +70,11 @@ Anonymization is performed using command:
7070
php artisan db:anonymize
7171
```
7272

73+
Or on specific models:
74+
```bash
75+
php artisan db:anonymize --model=\\App\User --model=\\App\\Profile
76+
```
77+
7378
## License
7479

7580
The MIT License (MIT). Please see [License File](LICENSE) for more information.

src/Anonymizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Outsidaz\LaravelDataAnonymization;;
3+
namespace Outsidaz\LaravelDataAnonymization;
44
use Faker\Factory;
55
use Faker\Generator;
66
use Illuminate\Database\Eloquent\Builder;

src/Commands/AnonymizerCommand.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ class AnonymizerCommand extends Command
1515
*
1616
* @var string
1717
*/
18-
protected $name = 'db:anonymize';
18+
protected $signature = 'db:anonymize {--model=*}';
1919

2020
public $description = 'Anonymize specific data of the database';
21+
2122
private Anonymizer $service;
2223

2324

@@ -30,10 +31,19 @@ public function __construct()
3031

3132
public function handle(): int
3233
{
34+
$specified = $this->option('model');
35+
3336
$anonymizationStart = microtime(true);
3437

3538
$anonymizableClasses = $this->service->getAnonymizableClasses();
3639

40+
if ($specified) {
41+
$anonymizableClasses=array_filter(
42+
$anonymizableClasses,
43+
fn($class) => in_array($class, $specified, true)
44+
);
45+
}
46+
3747
$this->warn('Anonymization started');
3848

3949
foreach ($anonymizableClasses as $anonymizableClass) {

0 commit comments

Comments
 (0)