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 ( 3d3029...b5f30e )
by Robert
02:38
created

Http::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
7
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
8
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
9
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
10
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
11
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
12
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
13
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
14
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
16
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17
 *
18
 * This software consists of voluntary contributions made by many individuals
19
 * and is licensed under the MIT license. For more information, see
20
 * <https://github.com/digitalkaoz/php-ipfs>
21
 */
22
23
namespace IPFS\Driver;
24
25
use Http\Client\HttpAsyncClient;
26
use Http\Message\MessageFactory;
27
use Http\Message\MultipartStream\MultipartStreamBuilder;
28
use Http\Message\UriFactory;
29
use IPFS\Command\Command;
30
use IPFS\Utils\AnnotationReader;
31
use Psr\Http\Message\RequestInterface;
32
use Psr\Http\Message\ResponseInterface;
33
use Psr\Http\Message\UriInterface;
34
35
class Http implements Driver
36
{
37
    /**
38
     * @var HttpAsyncClient
39
     */
40
    private $client;
41
    /**
42
     * @var MessageFactory
43
     */
44
    private $messageFactory;
45
    /**
46
     * @var UriFactory
47
     */
48
    private $uriFactory;
49
    /**
50
     * @var string
51
     */
52
    private $baseUri;
53
    /**
54
     * @var AnnotationReader
55
     */
56
    private $reader;
57
58 3
    public function __construct(HttpAsyncClient $client, MessageFactory $messageFactory, UriFactory $uriFactory, AnnotationReader $reader, $baseUri = 'http://localhost:5001/api/v0')
59
    {
60 3
        $this->client = $client;
61 3
        $this->messageFactory = $messageFactory;
62 3
        $this->uriFactory = $uriFactory;
63 3
        $this->baseUri = $baseUri;
64 3
        $this->reader = $reader;
65 3
    }
66
67 2
    public function execute(Command $command)
68
    {
69 2
        $request = $this->buildRequest($command, $this->getConfig($command));
70
71 2
        return $this->client->sendAsyncRequest($request)->then(function (ResponseInterface $response) {
72
            return (string) $response->getBody()->getContents();
73 2
        })->wait();
74
    }
75
76 2
    private function buildRequest(Command $command, $config): RequestInterface
77
    {
78 2
        $body = $this->buildBody($command);
79
80 2
        return $this->messageFactory->createRequest(
81 2
            $config['method'],
82 2
            $this->buildUri($command, $config),
83 2
            $this->buildHeaders($body),
84 2
            $body->build()
85
        );
86
    }
87
88 2
    private function buildUri(Command $command, array $config): UriInterface
89
    {
90 2
        $uri = $this->uriFactory->createUri($this->baseUri . $config['path']);
91 2
        $vars = [];
92
93 2
        foreach ($command->getArguments() as $name => $value) {
94 2
            if (is_string($value) && is_readable($value)) {
95 1
                continue;
96
            }
97
98 2
            $vars[$name] = $value;
99
        }
100
101
        //fix arg1= arg2= to arg= weird but seems to be correct (doubled query variable names)
102 2
        $query = preg_replace('/(\d+)=/', '=', http_build_query($vars));
103
104 2
        return $uri->withQuery($query);
105
    }
106
107 2
    private function buildBody(Command $command): MultipartStreamBuilder
108
    {
109 2
        $builder = new MultipartStreamBuilder();
110
111 2
        foreach ($command->getArguments() as $name => $value) {
112 2
            if (is_string($value) && is_readable($value)) {
113 1
                $builder->addResource($name, fopen($command->getArguments()[$name], 'rb'), [
114 2
                    'filename' => $value,
115
                    'headers'  => ['Content-Type' => 'application/octet-stream'],
116
                ]);
117
            }
118
        }
119
120 2
        return $builder;
121
    }
122
123 2
    private function buildHeaders(MultipartStreamBuilder $body): array
124
    {
125 2
        $emptyBody = '--' . $body->getBoundary() . "--\r\n";
126
127
        return [
128 2
            'User-Agent'   => 'php-ipfs',
129 2
            'Content-Type' => $body->build()->getContents() !== $emptyBody ? 'multipart/form-data; boundary=' . $body->getBoundary() : null,
130
        ];
131
    }
132
133 2
    private function getConfig(Command $command): array
134
    {
135
        $config = [
136 2
            'path'   => '/' . str_replace(':', '/', $this->reader->getName($command->getMethod())),
137 2
            'method' => 'GET',
138
        ];
139
140 2
        foreach ($command->getArguments() as $arg) {
141 2
            if (is_string($arg) && is_readable($arg)) {
142 1
                $config['method'] = 'POST';
143 2
                break;
144
            }
145
        }
146
147 2
        return $config;
148
    }
149
}
150