Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Utils / Create / Base.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Utils\Generate\Base.
6  */
7
8 namespace Drupal\Console\Utils\Create;
9
10 use Drupal\Component\Utility\Random;
11 use Drupal\Core\Field\FieldStorageDefinitionInterface;
12 use Drupal\field\FieldConfigInterface;
13 use Drupal\Core\Entity\EntityTypeManagerInterface;
14 use Drupal\Core\Entity\EntityFieldManagerInterface;
15 use Drupal\Core\Datetime\DateFormatterInterface;
16 use Drupal\Console\Utils\DrupalApi;
17
18 /**
19  * Class ContentNode
20  *
21  * @package Drupal\Console\Utils
22  */
23 abstract class Base
24 {
25     /**
26      * @var EntityTypeManagerInterface
27      */
28     protected $entityTypeManager = null;
29
30     /**
31      * @var EntityFieldManagerInterface
32      */
33     protected $entityFieldManager = null;
34
35     /**
36      * @var DateFormatterInterface
37      */
38     protected $dateFormatter = null;
39
40     /* @var array */
41     protected $users = [];
42
43     /**
44      * @var Random $random
45      */
46     protected $random = null;
47
48     /**
49      * @var DrupalApi
50      */
51     protected $drupalApi;
52
53     /**
54      * ContentNode constructor.
55      *
56      * @param EntityTypeManagerInterface  $entityTypeManager
57      * @param EntityFieldManagerInterface $entityFieldManager
58      * @param DateFormatterInterface      $dateFormatter
59      * @param DrupalApi                   $drupalApi
60      */
61     public function __construct(
62         EntityTypeManagerInterface $entityTypeManager,
63         EntityFieldManagerInterface $entityFieldManager,
64         DateFormatterInterface $dateFormatter,
65         DrupalApi $drupalApi = null
66     ) {
67         $this->entityTypeManager = $entityTypeManager;
68         $this->entityFieldManager = $entityFieldManager;
69         $this->dateFormatter = $dateFormatter;
70         $this->drupalApi = $drupalApi;
71     }
72
73     /**
74      * @param $entity
75      * @return array
76      */
77     private function getFields($entity)
78     {
79         $entityTypeId = $entity->getEntityType()->id();
80         $bundle = $entity->bundle();
81
82         $fields = array_filter(
83             $this->entityFieldManager->getFieldDefinitions($entityTypeId, $bundle), function ($fieldDefinition) {
84                 return $fieldDefinition instanceof FieldConfigInterface;
85             }
86         );
87
88         return $fields;
89     }
90
91     /**
92      * @param
93      * @param $entity
94      */
95     protected function generateFieldSampleData($entity)
96     {
97         $fields = $this->getFields($entity);
98
99         /* @var \Drupal\field\FieldConfigInterface[] $fields */
100         foreach ($fields as $field) {
101             $fieldName = $field->getFieldStorageDefinition()->getName();
102             $cardinality = $field->getFieldStorageDefinition()->getCardinality();
103             if ($cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {
104                 $cardinality = rand(1, 5);
105             }
106
107             $entity->$fieldName->generateSampleItems($cardinality);
108         }
109     }
110
111     /**
112      * Returns the random data generator.
113      *
114      * @return \Drupal\Component\Utility\Random
115      *   The random data generator.
116      */
117     protected function getRandom()
118     {
119         if (!$this->random) {
120             $this->random = new Random();
121         }
122
123         return $this->random;
124     }
125
126     /**
127      * Retrieve a random Uid of enabled users.
128      *
129      * @return array
130      */
131     protected function getUserId()
132     {
133         if (!$this->users) {
134             $userStorage = $this->entityTypeManager->getStorage('user');
135
136             $this->users = $userStorage->loadByProperties(['status' => true]);
137         }
138
139         return array_rand($this->users);
140     }
141 }