Skip to content

Commit c3caf27

Browse files
committed
Repository::updateSettings
1 parent 1e903a3 commit c3caf27

File tree

4 files changed

+165
-0
lines changed

4 files changed

+165
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Client\Github\CommandBus\Command\Repository;
4+
5+
use WyriHaximus\Tactician\CommandHandler\Annotations\Handler;
6+
7+
/**
8+
* @Handler("ApiClients\Client\Github\CommandBus\Handler\Repository\UpdateSettingsHandler")
9+
*/
10+
final class UpdateSettingsCommand
11+
{
12+
/**
13+
* @var string
14+
*/
15+
private $repository;
16+
17+
/**
18+
* @var array
19+
*/
20+
private $settings = [];
21+
22+
/**
23+
* @param string $repository
24+
* @param array $settings
25+
*/
26+
public function __construct(string $repository, array $settings)
27+
{
28+
$this->repository = $repository;
29+
$this->settings = $settings;
30+
}
31+
32+
/**
33+
* @return string
34+
*/
35+
public function getRepository(): string
36+
{
37+
return $this->repository;
38+
}
39+
40+
/**
41+
* @return array
42+
*/
43+
public function getSettings(): array
44+
{
45+
return $this->settings;
46+
}
47+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Client\Github\CommandBus\Handler\Repository;
4+
5+
use ApiClients\Client\Github\CommandBus\Command\Repository\UpdateSettingsCommand;
6+
use ApiClients\Client\Github\Resource\RepositoryInterface;
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 UpdateSettingsHandler
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 UpdateSettingsCommand $command
37+
* @return PromiseInterface
38+
*/
39+
public function handle(UpdateSettingsCommand $command): PromiseInterface
40+
{
41+
return $this->requestService->request(
42+
new Request(
43+
'PATCH',
44+
'repos/' . $command->getRepository(),
45+
[],
46+
new JsonStream($command->getSettings())
47+
)
48+
)->then(function ($repository) {
49+
return $this->hydrator->hydrate(
50+
RepositoryInterface::HYDRATE_CLASS,
51+
$repository->getBody()->getParsedContents()
52+
);
53+
});
54+
}
55+
}

src/Resource/Async/Repository.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use ApiClients\Client\Github\CommandBus\Command\Repository\TagsCommand;
2020
use ApiClients\Client\Github\CommandBus\Command\Repository\TravisCommand;
2121
use ApiClients\Client\Github\CommandBus\Command\Repository\UnSubscribeCommand;
22+
use ApiClients\Client\Github\CommandBus\Command\Repository\UpdateSettingsCommand;
2223
use ApiClients\Client\Github\Resource\Repository as BaseRepository;
2324
use React\Promise\PromiseInterface;
2425
use React\Stream\ReadableStreamInterface;
@@ -155,4 +156,13 @@ public function scrutinizerRepository(): PromiseInterface
155156
{
156157
return $this->handleCommand(new ScrutinizerCommand(...explode('/', $this->fullName())));
157158
}
159+
160+
public function updateSettings(array $settings): PromiseInterface
161+
{
162+
if (!isset($settings['name'])) {
163+
$settings['name'] = $this->name();
164+
}
165+
166+
return $this->handleCommand(new UpdateSettingsCommand($this->fullName(), $settings));
167+
}
158168
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Tests\Github\CommandBus\Handler\Repository;
4+
5+
use ApiClients\Client\Github\CommandBus\Command\Repository\UpdateSettingsCommand;
6+
use ApiClients\Client\Github\CommandBus\Handler\Repository\UpdateSettingsHandler;
7+
use ApiClients\Client\Github\Resource\LabelInterface;
8+
use ApiClients\Client\Github\Resource\RepositoryInterface;
9+
use ApiClients\Foundation\Hydrator\Hydrator;
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 UpdateSettingsHandlerTest extends TestCase
18+
{
19+
public function testCommand()
20+
{
21+
$resource = $this->prophesize(LabelInterface::class)->reveal();
22+
$repository = 'repository';
23+
$name = 'repository';
24+
$settings = [
25+
'name' => $name,
26+
'homepage' => 'https://www.example.com/',
27+
];
28+
29+
$request = new Request(
30+
'PATCH',
31+
'repos/' . $repository,
32+
[],
33+
new JsonStream($settings)
34+
);
35+
$response = new Response(
36+
200,
37+
[],
38+
new JsonStream($settings)
39+
);
40+
41+
$service = $this->prophesize(RequestService::class);
42+
$service->request($request)->shouldBeCalled()->willReturn(resolve($response));
43+
44+
$hydrator = $this->prophesize(Hydrator::class);
45+
$hydrator->hydrate(RepositoryInterface::HYDRATE_CLASS, $settings)->shouldBeCalled()->willReturn($resource);
46+
47+
$handler = new UpdateSettingsHandler($service->reveal(), $hydrator->reveal());
48+
$handler->handle(new UpdateSettingsCommand(
49+
$repository,
50+
$settings
51+
));
52+
}
53+
}

0 commit comments

Comments
 (0)