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