Open Data Catalog v2.0.0
Commands.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use Symfony\Component\Console\Helper\Table;
6 use Symfony\Component\Console\Output\ConsoleOutput;
7 
8 use Drush\Commands\DrushCommands;
9 
15 class Commands extends DrushCommands {
16  use Helper;
17 
23  protected $harvestFactory;
24 
30  protected $harvestService;
31 
37  protected $logger;
38 
42  public function __construct() {
43  // @todo passing via arguments doesn't seem play well with drush.services.yml
44  $this->harvestService = \Drupal::service('dkan_harvest.service');
45  $this->logger = \Drupal::service('dkan_harvest.logger_channel');
46  }
47 
56  public function index() {
57  // Each row needs to be an array for display.
58  $rows = array_map(
59  function ($id) {
60  return [$id];
61  },
62  $this->harvestService->getAllHarvestIds()
63  );
64  (new Table(new ConsoleOutput()))->setHeaders(['plan id'])->setRows($rows)->render();
65  }
66 
72  public function register($harvest_plan) {
73  try {
74  $plan = json_decode($harvest_plan);
75  $identifier = $this->harvestService
76  ->registerHarvest($plan);
77  $this->logger->notice("Successfully registered the {$identifier} harvest.");
78  }
79  catch (\Exception $e) {
80  $this->logger->error($e->getMessage());
81  $this->logger->debug($e->getTraceAsString());
82  }
83  }
84 
90  public function deregister($id) {
91  try {
92  if ($this->harvestService->deregisterHarvest($id)) {
93  $message = "Successfully deregistered the {$id} harvest.";
94  }
95  }
96  catch (\Exception $e) {
97  $message = $e->getMessage();
98  }
99 
100  (new ConsoleOutput())->write($message . PHP_EOL);
101  }
102 
114  public function run($id) {
115  $result = $this->harvestService
116  ->runHarvest($id);
117 
118  $this->renderResult($result);
119  }
120 
129  public function runAll() {
130 
131  $ids = $this->harvestService
132  ->getAllHarvestIds();;
133 
134  foreach ($ids as $id) {
135  $this->run($id);
136  }
137  }
138 
149  public function info($id, $run_id = NULL) {
150 
151  if (!isset($run_id)) {
152  $runs = $this->harvestService
153  ->getAllHarvestRunInfo($id);
154  $table = new Table(new ConsoleOutput());
155  $table->setHeaders(["{$id} runs"]);
156  foreach (array_keys($runs) as $run_id) {
157  $table->addRow([$run_id]);
158  }
159  $table->render();
160  }
161  else {
162  $run = $this->harvestService
163  ->getHarvestRunInfo($id, $run_id);
164  $result = json_decode($run);
165  print_r($result);
166  }
167 
168  }
169 
181  public function revert($id) {
182 
183  $result = $this->harvestService
184  ->revertHarvest($id);
185 
186  (new ConsoleOutput())->write("{$result} items reverted for the '{$id}' harvest plan." . PHP_EOL);
187  }
188 
189 }
Drupal\dkan_harvest\Drush\Commands\deregister
deregister($id)
Definition: Commands.php:90
Drupal\dkan_harvest\Drush\Commands\$harvestService
$harvestService
Definition: Commands.php:30
Drupal\dkan_harvest\Drush\Commands\$harvestFactory
$harvestFactory
Definition: Commands.php:23
Drupal\dkan_harvest\Drush\Commands\__construct
__construct()
Definition: Commands.php:42
Drupal\dkan_harvest\Drush\Commands\info
info($id, $run_id=NULL)
Definition: Commands.php:149
Drupal\dkan_harvest\Drush\Commands\run
run($id)
Definition: Commands.php:114
Drupal\dkan_harvest\Drush\Commands
Definition: Commands.php:15
Drupal\dkan_harvest\Drush
Definition: Commands.php:3
Drupal\dkan_harvest\Drush\Commands\revert
revert($id)
Definition: Commands.php:181
Drupal\dkan_harvest\Drush\Commands\index
index()
Definition: Commands.php:56
Drupal\dkan_harvest\Drush\Commands\runAll
runAll()
Definition: Commands.php:129
Drupal\dkan_harvest\Drush\Commands\$logger
$logger
Definition: Commands.php:37