db backup prior to drupal security update
[yaffs-website] / vendor / drupal / console / src / Utils / Create / VocabularyData.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Utils\Create\VocabularyData.
6  */
7
8 namespace Drupal\Console\Utils\Create;
9
10 use Drupal\Component\Utility\Unicode;
11 use Drupal\Core\Entity\EntityTypeManagerInterface;
12 use Drupal\Core\Entity\EntityFieldManagerInterface;
13 use Drupal\Core\Datetime\DateFormatterInterface;
14 use Drupal\Core\Language\LanguageInterface;
15
16 /**
17  * Class Vocabularies
18  *
19  * @package Drupal\Console\Utils
20  */
21 class VocabularyData extends Base
22 {
23     /**
24      * Vocabularies constructor.
25      *
26      * @param EntityTypeManagerInterface  $entityManager
27      * @param EntityFieldManagerInterface $entityFieldManager
28      * @param DateFormatterInterface      $dateFormatter
29      */
30     public function __construct(
31         EntityTypeManagerInterface $entityManager,
32         EntityFieldManagerInterface $entityFieldManager,
33         DateFormatterInterface $dateFormatter
34     ) {
35         parent::__construct(
36             $entityManager,
37             $entityFieldManager,
38             $dateFormatter
39         );
40     }
41
42     /**
43      * Create and returns an array of new Vocabularies.
44      *
45      * @param $limit
46      * @param $nameWords
47      *
48      * @return array
49      */
50     public function create(
51         $limit,
52         $nameWords
53     ) {
54         $vocabularies = [];
55         for ($i=0; $i<$limit; $i++) {
56
57             // Create a vocabulary.
58             $vocabulary = $this->entityTypeManager->getStorage('taxonomy_vocabulary')->create(
59                 [
60                     'name' => $this->getRandom()->sentences(mt_rand(1, $nameWords), true),
61                     'description' => $this->getRandom()->sentences(),
62                     'vid' => Unicode::strtolower($this->getRandom()->name()),
63                     'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
64                     'weight' => mt_rand(0, 10),
65                 ]
66             );
67
68             try {
69                 $vocabulary->save();
70                 $vocabularies['success'][] = [
71                     'vid' => $vocabulary->id(),
72                     'vocabulary' => $vocabulary->get('name'),
73                 ];
74             } catch (\Exception $error) {
75                 $vocabularies['error'][] = [
76                     'vid' => $vocabulary->id(),
77                     'name' => $vocabulary->get('name'),
78                     'error' => $error->getMessage()
79                 ];
80             }
81         }
82
83         return $vocabularies;
84     }
85 }