Skip to content
This repository was archived by the owner on Jul 24, 2023. It is now read-only.

Commit 044f7d0

Browse files
committed
Import command changes
- A progress bar will now be displayed indicating the imports status. - The displayed number of imported users is now all users who were imported or synchronized. Users who were not imported are not included in this count.
1 parent 0c84d7b commit 044f7d0

File tree

1 file changed

+53
-24
lines changed

1 file changed

+53
-24
lines changed

src/Commands/Import.php

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function handle()
6565
$this->info("Found {$count} user(s). Importing...");
6666
}
6767

68-
$this->info("Successfully imported {$this->import($users)} user(s).");
68+
$this->info("\nSuccessfully imported / synchronized {$this->import($users)} user(s).");
6969
}
7070

7171
/**
@@ -80,32 +80,37 @@ public function import(array $users = [])
8080
{
8181
$imported = 0;
8282

83+
// We need to filter our results to make sure they are
84+
// only users. In some cases, Contact models may be
85+
// returned due the possibility of the
86+
// existing in the same scope.
87+
$users = collect($users)->filter(function($user) {
88+
return $user instanceof User;
89+
});
90+
91+
$bar = $this->output->createProgressBar(count($users));
92+
8393
foreach ($users as $user) {
84-
if ($user instanceof User) {
85-
try {
86-
// Import the user and then save the model.
87-
$model = $this->getModelFromAdldap($user);
88-
89-
if ($this->saveModel($model) && $model->wasRecentlyCreated) {
90-
// Only increment imported for new models.
91-
$imported++;
92-
93-
// Log the successful import.
94-
if ($this->isLogging()) {
95-
logger()->info("Imported user {$user->getCommonName()}");
96-
}
97-
}
98-
99-
if ($this->isDeleting()) {
100-
$this->delete($user, $model);
101-
}
102-
} catch (\Exception $e) {
103-
// Log the unsuccessful import.
104-
if ($this->isLogging()) {
105-
logger()->error("Unable to import user {$user->getCommonName()}. {$e->getMessage()}");
106-
}
94+
try {
95+
// Import the user and retrieve it's model.
96+
$model = $this->getModelFromAdldap($user);
97+
98+
// Save the returned model.
99+
$this->save($user, $model);
100+
101+
if ($this->isDeleting()) {
102+
$this->delete($user, $model);
103+
}
104+
105+
$imported++;
106+
} catch (\Exception $e) {
107+
// Log the unsuccessful import.
108+
if ($this->isLogging()) {
109+
logger()->error("Unable to import user {$user->getCommonName()}. {$e->getMessage()}");
107110
}
108111
}
112+
113+
$bar->advance();
109114
}
110115

111116
return $imported;
@@ -181,6 +186,30 @@ public function createModel()
181186
return new $model();
182187
}
183188

189+
/**
190+
* Saves the specified user with its model.
191+
*
192+
* @param User $user
193+
* @param Model $model
194+
*
195+
* @return bool
196+
*/
197+
protected function save(User $user, Model $model)
198+
{
199+
$imported = false;
200+
201+
if ($this->saveModel($model) && $model->wasRecentlyCreated) {
202+
$imported = true;
203+
204+
// Log the successful import.
205+
if ($this->isLogging()) {
206+
logger()->info("Imported user {$user->getCommonName()}");
207+
}
208+
}
209+
210+
return $imported;
211+
}
212+
184213
/**
185214
* Soft deletes the specified model if the specified AD account is disabled.
186215
*

0 commit comments

Comments
 (0)