|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace ApiClients\Client\Github\CommandBus\Handler\Repository; |
| 4 | + |
| 5 | +use ApiClients\Client\Github\CommandBus\Command\Repository\BranchesCommand; |
| 6 | +use ApiClients\Client\Github\CommandBus\Command\Repository\MilestonesCommand; |
| 7 | +use ApiClients\Client\Github\CommandBus\Command\Repository\PullRequestsCommand; |
| 8 | +use ApiClients\Client\Github\Resource\Repository\BranchInterface; |
| 9 | +use ApiClients\Client\Github\Resource\Repository\MilestoneInterface; |
| 10 | +use ApiClients\Client\Github\Resource\Repository\PullRequestInterface; |
| 11 | +use ApiClients\Client\Github\Service\IteratePagesService; |
| 12 | +use ApiClients\Foundation\Hydrator\Hydrator; |
| 13 | +use function ApiClients\Tools\Rx\observableFromArray; |
| 14 | +use React\Promise\PromiseInterface; |
| 15 | +use function React\Promise\resolve; |
| 16 | + |
| 17 | +final class PullRequestsHandler |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var IteratePagesService |
| 21 | + */ |
| 22 | + private $iteratePagesService; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var Hydrator |
| 26 | + */ |
| 27 | + private $hydrator; |
| 28 | + |
| 29 | + /** |
| 30 | + * @param IteratePagesService $iteratePagesService |
| 31 | + * @param Hydrator $hydrator |
| 32 | + */ |
| 33 | + public function __construct(IteratePagesService $iteratePagesService, Hydrator $hydrator) |
| 34 | + { |
| 35 | + $this->iteratePagesService = $iteratePagesService; |
| 36 | + $this->hydrator = $hydrator; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @param PullRequestsCommand $command |
| 41 | + * @return PromiseInterface |
| 42 | + */ |
| 43 | + public function handle(PullRequestsCommand $command): PromiseInterface |
| 44 | + { |
| 45 | + return resolve( |
| 46 | + $this->iteratePagesService->iterate('repos/' . $command->getFullName() . '/pulls') |
| 47 | + ->flatMap(function ($milestones) { |
| 48 | + return observableFromArray($milestones); |
| 49 | + })->map(function ($milestone) { |
| 50 | + return $this->hydrator->hydrate(PullRequestInterface::HYDRATE_CLASS, $milestone); |
| 51 | + }) |
| 52 | + ); |
| 53 | + } |
| 54 | +} |
0 commit comments