-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to generate asynchronously using webhooks
- Loading branch information
1 parent
de08d60
commit 13eb438
Showing
21 changed files
with
680 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Sensiolabs\GotenbergBundle\Builder; | ||
|
||
interface AsyncBuilderInterface | ||
{ | ||
/** | ||
* Generates a file asynchronously. | ||
*/ | ||
public function generateAsync(): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
namespace Sensiolabs\GotenbergBundle\Builder; | ||
|
||
use Sensiolabs\GotenbergBundle\DependencyInjection\WebhookConfiguration\WebhookConfigurationRegistry; | ||
use Sensiolabs\GotenbergBundle\Exception\WebhookConfigurationException; | ||
|
||
trait AsyncBuilderTrait | ||
{ | ||
use DefaultBuilderTrait; | ||
private string $webhookUrl; | ||
private string $errorWebhookUrl; | ||
/** | ||
* @var array<string, mixed> | ||
*/ | ||
private array $webhookExtraHeaders = []; | ||
private \Closure $operationIdGenerator; | ||
private WebhookConfigurationRegistry|null $webhookConfigurationRegistry = null; | ||
|
||
public function generateAsync(): string | ||
{ | ||
$operationId = ($this->operationIdGenerator ?? self::defaultOperationIdGenerator(...))(); | ||
$this->logger?->debug('Generating a file asynchronously with operation id {sensiolabs_gotenberg.operation_id} using {sensiolabs_gotenberg.builder} builder.', [ | ||
'sensiolabs_gotenberg.operation_id' => $operationId, | ||
'sensiolabs_gotenberg.builder' => $this::class, | ||
]); | ||
|
||
$this->webhookExtraHeaders['X-Gotenberg-Operation-Id'] = $operationId; | ||
$headers = [ | ||
'Gotenberg-Webhook-Url' => $this->webhookUrl, | ||
'Gotenberg-Webhook-Error-Url' => $this->errorWebhookUrl, | ||
'Gotenberg-Webhook-Extra-Http-Headers' => json_encode($this->webhookExtraHeaders, \JSON_THROW_ON_ERROR), | ||
]; | ||
if (null !== $this->fileName) { | ||
$headers['Gotenberg-Output-Filename'] = basename($this->fileName, '.pdf'); | ||
} | ||
$this->client->call($this->getEndpoint(), $this->getMultipartFormData(), $headers); | ||
|
||
return $operationId; | ||
} | ||
|
||
public function setWebhookConfigurationRegistry(WebhookConfigurationRegistry $registry): static | ||
{ | ||
$this->webhookConfigurationRegistry = $registry; | ||
|
||
return $this; | ||
} | ||
|
||
public function webhookConfiguration(string $webhook): static | ||
{ | ||
if (null === $this->webhookConfigurationRegistry) { | ||
throw new WebhookConfigurationException('The WebhookConfigurationRegistry is not available.'); | ||
} | ||
$webhookConfiguration = $this->webhookConfigurationRegistry->get($webhook); | ||
|
||
return $this->webhookUrls($webhookConfiguration['success'], $webhookConfiguration['error']); | ||
} | ||
|
||
public function webhookUrls(string $successWebhook, string|null $errorWebhook = null): static | ||
{ | ||
$this->webhookUrl = $successWebhook; | ||
$this->errorWebhookUrl = $errorWebhook ?? $successWebhook; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param array<string, mixed> $extraHeaders | ||
*/ | ||
public function webhookExtraHeaders(array $extraHeaders): static | ||
{ | ||
$this->webhookExtraHeaders = array_merge($this->webhookExtraHeaders, $extraHeaders); | ||
|
||
return $this; | ||
} | ||
|
||
public function operationIdGenerator(\Closure $operationIdGenerator): static | ||
{ | ||
$this->operationIdGenerator = $operationIdGenerator; | ||
|
||
return $this; | ||
} | ||
|
||
protected static function defaultOperationIdGenerator(): string | ||
{ | ||
return 'gotenberg_'.bin2hex(random_bytes(16)).microtime(true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.