8000 Merge pull request #60 from php-api-clients/branch-get-commit · php-api-clients/github@f6a2ada · GitHub
[go: up one dir, main page]

Skip to content

Commit f6a2ada

Browse files
authored
Merge pull request #60 from php-api-clients/branch-get-commit
Get detailed commit from branch
2 parents 888fc34 + 3adba20 commit f6a2ada

File tree

4 files changed

+99
-2
lines changed

4 files changed

+99
-2
lines changed

examples/user-repositories-branches-async.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
return $user->repository($argv[2] ?? 'github');
1818
})->then(function (Repository $repository) {
1919
resource_pretty_print($repository, 1, true);
20-
$repository->branches()->subscribe(function ($labels) {
21-
resource_pretty_print($labels, 2, true);
20+
$repository->branches()->subscribe(function (Repository\Branch $branch) {
21+
resource_pretty_print($branch, 2, true);
22+
$branch->detailedCommit()->then(function (Repository\Commit $commit) {
23+
resource_pretty_print($commit);
24+
})->done(null, 'display_throwable');
2225
}, function ($error) {
2326
echo (string)$error;
2427
});
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\DetailedCommitHandler")
9+
*/
10+
final class DetailedCommitCommand
11+
{
12+
/**
13+
* @var string
14+
*/
15+
private $fullName;
16+
17+
/**
18+
* @var string
19+
*/
20+
private $sha;
21+
22+
/**
23+
* @param string $fullName
24+
* @param string $sha
25+
*/
26+
public function __construct(string $fullName, string $sha)
27+
{
28+
$this->fullName = $fullName;
29+
$this->sha = $sha;
30+
}
31+
32+
/**
33+
* @return string
34+
*/
35+
public function getFullName(): string
36+
{
37+
return $this->fullName;
38+
}
39+
40+
/**
41+
* @return string
42+
*/
43+
public function getSha(): string
44+
{
45+
return $this->sha;
46+
}
47+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Client\Github\CommandBus\Handler\Repository;
4+
5+
use ApiClients\Client\Github\CommandBus\Command\Repository\DetailedCommitCommand;
6+
use ApiClients\Client\Github\Resource\Repository\CommitInterface;
7+
use ApiClients\Tools\Services\< 8000 span class=pl-v>Client\FetchAndHydrateService;
8+
use React\Promise\PromiseInterface;
9+
10+
final class DetailedCommitHandler
11+
{
12+
/**
13+
* @var FetchAndHydrateService
14+
*/
15+
private $fetchAndHydrateService;
16+
17+
/**
18+
* @param FetchAndHydrateService $fetchAndHydrateService
19+
*/
20+
public function __construct(FetchAndHydrateService $fetchAndHydrateService)
21+
{
22+
$this->fetchAndHydrateService = $fetchAndHydrateService;
23+
}
24+
25+
/**
26+
* @param DetailedCommitCommand $command
27+
* @return PromiseInterface
28+
*/
29+
public function handle(DetailedCommitCommand $command): PromiseInterface
30+
{
31+
return $this->fetchAndHydrateService->fetch(
32+
'repos/' . $command->getFullName() . '/commits/' . $command->getSha(),
33+
'',
34+
CommitInterface::HYDRATE_CLASS
35+
);
36+
}
37+
}

src/Resource/Async/Repository/Branch.php

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

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

5+
use ApiClients\Client\Github\CommandBus\Command\Repository\DetailedCommitCommand;
56
use ApiClients\Client\Github\Resource\Repository\Branch as BaseBranch;
7+
use React\Promise\PromiseInterface;
68

79
class Branch extends BaseBranch
810
{
911
public function refresh(): Branch
1012
{
1113
throw new \Exception('TODO: create refresh method!');
1214
}
15+
16+
public function detailedCommit(): PromiseInterface
17+
{
18+
return $this->handleCommand(new DetailedCommitCommand(
19+
\str_replace('/branches/' . $this->name . '/protection', '', \explode('/repos/', $this->protection_url)[1]),
20+
$this->commit->sha()
21+
));
22+
}
1323
}

0 commit comments

Comments
 (0)
0