db backup prior to drupal security update
[yaffs-website] / vendor / drupal / console / src / Utils / Create / NodeData.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Utils\Create\NodeData.
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 use Drupal\Core\Language\LanguageInterface;
14
15 /**
16  * Class Nodes
17  *
18  * @package Drupal\Console\Utils
19  */
20 class NodeData extends Base
21 {
22     /* @var array */
23     protected $bundles = [];
24
25     /**
26      * Nodes constructor.
27      *
28      * @param EntityTypeManagerInterface  $entityTypeManager
29      * @param EntityFieldManagerInterface $entityFieldManager
30      * @param DateFormatterInterface      $dateFormatter
31      * @param array                       $bundles
32      */
33     public function __construct(
34         EntityTypeManagerInterface $entityTypeManager,
35         EntityFieldManagerInterface $entityFieldManager,
36         DateFormatterInterface $dateFormatter,
37         $bundles
38     ) {
39         $this->bundles = $bundles;
40         parent::__construct(
41             $entityTypeManager,
42             $entityFieldManager,
43             $dateFormatter
44         );
45     }
46
47     /**
48      * @param $contentTypes
49      * @param $limit
50      * @param $titleWords
51      * @param $timeRange
52      *
53      * @return array
54      */
55     public function create(
56         $contentTypes,
57         $limit,
58         $titleWords,
59         $timeRange,
60         $language = LanguageInterface::LANGCODE_NOT_SPECIFIED
61     ) {
62         $nodes = [];
63         for ($i=0; $i<$limit; $i++) {
64             $contentType = $contentTypes[array_rand($contentTypes)];
65             $node = $this->entityTypeManager->getStorage('node')->create(
66                 [
67                     'nid' => null,
68                     'type' => $contentType,
69
70                     'created' => REQUEST_TIME - mt_rand(0, $timeRange),
71                     'uid' => $this->getUserID(),
72                     'title' => $this->getRandom()->sentences(mt_rand(1, $titleWords), true),
73                     'revision' => mt_rand(0, 1),
74                     'status' => true,
75                     'promote' => mt_rand(0, 1),
76                     'langcode' => $language
77                 ]
78             );
79
80             $this->generateFieldSampleData($node);
81
82             try {
83                 $node->save();
84                 $nodes['success'][] = [
85                     'nid' => $node->id(),
86                     'node_type' => $this->bundles[$contentType],
87                     'title' => $node->getTitle(),
88                     'created' => $this->dateFormatter->format(
89                         $node->getCreatedTime(),
90                         'custom',
91                         'Y-m-d h:i:s'
92                     )
93                 ];
94             } catch (\Exception $error) {
95                 $nodes['error'][] = [
96                     'node_type' =>  $this->bundles[$contentType],
97                     'title' => $node->getTitle(),
98                     'error' => $error->getMessage()
99                 ];
100             }
101         }
102
103         return $nodes;
104     }
105 }