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 ( 7250ad...9b5a64 )
by Cees-Jan
14s
created

FileUploadHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 8
dl 0
loc 68
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B handle() 0 34 3
1
<?php declare(strict_types=1);
2
3
namespace ApiClients\Client\Github\CommandBus\Handler\Repository\Contents;
4
5
use ApiClients\Client\Github\CommandBus\Command\Repository\Contents\FileUploadCommand;
6
use ApiClients\Client\Github\Resource\Contents\FileOperationInterface;
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\WritableStreamBase64Encode;
15
use WyriHaximus\React\Stream\Json\JsonStream;
16
17
final class FileUploadHandler
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 3
    public function __construct(RequestService $requestService, Hydrator $hydrator, LoopInterface $loop)
40
    {
41 3
        $this->requestService = $requestService;
42 3
        $this->hydrator = $hydrator;
43 3
        $this->loop = $loop;
44 3
    }
45
46
    /**
47
     * @param  FileUploadCommand $command
48
     * @return PromiseInterface
49
     */
50 3
    public function handle(FileUploadCommand $command): PromiseInterface
51
    {
52 3
        $stream = new JsonStream();
53
54
        $this->loop->futureTick(function () use ($stream, $command) {
55 3
            $fileStream = new ThroughStream();
56 3
            $decoratedFileStream = new WritableStreamBase64Encode($fileStream);
57 3
            $stream->write('message', $command->getCommitMessage());
58 3
            $stream->write('content', $fileStream);
59 3
            if ($command->getSha() !== '') {
60 2
                $stream->write('sha', $command->getSha());
61
            }
62 3
            if ($command->getBranch() !== '') {
63 2
                $stream->write('branch', $command->getBranch());
64
            }
65
66 3
            $stream->end();
67
68
            $this->loop->futureTick(function () use ($decoratedFileStream, $command) {
69 3
                $command->getStream()->pipe($decoratedFileStream);
70 3
            });
71 3
        });
72
73 3
        return $this->requestService->request(
74 3
            new Request(
75 3
                'PUT',
76 3
                $command->getUrl(),
77 3
                [],
78 3
                new ReadableBodyStream($stream)
79
            )
80 3
        )->then(function ($operation) {
81 3
            return $this->hydrator->hydrate(FileOperationInterface::HYDRATE_CLASS, $operation->getBody()->getJson());
82 3
        });
83
    }
84
}
85