Skip to content

Commit dc3ccfb

Browse files
committed
Org add webhook
1 parent 112c86b commit dc3ccfb

File tree

4 files changed

+213
-2
lines changed

4 files changed

+213
-2
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Client\Github\CommandBus\Command\Organization;
4+
5+
use WyriHaximus\Tactician\CommandHandler\Annotations\Handler;
6+
7+
/**
8+
* @Handler("ApiClients\Client\Github\CommandBus\Handler\Organization\AddWebHookHandler")
9+
*/
10+
final class AddWebHookCommand
11+
{
12+
/**
13+
* @var string
14+
*/
15+
private $organization;
16+
17+
/**
18+
* @var string
19+
*/
20+
private $name;
21+
22+
/**
23+
* @var array
24+
*/
25+
private $config;
26+
27+
/**
28+
* @var array
29+
*/
30+
private $events;
31+
32+
/**
33+
* @var bool
34+
*/
35+
private $active;
36+
37+
/**
38+
* @param string $repository
39+
* @param string $name
40+
* @param array $config
41+
* @param array $events
42+
* @param bool $active
43+
*/
44+
public function __construct(string $repository, string $name, array $config, array $events, bool $active)
45+
{
46+
$this->organization = $repository;
47+
$this->name = $name;
48+
$this->config = $config;
49+
$this->events = $events;
50+
$this->active = $active;
51+
}
52+
53+
/**
54+
* @return string
55+
*/
56+
public function getOrganization(): string
57+
{
58+
return $this->organization;
59+
}
60+
61+
/**
62+
* @return string
63+
*/
64+
public function getName(): string
65+
{
66+
return $this->name;
67+
}
68+
69+
/**
70+
* @return array
71+
*/
72+
public function getConfig(): array
73+
{
74+
return $this->config;
75+
}
76+
77+
/**
78+
* @return array
79+
*/
80+
public function getEvents(): array
81+
{
82+
return $this->events;
83+
}
84+
85+
/**
86+
* @return bool
87+
*/
88+
public function isActive(): bool
89+
{
90+
return $this->active;
91+
}
92+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Client\Github\CommandBus\Handler\Organization;
4+
5+
use ApiClients\Client\Github\CommandBus\Command\Organization\AddWebHookCommand;
6+
use ApiClients\Client\Github\Resource\WebHookInterface;
7+
use ApiClients\Foundation\Hydrator\Hydrator;
8+
use ApiClients\Foundation\Transport\Service\RequestService;
9+
use ApiClients\Middleware\Json\JsonStream;
10+
use React\Promise\PromiseInterface;
11+
use RingCentral\Psr7\Request;
12+
13+
final class AddWebHookHandler
14+
{
15+
/**
16+
* @var RequestService
17+
*/
18+
private $requestService;
19+
20+
/**
21+
* @var Hydrator
22+
*/
23+
private $hydrator;
24+
25+
/**
26+
* @param RequestService $requestService
27+
* @param Hydrator $hydrator
28+
*/
29+
public function __construct(RequestService $requestService, Hydrator $hydrator)
30+
{
31+
$this->requestService = $requestService;
32+
$this->hydrator = $hydrator;
33+
}
34+
35+
/**
36+
* @param AddWebHookCommand $command
37+
* @return PromiseInterface
38+
*/
39+
public function handle(AddWebHookCommand $command): PromiseInterface
40+
{
41+
return $this->requestService->request(
42+
new Request(
43+
'POST',
44+
'orgs/' . $command->getOrganization() . '/hooks',
45+
[],
46+
new JsonStream([
47+
'name' => $command->getName(),
48+
'config' => $command->getConfig(),
49+
'events' => $command->getEvents(),
50+
'active' => $command->isActive(),
51+
])
52+
)
53+
)->then(function ($hook) {
54+
return $this->hydrator->hydrate(WebHookInterface::HYDRATE_CLASS, $hook->getBody()->getParsedContents());
55+
});
56+
}
57+
}

src/CommandBus/Handler/Repository/AddWebHookHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ public function handle(AddWebHookCommand $command): PromiseInterface
5050
'active' => $command->isActive(),
5151
])
5252
)
53-
)->then(function ($label) {
54-
return $this->hydrator->hydrate(WebHookInterface::HYDRATE_CLASS, $label->getBody()->getParsedContents());
53+
)->then(function ($hook) {
54+
return $this->hydrator->hydrate(WebHookInterface::HYDRATE_CLASS, $hook->getBody()->getParsedContents());
5555
});
5656
}
5757
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Tests\Github\CommandBus\Handler\Organization;
4+
5+
use ApiClients\Client\Github\CommandBus\Command\Organization\AddWebHookCommand;
6+
use ApiClients\Client\Github\CommandBus\Handler\Organization\AddWebHookHandler;
7+
use ApiClients\Client\Github\Resource\WebHookInterface;
8+
use ApiClients\Foundation\Hydrator\Hydrator;
9+
use ApiClients\Foundation\Transport\Service\RequestService;
10+
use ApiClients\Middleware\Json\JsonStream;
11+
use ApiClients\Tools\TestUtilities\TestCase;
12+
use RingCentral\Psr7\Request;
13+
use RingCentral\Psr7\Response;
14+
use function React\Promise\resolve;
15+
16+
final class AddWebHookHandlerTest extends TestCase
17+
{
18+
public function testCommand()
19+
{
20+
$resource = $this->prophesize(WebHookInterface::class)->reveal();
21+
$org = 'repository';
22+
$name = 'repository';
23+
$config = [
24+
'url' => 'https://www.example.com/',
25+
];
26+
$events = ['all'];
27+
$active = true;
28+
$webhook = [];
29+
30+
$request = new Request(
31+
'POST',
32+
'orgs/' . $org . '/hooks',
33+
[],
34+
new JsonStream([
35+
'name' => $name,
36+
'config' => $config,
37+
'events' => $events,
38+
'active' => $active,
39+
])
40+
);
41+
$response = new Response(
42+
200,
43+
[],
44+
new JsonStream([])
45+
);
46+
47+
$service = $this->prophesize(RequestService::class);
48+
$service->request($request)->shouldBeCalled()->willReturn(resolve($response));
49+
50+
$hydrator = $this->prophesize(Hydrator::class);
51+
$hydrator->hydrate(WebHookInterface::HYDRATE_CLASS, $webhook)->shouldBeCalled()->willReturn($resource);
52+
53+
$handler = new AddWebHookHandler($service->reveal(), $hydrator->reveal());
54+
$handler->handle(new AddWebHookCommand(
55+
$org,
56+
$name,
57+
$config,
58+
$events,
59+
$active
60+
));
61+
}
62+
}

0 commit comments

Comments
 (0)