Version 1
[yaffs-website] / vendor / drupal / console / src / Utils / Create / UserData.php
diff --git a/vendor/drupal/console/src/Utils/Create/UserData.php b/vendor/drupal/console/src/Utils/Create/UserData.php
new file mode 100644 (file)
index 0000000..e60907b
--- /dev/null
@@ -0,0 +1,106 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\Console\Utils\Create\UserData.
+ */
+
+namespace Drupal\Console\Utils\Create;
+
+use Drupal\Core\Entity\EntityTypeManagerInterface;
+use Drupal\Core\Entity\EntityFieldManagerInterface;
+use Drupal\Core\Datetime\DateFormatterInterface;
+
+/**
+ * Class Users
+ *
+ * @package Drupal\Console\Utils\Create
+ */
+class UserData extends Base
+{
+    /* @var array */
+    protected $roles = [];
+
+    /**
+     * Users constructor.
+     *
+     * @param EntityTypeManagerInterface  $entityTypeManager
+     * @param EntityFieldManagerInterface $entityFieldManager
+     * @param DateFormatterInterface      $dateFormatter
+     * @param array                       $roles
+     */
+    public function __construct(
+        EntityTypeManagerInterface $entityTypeManager,
+        EntityFieldManagerInterface $entityFieldManager,
+        DateFormatterInterface $dateFormatter,
+        $roles
+    ) {
+        $this->roles = $roles;
+        parent::__construct(
+            $entityTypeManager,
+            $entityFieldManager,
+            $dateFormatter
+        );
+    }
+
+    /**
+     * Create and returns an array of new Users.
+     *
+     * @param $roles
+     * @param $limit
+     * @param $password
+     * @param $timeRange
+     *
+     * @return array
+     */
+    public function create(
+        $roles,
+        $limit,
+        $password,
+        $timeRange
+    ) {
+        $users = [];
+        for ($i=0; $i<$limit; $i++) {
+            $username = $this->getRandom()->word(mt_rand(6, 12));
+
+            $user = $this->entityTypeManager->getStorage('user')->create(
+                [
+                    'name' => $username,
+                    'mail' => $username . '@example.com',
+                    'pass' => $password?:$this->getRandom()->word(mt_rand(8, 16)),
+                    'status' => mt_rand(0, 1),
+                    'roles' => $roles[array_rand($roles)],
+                    'created' => REQUEST_TIME - mt_rand(0, $timeRange),
+                ]
+            );
+
+            try {
+                $user->save();
+
+                $userRoles = [];
+                foreach ($user->getRoles() as $userRole) {
+                    $userRoles[] = $this->roles[$userRole];
+                }
+
+                $users['success'][] = [
+                    'user-id' => $user->id(),
+                    'username' => $user->getUsername(),
+                    'roles' => implode(', ', $userRoles),
+                    'created' => $this->dateFormatter->format(
+                        $user->getCreatedTime(),
+                        'custom',
+                        'Y-m-d h:i:s'
+                    )
+                ];
+            } catch (\Exception $error) {
+                $users['error'][] = [
+                    'vid' => $user->id(),
+                    'name' => $user->get('name'),
+                    'error' => $error->getMessage()
+                ];
+            }
+        }
+
+        return $users;
+    }
+}