5 * Contains \Drupal\Console\Utils\Create\UserData.
8 namespace Drupal\Console\Utils\Create;
10 use Drupal\Core\Entity\EntityTypeManagerInterface;
11 use Drupal\Core\Entity\EntityFieldManagerInterface;
12 use Drupal\Core\Datetime\DateFormatterInterface;
17 * @package Drupal\Console\Utils\Create
19 class UserData extends Base
22 protected $roles = [];
27 * @param EntityTypeManagerInterface $entityTypeManager
28 * @param EntityFieldManagerInterface $entityFieldManager
29 * @param DateFormatterInterface $dateFormatter
32 public function __construct(
33 EntityTypeManagerInterface $entityTypeManager,
34 EntityFieldManagerInterface $entityFieldManager,
35 DateFormatterInterface $dateFormatter,
38 $this->roles = $roles;
47 * Create and returns an array of new Users.
56 public function create(
63 for ($i=0; $i<$limit; $i++) {
64 $username = $this->getRandom()->word(mt_rand(6, 12));
66 $user = $this->entityTypeManager->getStorage('user')->create(
69 'mail' => $username . '@example.com',
70 'pass' => $password?:$this->getRandom()->word(mt_rand(8, 16)),
71 'status' => mt_rand(0, 1),
72 'roles' => $roles[array_rand($roles)],
73 'created' => REQUEST_TIME - mt_rand(0, $timeRange),
81 foreach ($user->getRoles() as $userRole) {
82 $userRoles[] = $this->roles[$userRole];
85 $users['success'][] = [
86 'user-id' => $user->id(),
87 'username' => $user->getUsername(),
88 'roles' => implode(', ', $userRoles),
89 'created' => $this->dateFormatter->format(
90 $user->getCreatedTime(),
95 } catch (\Exception $error) {
98 'name' => $user->get('name'),
99 'error' => $error->getMessage()