Skip to content

Commit b095b9e

Browse files
committed
Ping webhook
1 parent 4bd93e2 commit b095b9e

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-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\PingWebHookHandler")
9+
*/
10+
final class PingWebHookCommand
11+
{
12+
/**
13+
* @var string
14+
*/
15+
private $pingUrl;
16+
17+
/**
18+
* @param string $pingUrl
19+
*/
20+
public function __construct(string $pingUrl)
21+
{
22+
$this->pingUrl = $pingUrl;
23+
}
24+
25+
/**
26+
* @return string
27+
*/
28+
public function getPingUrl(): string
29+
{
30+
return $this->pingUrl;
31+
}
32+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Client\Github\CommandBus\Handler;
4+
5+
use ApiClients\Client\Github\CommandBus\Command\PingWebHookCommand;
6+
use ApiClients\Foundation\Transport\Service\RequestService;
7+
use Psr\Http\Message\ResponseInterface;
8+
use React\Promise\PromiseInterface;
9+
10+
final class PingWebHookHandler
11+
{
12+
/**
13+
* @var RequestService
14+
*/
15+
private $requestService;
16+
17+
/**
18+
* @param RequestService $requestService
19+
*/
20+
public function __construct(RequestService $requestService)
21+
{
22+
$this->requestService = $requestService;
23+
}
24+
25+
/**
26+
* @param PingWebHookCommand $command
27+
* @return PromiseInterface
28+
*/
29+
public function handle(PingWebHookCommand $command): PromiseInterface
30+
{
31+
return $this->requestService->request($command->getPingUrl())
32+
->then(function (ResponseInterface $response) {
33+
return $response->getStatusCode() == 204;
34+
});
35+
}
36+
}

src/Resource/Async/WebHook.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@
22

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

5+
use ApiClients\Client\Github\CommandBus\Command\PingWebHookCommand;
56
use ApiClients\Client\Github\Resource\WebHook as BaseWebHook;
7+
use React\Promise\PromiseInterface;
68

79
class WebHook extends BaseWebHook
810
{
911
public function refresh(): WebHook
1012
{
1113
throw new \Exception('TODO: create refresh method!');
1214
}
15+
16+
public function ping(): PromiseInterface
17+
{
18+
return $this->handleCommand(
19+
new PingWebHookCommand($this->pingUrl())
20+
);
21+
}
1322
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Tests\Github\CommandBus\Handler;
4+
5+
use ApiClients\Client\Github\CommandBus\Command\SaveCommand;
6+
use ApiClients\Client\Github\CommandBus\Handler\SaveHandler;
7+
use ApiClients\Client\Github\Resource\OrganizationInterface;
8+
use ApiClients\Foundation\Hydrator\Hydrator;
9+
use ApiClients\Foundation\Resource\ResourceInterface;
10+
use ApiClients\Foundation\Transport\Service\RequestService;
11+
use ApiClients\Middleware\Json\JsonStream;
12+
use ApiClients\Tools\TestUtilities\TestCase;
13+
use RingCentral\Psr7\Request;
14+
use RingCentral\Psr7\Response;
15+
use function React\Promise\resolve;
16+
17+
final class PingWebhookHandlerTest extends TestCase
18+
{
19+
public function testCommand()
20+
{
21+
$url = 'url';
22+
$data = [];
23+
24+
$request = new Request(
25+
'PATCH',
26+
$url,
27+
[],
28+
new JsonStream($data)
29+
);
30+
$response = new Response(
31+
200,
32+
[],
33+
new JsonStream($data)
34+
);
35+
36+
$resource = $this->prophesize(ResourceInterface::class)->reveal();
37+
38+
$service = $this->prophesize(RequestService::class);
39+
$service->request($request)->shouldBeCalled()->willReturn(resolve($response));
40+
41+
$hydrator = $this->prophesize(Hydrator::class);
42+
$hydrator->hydrate(OrganizationInterface::HYDRATE_CLASS, $data)->shouldBeCalled()->willReturn($resource);
43+
44+
$handler = new SaveHandler($service->reveal(), $hydrator->reveal());
45+
$handler->handle(new SaveCommand(
46+
OrganizationInterface::HYDRATE_CLASS,
47+
$data,
48+
$url
49+
));
50+
}
51+
}

0 commit comments

Comments
 (0)