GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( b96dc4...ba3ef4 )
by Cees-Jan
07:47
created

BlobHandler::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php declare(strict_types=1);
2
3
namespace ApiClients\Client\Github\CommandBus\Handler\Repository;
4
5
use ApiClients\Client\Github\CommandBus\Command\Repository\BlobCommand;
6
use ApiClients\Client\Github\Resource\TreeInterface;
7
use ApiClients\Foundation\Hydrator\Hydrator;
8
use ApiClients\Foundation\Transport\Service\RequestService;
9
use Clue\React\Buzz\Message\ReadableBodyStream;
10
use React\EventLoop\LoopInterface;
11
use React\Promise\PromiseInterface;
12
use React\Stream\ThroughStream;
13
use RingCentral\Psr7\Request;
14
use WyriHaximus\React\Stream\Base64\ReadableStreamBase64Encode;
15
use WyriHaximus\React\Stream\Json\JsonStream;
16
17
final class BlobHandler
18
{
19
    /**
20
     * @var RequestService
21
     */
22
    private $requestService;
23
24
    /**
25
     * @var Hydrator
26
     */
27
    private $hydrator;
28
29
    /**
30
     * @var LoopInterface
31
     */
32
    private $loop;
33
34
    /**
35
     * @param RequestService $requestService
36
     * @param Hydrator       $hydrator
37
     * @param LoopInterface  $loop
38
     */
39
    public function __construct(RequestService $requestService, Hydrator $hydrator, LoopInterface $loop)
40
    {
41
        $this->requestService = $requestService;
42
        $this->hydrator = $hydrator;
43
        $this->loop = $loop;
44
    }
45
46
    /**
47
     * @param  BlobCommand      $command
48
     * @return PromiseInterface
49
     */
50
    public function handle(BlobCommand $command): PromiseInterface
51
    {
52
        $stream = new JsonStream();
53
        $this->loop->futureTick(function () use ($stream, $command) {
54
            $contentsStream = new ThroughStream();
55
            $stream->write('encoding', 'base64');
56
            $stream->write('content', new ReadableStreamBase64Encode($contentsStream));
57
            $stream->end();
58
59
            $this->loop->futureTick(function () use ($contentsStream, $command) {
60
                $command->getContents()->pipe($contentsStream);
61
            });
62
        });
63
64
        return $this->requestService->request(
65
            new Request(
66
                'POST',
67
                'repos/' . $command->getRepository() . '/git/blobs',
68
                [],
69
                new ReadableBodyStream($stream)
70
            )
71
        )->then(function ($tree) {
72
            return $this->hydrator->hydrate(TreeInterface::HYDRATE_CLASS, $tree->getBody()->getParsedContents());
73
        });
74
    }
75
}
76