1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Client\Github\CommandBus\Handler\Repository; |
4
|
|
|
|
5
|
|
|
use ApiClients\Client\Github\CommandBus\Command\Repository\LabelsCommand; |
6
|
|
|
use ApiClients\Client\Github\Resource\LabelInterface; |
7
|
|
|
use ApiClients\Client\Github\Service\IteratePagesService; |
8
|
|
|
use ApiClients\Foundation\Hydrator\Hydrator; |
9
|
|
|
use React\Promise\PromiseInterface; |
10
|
|
|
use Rx\Observable; |
11
|
|
|
use function ApiClients\Tools\Rx\unwrapObservableFromPromise; |
12
|
|
|
use function React\Promise\resolve; |
13
|
|
|
use function WyriHaximus\React\futureFunctionPromise; |
14
|
|
|
|
15
|
|
View Code Duplication |
final class LabelsHandler |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var IteratePagesService |
19
|
|
|
*/ |
20
|
|
|
private $iteratePagesService; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Hydrator |
24
|
|
|
*/ |
25
|
|
|
private $hydrator; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param IteratePagesService $iteratePagesService |
29
|
|
|
* @param Hydrator $hydrator |
30
|
|
|
*/ |
31
|
|
|
public function __construct(IteratePagesService $iteratePagesService, Hydrator $hydrator) |
32
|
|
|
{ |
33
|
|
|
$this->iteratePagesService = $iteratePagesService; |
34
|
|
|
$this->hydrator = $hydrator; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param LabelsCommand $command |
39
|
|
|
* @return PromiseInterface |
40
|
|
|
*/ |
41
|
|
|
public function handle(LabelsCommand $command): PromiseInterface |
42
|
|
|
{ |
43
|
|
|
return resolve(unwrapObservableFromPromise( |
44
|
|
|
$this->iteratePagesService->handle('repos/' . $command->getFullName() . '/labels') |
45
|
|
|
)->flatMap(function ($labels) { |
46
|
|
|
return Observable::fromArray($labels); |
47
|
|
|
})->map(function ($label) { |
48
|
|
|
return $this->hydrator->hydrate(LabelInterface::HYDRATE_CLASS, $label); |
49
|
|
|
})); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.