5 use Harvest\Harvester as DkanHarvester;
6 use Contracts\BulkRetrieverInterface;
7 use Contracts\FactoryInterface;
8 use Contracts\StorerInterface;
9 use Harvest\ETL\Factory;
16 private $storeFactory;
22 $this->storeFactory = $storeFactory;
32 $store = $this->storeFactory->getInstance(
"harvest_plans");
34 if ($store instanceof BulkRetrieverInterface) {
35 return $store->retrieveAll();
37 throw new \Exception(
"The store created by {get_class($this->storeFactory)} does not implement {BulkRetrieverInterface::class}");
52 $store = $this->storeFactory->getInstance(
"harvest_plans");
54 if ($store instanceof BulkRetrieverInterface) {
55 return $store->retrieve($plan_id);
57 throw new \Exception(
"The store created by {get_class($this->storeFactory)} does not implement {RetrieverInterface::class}");
76 $store = $this->storeFactory->getInstance(
"harvest_plans");
78 if ($store instanceof StorerInterface) {
79 return $store->store(json_encode($plan), $plan->identifier);
81 throw new \Exception(
"The store created by {get_class($this->storeFactory)} does not implement {StorerInterface::class}");
96 $plan_store = $this->storeFactory->getInstance(
"harvest_plans");
98 return $plan_store->remove($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.");
111 $run_store->destroy();
112 $harvester = $this->getHarvester($id);
113 return $harvester->revert();
120 $harvester = $this->getHarvester($id);
122 $result = $harvester->harvest();
124 $run_store = $this->storeFactory->getInstance(
"harvest_{$id}_runs");
125 $current_time = time();
126 $run_store->store(json_encode($result),
"{$current_time}");
139 $found = array_search($runId, $allRuns);
141 if ($found !== FALSE) {
142 $run_store = $this->storeFactory->getInstance(
"harvest_{$id}_runs");
143 return $run_store->retrieve($runId);
153 $run_store = $this->storeFactory->getInstance(
"harvest_{$id}_runs");
154 $runs = $run_store->retrieveAll();
168 return Factory::validateHarvestPlan($plan);
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");
188 return new DkanHarvester(
new Factory($harvestPlan, $item_store, $hash_store));