Open Data Catalog v2.0.0
Drush.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Drupal\dkan_datastore;
4 
5 use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
6 use Consolidation\OutputFormatters\StructuredData\UnstructuredListData;
8 use Drush\Commands\DrushCommands;
9 
15 class Drush extends DrushCommands {
16 
22  protected $datastoreService;
23 
29  protected $logger;
30 
34  public function __construct() {
35  $this->datastoreService = \Drupal::service('dkan_datastore.service');
36  $this->logger = \Drupal::service('dkan_datastore.logger_channel');
37  }
38 
50  public function import($uuid, $deferred = FALSE) {
51 
52  try {
53  // Load metadata with both identifier and data for this request.
54  drupal_static('dkan_data_dereference_method', Dereferencer::DEREFERENCE_OUTPUT_REFERENCE_IDS);
55 
56  $this->datastoreService->import($uuid, $deferred);
57  }
58  catch (\Exception $e) {
59  $this->logger->error("We were not able to load the entity with uuid {$uuid}");
60  $this->logger->debug($e->getMessage());
61  }
62  }
63 
81  public function list($options = [
82  'format' => 'table',
83  'status' => NULL,
84  'uuid-only' => FALSE,
85  ]) {
86  $status = $options['status'];
87  $uuid_only = $options['uuid-only'];
88 
89  $list = $this->datastoreService->list();
90  $rows = [];
91  foreach ($list as $uuid => $item) {
92  $rows[] = $this->createRow($uuid, $item);
93  }
94 
95  if (!empty($status)) {
96  $rows = array_filter($rows, function ($row) use ($status) {
97  if ($row['fileFetcherStatus'] == $status || $row['importerStatus'] == $status) {
98  return TRUE;
99  }
100  return FALSE;
101  });
102  }
103 
104  if ($uuid_only) {
105  foreach ($rows as $index => $row) {
106  $rows[$index] = $row['uuid'];
107  }
108  return new UnstructuredListData($rows);
109  }
110 
111  return new RowsOfFields($rows);
112  }
113 
117  private function createRow($uuid, $item) {
118  return [
119  'uuid' => $uuid,
120  'fileName' => $item->fileName,
121  'fileFetcherStatus' => $item->fileFetcherStatus,
122  'fileFetcherBytes' => \format_size($item->fileFetcherBytes) . " ($item->fileFetcherPercentDone%)",
123  'importerStatus' => $item->importerStatus,
124  'importerBytes' => \format_size($item->importerBytes) . " ($item->importerPercentDone%)",
125  ];
126  }
127 
136  public function drop($uuid) {
137  try {
138  // Load metadata with both identifier and data for this request.
139  drupal_static('dkan_data_dereference_method', Dereferencer::DEREFERENCE_OUTPUT_REFERENCE_IDS);
140  $this->datastoreService->drop($uuid);
141  }
142  catch (\Exception $e) {
143  $this->logger->error("We were not able to load the entity with uuid {$uuid}");
144  $this->logger->debug($e->getMessage());
145  }
146  }
147 
148 }
Drupal\dkan_datastore\Drush
Definition: Drush.php:15
Drupal\dkan_datastore
Definition: DataNodeLifeCycle.php:3
Drupal\dkan_data\Reference\Dereferencer\DEREFERENCE_OUTPUT_REFERENCE_IDS
const DEREFERENCE_OUTPUT_REFERENCE_IDS
Definition: Dereferencer.php:29
Drupal\dkan_datastore\Drush\__construct
__construct()
Definition: Drush.php:34
Drupal\dkan_datastore\Drush\list
list($options=[ 'format'=> 'table', 'status'=> NULL, 'uuid-only'=> FALSE,])
Definition: Drush.php:81
Drupal\dkan_datastore\Drush\$datastoreService
$datastoreService
Definition: Drush.php:22
Drupal\dkan_datastore\Drush\$logger
$logger
Definition: Drush.php:29
Drupal\dkan_data\Reference\Dereferencer
Definition: Dereferencer.php:13
Drupal\dkan_datastore\Drush\drop
drop($uuid)
Definition: Drush.php:136