Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Commit

Permalink
Merge 290201d into 02d35ff
Browse files Browse the repository at this point in the history
  • Loading branch information
Jalle19 committed Apr 25, 2018
2 parents 02d35ff + 290201d commit 87c605e
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 14 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -21,6 +21,10 @@ synchronize content from Contentful to your application.
* a middleware for proper New Relic transaction name instrumentation for webhook requests
* asynchronous processing support by using jobs

### Requirements

* PHP >= 7.1

## Installation

1. Start by adding the library as a dependency to your application:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -14,7 +14,7 @@
"php": ">=7.1",
"jalle19/laravel-lost-interfaces": "^1.1",
"laravel/lumen-framework": "^5.4",
"nordsoftware/lumen-contentful": "^1.1",
"nordsoftware/lumen-contentful": "^2.0",
"nordsoftware/lumen-newrelic": "^1.1",
"digiaonline/json-helpers": "^1.0"
},
Expand Down
3 changes: 0 additions & 3 deletions src/Console/Commands/SyncAssetsCommand.php
Expand Up @@ -2,9 +2,7 @@

namespace Digia\Lumen\ContentfulSync\Console\Commands;

use Contentful\Delivery\Asset;
use Contentful\Delivery\Query;
use Contentful\ResourceArray;
use Digia\JsonHelpers\JsonEncoder;

/**
Expand Down Expand Up @@ -50,7 +48,6 @@ public function handle()
$this->output->progressStart($this->getClient()->getAssets($this->getTotalQuery())->getTotal());

do {
/** @var Asset[]|ResourceArray $assets */
$assets = $this->getClient()->getAssets($this->getQuery());

// Process the current batch
Expand Down
43 changes: 38 additions & 5 deletions src/Http/Controllers/ContentfulSyncController.php
Expand Up @@ -2,6 +2,8 @@

namespace Digia\Lumen\ContentfulSync\Http\Controllers;

use Contentful\Core\Resource\ResourceInterface;
use Contentful\Delivery\SystemProperties;
use Digia\Lumen\ContentfulSync\Contracts\ContentfulSyncServiceContract;
use Digia\Lumen\ContentfulSync\Exceptions\ContentfulSyncException;
use Digia\Lumen\ContentfulSync\Http\Middleware\NewRelicMiddleware;
Expand Down Expand Up @@ -54,14 +56,15 @@ public function __construct(
*
* @return Response
*
* @throws \InvalidArgumentException if the space or environment ID is invalid
* @throws ContentfulSyncException if the webhook cannot be handled
*/
public function handleIncomingWebhook(Request $request): Response
{
$requestContent = (string)$request->getContent();

// Parse the payload into a Contentful SDK resource object
$resource = $this->contentfulService->getClient()->reviveJson($requestContent);
$resource = $this->contentfulService->getClient()->parseJson($requestContent);

// Instrument the request so the middleware can do its job
$contentfulTopic = $this->getContentfulTopic($request);
Expand All @@ -74,22 +77,22 @@ public function handleIncomingWebhook(Request $request): Response
break;
case self::TOPIC_CONTENT_MANAGEMENT_ASSET_UNPUBLISH:
case self::TOPIC_CONTENT_MANAGEMENT_ASSET_DELETE:
$this->contentfulSyncService->deleteAsset($resource->getId());
$this->contentfulSyncService->deleteAsset($this->getResourceId($resource));
break;
case self::TOPIC_CONTENT_MANAGEMENT_ENTRY_PUBLISH:
$contentType = $resource->getContentType()->getId();
$contentType = $this->getResourceContentType($resource);

$request->attributes->set(NewRelicMiddleware::ATTRIBUTE_CONTENT_TYPE, $contentType);

$this->contentfulSyncService->publishEntry($contentType, $requestContent);
break;
case self::TOPIC_CONTENT_MANAGEMENT_ENTRY_UNPUBLISH:
case self::TOPIC_CONTENT_MANAGEMENT_ENTRY_DELETE:
$contentType = $resource->getContentType()->getId();
$contentType = $this->getResourceContentType($resource);

$request->attributes->set(NewRelicMiddleware::ATTRIBUTE_CONTENT_TYPE, $contentType);

$this->contentfulSyncService->deleteEntry($contentType, $resource->getId());
$this->contentfulSyncService->deleteEntry($contentType, $this->getResourceId($resource));
break;
default:
throw new ContentfulSyncException(sprintf('Unknown topic "%s"', $contentfulTopic));
Expand All @@ -98,6 +101,36 @@ public function handleIncomingWebhook(Request $request): Response
return new Response();
}

/**
* @param ResourceInterface $resource
*
* @return string
*
* @throws \InvalidArgumentException
*/
private function getResourceContentType(ResourceInterface $resource): string
{
/** @var SystemProperties $systemProperties */
$systemProperties = $resource->getSystemProperties();
$contentType = $systemProperties->getContentType();

if ($contentType === null) {
throw new \InvalidArgumentException('Resource does not have a content type');
}

return $contentType->getId();
}

/**
* @param ResourceInterface $resource
*
* @return string
*/
private function getResourceId(ResourceInterface $resource): string
{
return $resource->getSystemProperties()->getId();
}

/**
* @param Request $request
*
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Middleware/NewRelicMiddleware.php
Expand Up @@ -31,7 +31,7 @@ public function getTransactionName(Request $request): string
return "{$topic}@{$contentType}";
}

return "{$topic}";
return $topic;
}

}
6 changes: 2 additions & 4 deletions src/Http/Middleware/WebhookAuthenticationMiddleware.php
Expand Up @@ -42,10 +42,8 @@ public function __construct(string $expectedUsername, string $expectedPassword)
*/
public function handle(Request $request, \Closure $next)
{
if ($request->hasHeader('Authorization')) {
if ($request->header('Authorization') === $this->getExpectedAuthorizationLine()) {
return $next($request);
}
if ($request->hasHeader('Authorization') && $request->header('Authorization') === $this->getExpectedAuthorizationLine()) {
return $next($request);
}

throw new \InvalidArgumentException('Invalid webhook authentication');
Expand Down

0 comments on commit 87c605e

Please sign in to comment.