db backup prior to drupal security update
[yaffs-website] / vendor / drupal / console / src / Utils / Create / UserData.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Utils\Create\UserData.
6  */
7
8 namespace Drupal\Console\Utils\Create;
9
10 use Drupal\Core\Entity\EntityTypeManagerInterface;
11 use Drupal\Core\Entity\EntityFieldManagerInterface;
12 use Drupal\Core\Datetime\DateFormatterInterface;
13
14 /**
15  * Class Users
16  *
17  * @package Drupal\Console\Utils\Create
18  */
19 class UserData extends Base
20 {
21     /* @var array */
22     protected $roles = [];
23
24     /**
25      * Users constructor.
26      *
27      * @param EntityTypeManagerInterface  $entityTypeManager
28      * @param EntityFieldManagerInterface $entityFieldManager
29      * @param DateFormatterInterface      $dateFormatter
30      * @param array                       $roles
31      */
32     public function __construct(
33         EntityTypeManagerInterface $entityTypeManager,
34         EntityFieldManagerInterface $entityFieldManager,
35         DateFormatterInterface $dateFormatter,
36         $roles
37     ) {
38         $this->roles = $roles;
39         parent::__construct(
40             $entityTypeManager,
41             $entityFieldManager,
42             $dateFormatter
43         );
44     }
45
46     /**
47      * Create and returns an array of new Users.
48      *
49      * @param $roles
50      * @param $limit
51      * @param $password
52      * @param $timeRange
53      *
54      * @return array
55      */
56     public function create(
57         $roles,
58         $limit,
59         $password,
60         $timeRange
61     ) {
62         $users = [];
63         for ($i=0; $i<$limit; $i++) {
64             $username = $this->getRandom()->word(mt_rand(6, 12));
65
66             $user = $this->entityTypeManager->getStorage('user')->create(
67                 [
68                     'name' => $username,
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),
74                 ]
75             );
76
77             try {
78                 $user->save();
79
80                 $userRoles = [];
81                 foreach ($user->getRoles() as $userRole) {
82                     $userRoles[] = $this->roles[$userRole];
83                 }
84
85                 $users['success'][] = [
86                     'user-id' => $user->id(),
87                     'username' => $user->getUsername(),
88                     'roles' => implode(', ', $userRoles),
89                     'created' => $this->dateFormatter->format(
90                         $user->getCreatedTime(),
91                         'custom',
92                         'Y-m-d h:i:s'
93                     )
94                 ];
95             } catch (\Exception $error) {
96                 $users['error'][] = [
97                     'vid' => $user->id(),
98                     'name' => $user->get('name'),
99                     'error' => $error->getMessage()
100                 ];
101             }
102         }
103
104         return $users;
105     }
106 }