Completed
Push — master ( 784303...e88874 )
by Pieter
03:18
created

Artax::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 4
crap 2
1
<?php declare(strict_types=1);
2
3
namespace PeeHaa\AsyncTwitter\Http;
4
5
use Amp\Artax\Client as ArtaxClient;
6
use Amp\Artax\FormBody;
7
use Amp\Promise;
8
use PeeHaa\AsyncTwitter\Exception;
9
use PeeHaa\AsyncTwitter\Request\Body;
10
use PeeHaa\AsyncTwitter\Request\FieldParameter;
11
use PeeHaa\AsyncTwitter\Request\FileParameter;
12
use PeeHaa\AsyncTwitter\Request\Parameter;
13
use PeeHaa\AsyncTwitter\Request\Url;
14
use PeeHaa\AsyncTwitter\Oauth\Header;
15
use Amp\Artax\Request;
16
17
class Artax implements Client
18
{
19
    private $client;
20
21 4
    public function __construct(ArtaxClient $client)
22
    {
23 4
        $this->client = $client;
24
    }
25
26 2
    public function post(Url $url, Header $header, Body $body, int $flags = 0): Promise
27
    {
28 2
        $body = $this->getFormBody($body);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $body. This often makes code more readable.
Loading history...
29
30 2
        $request = (new Request)
31 2
            ->setMethod('POST')
32 2
            ->setUri($url->getUrl())
33 2
            ->setHeader('Authorization', $header->getHeader())
34 2
            ->setBody($body)
35
        ;
36
37 2
        $options = [];
38 2
        if ($flags & Client::OP_STREAM) {
39 1
            $options[ArtaxClient::OP_MS_TRANSFER_TIMEOUT] = -1;
40
        }
41
42 2
        return $this->client->request($request, $options);
43
    }
44
45 2
    private function getFormBody(Body $body): FormBody
46
    {
47 2
        $formBody = new FormBody();
48
49 2
        foreach ($body->getParameters() as $parameter) {
50 2
            if ($parameter instanceof FieldParameter) {
51 2
                $formBody->addField($parameter->getKey(), $parameter->getValue(), $parameter->getType());
52
            } else if ($parameter instanceof FileParameter) {
53
                $formBody->addFile($parameter->getKey(), $parameter->getPath(), $parameter->getType());
54
            } else {
55 2
                throw new Exception("Unexpected parameter type: " . get_class($parameter));
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Unexpected parameter type: does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
56
            }
57
        }
58
59 2
        return $formBody;
60
    }
61
62 2
    public function get(Url $url, Header $header, array $parameters, int $flags = 0): Promise
63
    {
64 2
        $request = (new Request)
65 2
            ->setMethod('GET')
66 2
            ->setUri($url->getUrl() . $this->buildQueryString(...$parameters))
67 2
            ->setAllHeaders(['Authorization' => $header->getHeader()])
68
        ;
69
70 2
        $options = [];
71 2
        if ($flags & Client::OP_STREAM) {
72 1
            $options[ArtaxClient::OP_MS_TRANSFER_TIMEOUT] = -1;
73
        }
74
75 2
        return $this->client->request($request, $options);
76
    }
77
78 2
    private function buildQueryString(Parameter ...$parameters): string
79
    {
80 2
        $queryString = [];
81
82 2
        foreach ($parameters as $parameter) {
83 2
            if (!$parameter instanceof FieldParameter) {
84
                throw new Exception("Unexpected parameter type: " . get_class($parameter));
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Unexpected parameter type: does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
85
            }
86
87 2
            $queryString[$parameter->getKey()] = $parameter->getValue();
88
        }
89
90 2
        return '?' . http_build_query($queryString);
91
    }
92
}
93