Skip to content

Commit cd7cf12

Browse files
feat (Event): Dispatch a new PreSendEnvelopeEvent before sending the envelope to DocuSign (#119)
1 parent 3820325 commit cd7cf12

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

doc/events.md

+12
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace App\EventSubscriber;
1515

1616
use DocusignBundle\EnvelopeBuilder;
1717
use DocusignBundle\Events\PreSignEvent;
18+
use DocusignBundle\Events\PreSendEnvelopeEvent;
1819
use DocusignBundle\Events\DocumentSignatureCompletedEvent;
1920
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
2021

@@ -25,6 +26,7 @@ class PreSignSubscriber implements EventSubscriberInterface
2526
// return the subscribed events, their methods and priorities
2627
return [
2728
PreSignEvent::class => 'preSign',
29+
PreSendEnvelopeEvent::class => 'preSendEnvelope',
2830
DocumentSignatureCompletedEvent::class => 'onDocumentSignatureCompleted'
2931
];
3032
}
@@ -39,6 +41,16 @@ class PreSignSubscriber implements EventSubscriberInterface
3941
// $envelopeBuilder->setCallbackParameters();
4042
// ...
4143
}
44+
45+
public function preSendEnvelope(PreSendEnvelopeEvent $preSendEnvelope)
46+
{
47+
// Here you can manipulate the EnvelopeBuilder and do some adjustment to the envelope before being sent to DocuSign.
48+
$envelopeBuilder = $preSendEnvelope->getEnvelopeBuilder();
49+
50+
// $envelopeDefinition = $envelopeBuilder->getEnvelopeDefinition([]);
51+
// ...
52+
// $envelopeBuilder->setEnvelopeDefinition($envelopeDefinition);
53+
}
4254

4355
public function onDocumentSignatureCompleted(DocumentSignatureCompletedEvent $documentSignatureCompleted)
4456
{

src/EnvelopeCreator/SendEnvelope.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,24 @@
1717
use DocuSign\eSign\ApiClient;
1818
use DocuSign\eSign\Configuration;
1919
use DocusignBundle\EnvelopeBuilderInterface;
20+
use DocusignBundle\Events\PreSendEnvelopeEvent;
2021
use DocusignBundle\Grant\GrantInterface;
22+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
2123
use Symfony\Component\Routing\RouterInterface;
2224

2325
final class SendEnvelope implements EnvelopeBuilderCallableInterface
2426
{
2527
public $grant;
2628
private $router;
2729
private $envelopeBuilder;
30+
private $eventDispatcher;
2831

29-
public function __construct(EnvelopeBuilderInterface $envelopeBuilder, GrantInterface $grant, RouterInterface $router)
32+
public function __construct(EnvelopeBuilderInterface $envelopeBuilder, GrantInterface $grant, RouterInterface $router, EventDispatcherInterface $eventDispatcher)
3033
{
3134
$this->grant = $grant;
3235
$this->router = $router;
3336
$this->envelopeBuilder = $envelopeBuilder;
37+
$this->eventDispatcher = $eventDispatcher;
3438
}
3539

3640
/**
@@ -45,6 +49,10 @@ public function __invoke(array $context = [])
4549
}
4650

4751
$this->envelopeBuilder->setEnvelopesApi($this->setUpConfiguration());
52+
53+
$this->eventDispatcher->dispatch($preSendEnvelopeEvent = new PreSendEnvelopeEvent($this->envelopeBuilder));
54+
$this->envelopeBuilder = $preSendEnvelopeEvent->getEnvelopeBuilder();
55+
4856
$this->envelopeBuilder->setEnvelopeId($this->envelopeBuilder->getEnvelopesApi()->createEnvelope((string) $this->envelopeBuilder->getAccountId(), $this->envelopeBuilder->getEnvelopeDefinition())->getEnvelopeId());
4957
}
5058

src/Events/PreSendEnvelopeEvent.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the DocusignBundle.
5+
*
6+
* (c) Grégoire Hébert <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace DocusignBundle\Events;
15+
16+
use DocusignBundle\EnvelopeBuilderInterface;
17+
use Symfony\Contracts\EventDispatcher\Event;
18+
19+
class PreSendEnvelopeEvent extends Event
20+
{
21+
private $envelopeBuilder;
22+
23+
public function __construct(EnvelopeBuilderInterface $envelopeBuilder)
24+
{
25+
$this->envelopeBuilder = $envelopeBuilder;
26+
}
27+
28+
public function getEnvelopeBuilder(): EnvelopeBuilderInterface
29+
{
30+
return $this->envelopeBuilder;
31+
}
32+
}

tests/EnvelopeCreator/SendEnvelopeTest.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
use DocuSign\eSign\Model\EnvelopeSummary;
1919
use DocusignBundle\EnvelopeBuilderInterface;
2020
use DocusignBundle\EnvelopeCreator\SendEnvelope;
21+
use DocusignBundle\Events\PreSendEnvelopeEvent;
2122
use DocusignBundle\Grant\GrantInterface;
2223
use DocusignBundle\Tests\ProphecyTrait;
2324
use PHPUnit\Framework\TestCase;
2425
use Prophecy\Argument;
26+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
2527
use Symfony\Component\Routing\RouterInterface;
2628

2729
class SendEnvelopeTest extends TestCase
@@ -33,6 +35,7 @@ class SendEnvelopeTest extends TestCase
3335
private $routerProphecyMock;
3436
private $envelopesApiProphecyMock;
3537
private $envelopeSummaryProphecyMock;
38+
private $eventDispatcherProphecyMock;
3639

3740
protected function setUp(): void
3841
{
@@ -41,6 +44,7 @@ protected function setUp(): void
4144
$this->routerProphecyMock = $this->prophesize(RouterInterface::class);
4245
$this->envelopesApiProphecyMock = $this->prophesize(EnvelopesApi::class);
4346
$this->envelopeSummaryProphecyMock = $this->prophesize(EnvelopeSummary::class);
47+
$this->eventDispatcherProphecyMock = $this->prophesize(EventDispatcherInterface::class);
4448
}
4549

4650
public function testItCreatesASendEnvelope(): void
@@ -62,7 +66,9 @@ public function testItCreatesASendEnvelope(): void
6266
$this->envelopeBuilderProphecyMock->setEnvelopesApi(Argument::type(EnvelopesApi::class))->shouldBeCalled();
6367
$this->envelopeBuilderProphecyMock->setEnvelopeId('envelopeId')->shouldBeCalled();
6468

65-
$sendEnvelope = new SendEnvelope($this->envelopeBuilderProphecyMock->reveal(), $this->grantProphecyMock->reveal(), $this->routerProphecyMock->reveal(), 'default');
69+
$this->eventDispatcherProphecyMock->dispatch(Argument::Type(PreSendEnvelopeEvent::class))->shouldBeCalled();
70+
71+
$sendEnvelope = new SendEnvelope($this->envelopeBuilderProphecyMock->reveal(), $this->grantProphecyMock->reveal(), $this->routerProphecyMock->reveal(), $this->eventDispatcherProphecyMock->reveal());
6672
$sendEnvelope(['signature_name' => 'default']);
6773
}
6874
}

0 commit comments

Comments
 (0)