5 use Procrastinator\HydratableTrait;
6 use Procrastinator\JsonSerializeTrait;
29 use HydratableTrait, JsonSerializeTrait;
42 public function __construct($file_path, $mime_type, $perspective = self::DEFAULT_SOURCE_PERSPECTIVE) {
44 $this->identifier = md5($file_path);
45 $this->filePath = $file_path;
46 $this->mimeType = $mime_type;
47 $this->perspective = $perspective;
49 $this->version = time();
65 if ($newVersion == $this->version) {
69 return $this->createCommon(
'version', $newVersion);
84 $new = $this->createCommon(
'perspective', $perspective);
85 $new->changeFilePath($uri);
93 $this->filePath = $newPath;
99 private function createCommon($property, $value) {
100 $current = $this->{$property};
102 $this->{$property} = $new;
103 $newResource = clone $this;
104 $this->{$property} = $current;
112 return $this->identifier;
119 return $this->filePath;
126 return dirname($this->filePath);
133 return $this->mimeType;
140 return $this->version;
147 return $this->perspective;
154 return "{$this->identifier}__{$this->version}__{$this->perspective}";
163 return $this->serialize();
179 $pieces = explode(
"__", $uid);
180 if (count($pieces) != 3) {
181 throw new \Exception(
"Badly constructed unique identifier {$uid}");
184 'identifier' => $pieces[0],
185 'version' => $pieces[1],
186 'perspective' => $pieces[2],
201 return [$parts[
'identifier'], $parts[
'version']];
203 catch (\Exception $e) {
207 if (substr_count($string,
'__') > 0) {
208 $parts = explode(
"__", $string);
209 if (count($parts) == 2) {
214 $distribution = self::getDistribution($string);
217 if (isset($distribution->data->{
"%Ref:downloadURL"})) {
218 $resource = $distribution->data->{
"%Ref:downloadURL"}[0]->data;
219 return [$resource->identifier, $resource->version];
222 throw new \Exception(
"Could not find identifier and version for {$string}");
228 private static function getDistribution($identifier) {
230 $factory = \Drupal::service(
'dkan.metastore.storage');
233 $storage = $factory->getInstance(
'distribution');
235 $distroJson = $storage->retrieve($identifier);
236 return json_decode($distroJson);