Open Data Catalog v2.0.0
Drush.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Drupal\datastore;
4 
5 use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
6 use Consolidation\OutputFormatters\StructuredData\UnstructuredListData;
10 use Drush\Commands\DrushCommands;
11 
17 class Drush extends DrushCommands {
18  use TableTrait;
24  protected $metastoreService;
25 
31  protected $datastoreService;
32 
38  private $resourceLocalizer;
39 
43  public function __construct(
46  ResourceLocalizer $resourceLocalizer
47  ) {
48  $this->metastoreService = $metastoreService;
49  $this->datastoreService = $datastoreService;
50  $this->resourceLocalizer = $resourceLocalizer;
51  }
52 
66  public function import($uuid, $deferred = FALSE) {
67 
68  try {
69  $this->datastoreService->import($uuid, $deferred);
70  }
71  catch (\Exception $e) {
72  $this->logger->error("We were not able to load the entity with uuid {$uuid}");
73  $this->logger->debug($e->getMessage());
74  }
75  }
76 
96  public function list($options = [
97  'format' => 'table',
98  'status' => NULL,
99  'uuid-only' => FALSE,
100  ]) {
101  $status = $options['status'];
102  $uuid_only = $options['uuid-only'];
103 
104  $list = $this->datastoreService->list();
105  $rows = [];
106  foreach ($list as $uuid => $item) {
107  $rows[] = $this->createRow($uuid, $item);
108  }
109 
110  if (!empty($status)) {
111  $rows = array_filter($rows, function ($row) use ($status) {
112  if ($row['fileFetcherStatus'] == $status || $row['importerStatus'] == $status) {
113  return TRUE;
114  }
115  return FALSE;
116  });
117  }
118 
119  if ($uuid_only) {
120  foreach ($rows as $index => $row) {
121  $rows[$index] = $row['uuid'];
122  }
123  return new UnstructuredListData($rows);
124  }
125 
126  return new RowsOfFields($rows);
127  }
128 
132  private function createRow($uuid, $item) {
133  return [
134  'uuid' => $uuid,
135  'fileName' => $item->fileName,
136  'fileFetcherStatus' => $item->fileFetcherStatus,
137  'fileFetcherBytes' => \format_size($item->fileFetcherBytes) . " ($item->fileFetcherPercentDone%)",
138  'importerStatus' => $item->importerStatus,
139  'importerBytes' => \format_size($item->importerBytes) . " ($item->importerPercentDone%)",
140  ];
141  }
142 
153  public function drop($uuid) {
154  try {
155  $this->datastoreService->drop($uuid);
156  $this->logger->notice("Successfully dropped the datastore for {$uuid}");
157  }
158  catch (\Exception $e) {
159  $this->logger->error("Unable to find an entity with uuid {$uuid}");
160  $this->logger->debug($e->getMessage());
161  return;
162  }
163  catch (\TypeError $e) {
164  // This will catch all TypeErrors with all arguments. Since we only
165  // pass the UUID to the DataStore::Service::drop method we can safely
166  // assume the issue is with the uuid.
167  $this->logger->error("Unexpected entity uuid.");
168  $this->logger->debug($e->getMessage());
169  return;
170  }
171 
172  $this->jobstorePrune($uuid);
173  }
174 
180  public function dropAll() {
181  foreach ($this->metastoreService->getAll('distribution') as $distribution) {
182  $uuid = $distribution->data->{"%Ref:downloadURL"}[0]->data->identifier;
183  $this->drop($uuid);
184  }
185  }
186 
187 }
Drupal\datastore\Drush\$metastoreService
$metastoreService
Definition: Drush.php:24
Drupal\datastore\Drush
Definition: Drush.php:17
Drupal\datastore\Service\ResourceLocalizer
Definition: ResourceLocalizer.php:23
Drupal\metastore\Service
Definition: Uuid5.php:5
Drupal\datastore
Drupal\datastore\Drush\__construct
__construct(Metastore $metastoreService, Datastore $datastoreService, ResourceLocalizer $resourceLocalizer)
Definition: Drush.php:43
Drupal\datastore\Drush\list
list($options=[ 'format'=> 'table', 'status'=> NULL, 'uuid-only'=> FALSE,])
Definition: Drush.php:96
Drupal\datastore\Drush\dropAll
dropAll()
Definition: Drush.php:180
Drupal\datastore\Service
Definition: DatastoreQuery.php:3
Drupal\datastore\Drush\$datastoreService
$datastoreService
Definition: Drush.php:31
Drupal\datastore\Drush\drop
drop($uuid)
Definition: Drush.php:153