Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Giving Milline a proper api and not just accepting event messages who… #48

Merged
merged 6 commits into from
Aug 15, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
205 changes: 17 additions & 188 deletions Milliner/src/Controller/MillinerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,40 +36,17 @@ public function __construct(MillinerServiceInterface $milliner, LoggerInterface
}

/**
* @param string $uuid
* @param \Symfony\Component\HttpFoundation\Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function saveContent(Request $request)
public function saveNode($uuid, Request $request)
{
if (0 !== strpos($request->headers->get('Content-Type'), 'application/ld+json')) {
return new Response(
"Expecting 'Content-Type' of 'application/ld+json'",
400
);
}

$token = $request->headers->get("Authorization", null);

$event = json_decode($request->getContent(), true);

$uuid = $this->parseUuid($event);
if (empty($uuid)) {
return new Response(
"Could not parse UUID from request body",
400
);
}

$jsonld_url = $this->parseJsonldUrl($event);
if (empty($jsonld_url)) {
return new Response(
"Could not parse JSONLD url from request body",
400
);
}
$jsonld_url = $request->headers->get("Content-Location");

try {
$response = $this->milliner->saveContent(
$response = $this->milliner->saveNode(
$uuid,
$jsonld_url,
$token
Expand All @@ -85,141 +62,18 @@ public function saveContent(Request $request)
}
}

protected function parseUuid(array $event)
{
if (!isset($event['object']) || !isset($event['object']['id'])) {
return null;
}

$urn = $event['object']['id'];
if (preg_match("/urn:uuid:(?<uuid>.*)/", $urn, $matches)) {
if (!empty($matches['uuid'])) {
return $matches['uuid'];
}
}

return null;
}

protected function parseJsonldUrl(array $event)
{
$filtered = array_filter($event['object']['url'], function ($elem) {
return $elem['mediaType'] == 'application/ld+json';
});
if ($url = reset($filtered)) {
return $url['href'];
}
return null;
}

/**
* @param \Symfony\Component\HttpFoundation\Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function saveMedia(Request $request)
{
if (0 !== strpos($request->headers->get('Content-Type'), 'application/ld+json')) {
return new Response(
"Expecting 'Content-Type' of 'application/ld+json'",
400
);
}

$token = $request->headers->get("Authorization", null);

$event = json_decode($request->getContent(), true);

$jsonld_url = $this->parseJsonldUrl($event);
if (empty($jsonld_url)) {
return new Response(
"Could not parse JSONLD url from request body",
400
);
}

$json_url = $this->parseJsonUrl($event);
if (empty($json_url)) {
return new Response(
"Could not parse JSON url from request body",
400
);
}

try {
$response = $this->milliner->saveMedia(
$json_url,
$jsonld_url,
$token
);

return new Response(
$response->getBody(),
$response->getStatusCode()
);
} catch (\Exception $e) {
$this->log->error("", ['Exception' => $e]);
$code = $e->getCode() == 0 ? 500 : $e->getCode();
return new Response($e->getMessage(), $code);
}
}

protected function parseJsonUrl(array $event)
{
$filtered = array_filter($event['object']['url'], function ($elem) {
return $elem['mediaType'] == 'application/json';
});
if ($url = reset($filtered)) {
return $url['href'];
}
return null;
}

/**
* @param string $uuid
* @param \Symfony\Component\HttpFoundation\Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function saveFile(Request $request)
public function deleteNode($uuid, Request $request)
{
if (0 !== strpos($request->headers->get('Content-Type'), 'application/ld+json')) {
return new Response(
"Expecting 'Content-Type' of 'application/ld+json'",
400
);
}

$token = $request->headers->get("Authorization", null);

$event = json_decode($request->getContent(), true);

$uuid = $this->parseUuid($event);
if (empty($uuid)) {
return new Response(
"Could not parse UUID from request body",
400
);
}

$file_url = $this->parseFileUrl($event);
if (empty($file_url)) {
return new Response(
"Could not parse Drupal File URL from request body",
400
);
}

$checksum_url = $this->parseChecksumUrl($event);
if (empty($checksum_url)) {
return new Response(
"Could not parse Drupal File Checksum URL from request body",
400
);
}

try {
$response = $this->milliner->saveFile(
$response = $this->milliner->deleteNode(
$uuid,
$file_url,
$checksum_url,
$token
);

Expand All @@ -234,47 +88,20 @@ public function saveFile(Request $request)
}

/**
* @param array $event
* @return string|null
*/
protected function parseFileUrl(array $event)
{
$filtered = array_filter($event['object']['url'], function ($elem) {
return $elem['name'] == 'File';
});
if ($url = reset($filtered)) {
return $url['href'];
}
return null;
}

/**
* @param array $event
* @return string|null
*/
protected function parseChecksumUrl(array $event)
{
$filtered = array_filter($event['object']['url'], function ($elem) {
return $elem['name'] == 'Checksum';
});
if ($url = reset($filtered)) {
return $url['href'];
}
return null;
}

/**
* @param string $uuid
* @param string $source_field
* @param \Symfony\Component\HttpFoundation\Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function delete($uuid, Request $request)
public function saveMedia($source_field, Request $request)
{
$this->log->debug("YOU GET HERE YET?");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to take that line out or make it a proper message.

$token = $request->headers->get("Authorization", null);
$json_url = $request->headers->get("Content-Location");

try {
$response = $this->milliner->delete(
$uuid,
$response = $this->milliner->saveMedia(
$source_field,
$json_url,
$token
);

Expand All @@ -284,7 +111,9 @@ public function delete($uuid, Request $request)
);
} catch (\Exception $e) {
$this->log->error("", ['Exception' => $e]);
return new Response($e->getMessage(), $e->getCode());
$code = $e->getCode() == 0 ? 500 : $e->getCode();
return new Response($e->getMessage(), $code);
}
}

}
Loading