Skip to content

Commit 4bf6ffb

Browse files
committed
Delete webhook
1 parent 1701264 commit 4bf6ffb

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Client\Github\CommandBus\Command;
4+
5+
use WyriHaximus\Tactician\CommandHandler\Annotations\Handler;
6+
7+
/**
8+
* @Handler("ApiClients\Client\Github\CommandBus\Handler\DeleteHandler")
9+
*/
10+
final class DeleteCommand
11+
{
12+
/**
13+
* @var string
14+
*/
15+
private $url;
16+
17+
/**
18+
* @param string $url
19+
*/
20+
public function __construct(string $url)
21+
{
22+
$this->url = $url;
23+
}
24+
25+
/**
26+
* @return string
27+
*/
28+
public function getUrl(): string
29+
{
30+
return $this->url;
31+
}
32+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Client\Github\CommandBus\Handler;
4+
5+
use ApiClients\Client\Github\CommandBus\Command\DeleteCommand;
6+
use ApiClients\Foundation\Transport\Service\RequestService;
7+
use Psr\Http\Message\ResponseInterface;
8+
use React\Promise\PromiseInterface;
9+
use RingCentral\Psr7\Request;
10+
11+
final class DeleteHandler
12+
{
13+
/**
14+
* @var RequestService
15+
*/
16+
private $requestService;
17+
18+
/**
19+
* @param RequestService $requestService
20+
*/
21+
public function __construct(RequestService $requestService)
22+
{
23+
$this->requestService = $requestService;
24+
}
25+
26+
/**
27+
* @param DeleteCommand $command
28+
* @return PromiseInterface
29+
*/
30+
public function handle(DeleteCommand $command): PromiseInterface
31+
{
32+
return $this->requestService->request(
33+
new Request(
34+
'DELETE',
35+
$command->getUrl()
36+
)
37+
)->then(function (ResponseInterface $response) {
38+
return $response->getStatusCode() == 204;
39+
});
40+
}
41+
}

src/Resource/Async/WebHook.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ApiClients\Client\Github\Resource\Async;
44

5+
use ApiClients\Client\Github\CommandBus\Command\DeleteCommand;
56
use ApiClients\Client\Github\CommandBus\Command\WebHook\PingCommand;
67
use ApiClients\Client\Github\Resource\WebHook as BaseWebHook;
78
use React\Promise\PromiseInterface;
@@ -19,4 +20,11 @@ public function ping(): PromiseInterface
1920
new PingCommand($this->pingUrl())
2021
);
2122
}
23+
24+
public function delete(): PromiseInterface
25+
{
26+
return $this->handleCommand(
27+
new DeleteCommand($this->url())
28+
);
29+
}
2230
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Tests\Github\CommandBus\Handler\WebHook;
4+
5+
use ApiClients\Client\Github\CommandBus\Command\DeleteCommand;
6+
use ApiClients\Client\Github\CommandBus\Handler\DeleteHandler;
7+
use ApiClients\Foundation\Transport\Service\RequestService;
8+
use ApiClients\Tools\TestUtilities\TestCase;
9+
use RingCentral\Psr7\Request;
10+
use RingCentral\Psr7\Response;
11+
use function React\Promise\resolve;
12+
13+
final class DeleteHandlerTest extends TestCase
14+
{
15+
public function testCommand()
16+
{
17+
$url = 'url';
18+
19+
$request = new Request('DELETE', $url);
20+
$response = new Response(204);
21+
22+
$service = $this->prophesize(RequestService::class);
23+
$service->request($request)->shouldBeCalled()->willReturn(resolve($response));
24+
25+
$handler = new DeleteHandler($service->reveal());
26+
self::assertTrue($this->await($handler->handle(new DeleteCommand(
27+
$url
28+
))));
29+
}
30+
31+
public function testCommandError()
32+
{
33+
$url = 'url';
34+
35+
$request = new Request('DELETE', $url);
36+
$response = new Response(500);
37+
38+
$service = $this->prophesize(RequestService::class);
39+
$service->request($request)->shouldBeCalled()->willReturn(resolve($response));
40+
41+
$handler = new DeleteHandler($service->reveal());
42+
self::assertFalse($this->await($handler->handle(new DeleteCommand(
43+
$url
44+
))));
45+
}
46+
}

0 commit comments

Comments
 (0)