Open Data Catalog v2.0.0
Harvester.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Drupal\dkan_harvest;
4 
5 use Harvest\Harvester as DkanHarvester;
6 use Contracts\BulkRetrieverInterface;
7 use Contracts\FactoryInterface;
8 use Contracts\StorerInterface;
9 use Harvest\ETL\Factory;
10 
14 class Harvester {
15 
16  private $storeFactory;
17 
21  public function __construct(FactoryInterface $storeFactory) {
22  $this->storeFactory = $storeFactory;
23  }
24 
31  public function getAllHarvestIds() {
32  $store = $this->storeFactory->getInstance("harvest_plans");
33 
34  if ($store instanceof BulkRetrieverInterface) {
35  return $store->retrieveAll();
36  }
37  throw new \Exception("The store created by {get_class($this->storeFactory)} does not implement {BulkRetrieverInterface::class}");
38  }
39 
51  public function getHarvestPlan($plan_id) {
52  $store = $this->storeFactory->getInstance("harvest_plans");
53 
54  if ($store instanceof BulkRetrieverInterface) {
55  return $store->retrieve($plan_id);
56  }
57  throw new \Exception("The store created by {get_class($this->storeFactory)} does not implement {RetrieverInterface::class}");
58  }
59 
72  public function registerHarvest($plan) {
73 
74  $this->validateHarvestPlan($plan);
75 
76  $store = $this->storeFactory->getInstance("harvest_plans");
77 
78  if ($store instanceof StorerInterface) {
79  return $store->store(json_encode($plan), $plan->identifier);
80  }
81  throw new \Exception("The store created by {get_class($this->storeFactory)} does not implement {StorerInterface::class}");
82  }
83 
93  public function deregisterHarvest(string $id) {
94  $this->revertHarvest($id);
95 
96  $plan_store = $this->storeFactory->getInstance("harvest_plans");
97 
98  return $plan_store->remove($id);
99  }
100 
106  public function revertHarvest($id) {
107  $run_store = $this->storeFactory->getInstance("harvest_{$id}_runs");
108  if (!method_exists($run_store, "destroy")) {
109  throw new \Exception("Storage of class " . get_class($run_store) . " does not implement destroy method.");
110  }
111  $run_store->destroy();
112  $harvester = $this->getHarvester($id);
113  return $harvester->revert();
114  }
115 
119  public function runHarvest($id) {
120  $harvester = $this->getHarvester($id);
121 
122  $result = $harvester->harvest();
123 
124  $run_store = $this->storeFactory->getInstance("harvest_{$id}_runs");
125  $current_time = time();
126  $run_store->store(json_encode($result), "{$current_time}");
127 
128  return $result;
129  }
130 
137  public function getHarvestRunInfo($id, $runId) {
138  $allRuns = $this->getAllHarvestRunInfo($id);
139  $found = array_search($runId, $allRuns);
140 
141  if ($found !== FALSE) {
142  $run_store = $this->storeFactory->getInstance("harvest_{$id}_runs");
143  return $run_store->retrieve($runId);
144  }
145 
146  return FALSE;
147  }
148 
152  public function getAllHarvestRunInfo($id) {
153  $run_store = $this->storeFactory->getInstance("harvest_{$id}_runs");
154  $runs = $run_store->retrieveAll();
155  return $runs;
156  }
157 
167  public function validateHarvestPlan($plan) {
168  return Factory::validateHarvestPlan($plan);
169  }
170 
174  private function getHarvester($id) {
175  $plan_store = $this->storeFactory->getInstance("harvest_plans");
176  $harvestPlan = json_decode($plan_store->retrieve($id));
177  $item_store = $this->storeFactory->getInstance("harvest_{$id}_items");
178  $hash_store = $this->storeFactory->getInstance("harvest_{$id}_hashes");
179  return $this->getDkanHarvesterInstance($harvestPlan, $item_store, $hash_store);
180  }
181 
187  protected function getDkanHarvesterInstance($harvestPlan, $item_store, $hash_store) {
188  return new DkanHarvester(new Factory($harvestPlan, $item_store, $hash_store));
189  }
190 
191 }
Drupal\dkan_harvest\Harvester\getAllHarvestIds
getAllHarvestIds()
Definition: Harvester.php:31
Drupal\dkan_harvest\Harvester\__construct
__construct(FactoryInterface $storeFactory)
Definition: Harvester.php:21
Drupal\dkan_harvest\Harvester\getDkanHarvesterInstance
getDkanHarvesterInstance($harvestPlan, $item_store, $hash_store)
Definition: Harvester.php:187
Drupal\dkan_harvest\Harvester\getAllHarvestRunInfo
getAllHarvestRunInfo($id)
Definition: Harvester.php:152
Drupal\dkan_harvest
Drupal\dkan_harvest\Harvester\getHarvestRunInfo
getHarvestRunInfo($id, $runId)
Definition: Harvester.php:137
Drupal\dkan_harvest\Harvester\deregisterHarvest
deregisterHarvest(string $id)
Definition: Harvester.php:93
Drupal\dkan_harvest\Harvester
Definition: Harvester.php:14
Drupal\dkan_harvest\Harvester\getHarvestPlan
getHarvestPlan($plan_id)
Definition: Harvester.php:51
Drupal\dkan_harvest\Harvester\registerHarvest
registerHarvest($plan)
Definition: Harvester.php:72
Drupal\dkan_harvest\Harvester\revertHarvest
revertHarvest($id)
Definition: Harvester.php:106
Drupal\dkan_harvest\Harvester\runHarvest
runHarvest($id)
Definition: Harvester.php:119
Drupal\dkan_harvest\Harvester\validateHarvestPlan
validateHarvestPlan($plan)
Definition: Harvester.php:167