Completed
Push — master ( 4993cd...b5e1f6 )
by Pieter
05:14
created

Artax::getBodyString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
crap 2
1
<?php declare(strict_types=1);
2
3
namespace PeeHaa\AsyncTwitter\Http;
4
5
use Amp\Artax\Client;
6
use Amp\Promise;
7
use PeeHaa\AsyncTwitter\Request\Body;
8
use PeeHaa\AsyncTwitter\Request\Parameter;
9
use PeeHaa\AsyncTwitter\Request\Url;
10
use PeeHaa\AsyncTwitter\Oauth\Header;
11
use Amp\Artax\Request;
12
13
class Artax
14
{
15
    private $client;
16
17 2
    public function __construct(Client $client)
18
    {
19 2
        $this->client = $client;
20 2
    }
21
22 1 View Code Duplication
    public function post(Url $url, Header $header, Body $body): Promise
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
23
    {
24 1
        $request = (new Request)
25 1
            ->setMethod('POST')
26 1
            ->setUri($url->getUrl())
27 1
            ->setAllHeaders([
28 1
                'Authorization' => $header->getHeader(),
29 1
                'Content-Type'  => 'application/x-www-form-urlencoded',
30
            ])
31 1
            ->setBody($this->getBodyString($body))
32
        ;
33
34 1
        return $this->client->request($request);
35
    }
36
37 1
    private function getBodyString(Body $body): string
38
    {
39 1
        $bodyString = '';
40 1
        $delimiter  = '';
41
42 1
        foreach ($body->getParameters() as $parameter) {
43 1
            $bodyString .= $delimiter . $parameter->getKey() . '=' . rawurlencode($parameter->getValue());
44
45 1
            $delimiter = '&';
46
        }
47
48 1
        return $bodyString;
49
    }
50
51 1 View Code Duplication
    public function get(Url $url, Header $header, Parameter ...$parameters): Promise
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
52
    {
53 1
        $request = (new Request)
54 1
            ->setMethod('GET')
55 1
            ->setUri($url->getUrl() . $this->buildQueryString(...$parameters))
56 1
            ->setAllHeaders(['Authorization' => $header->getHeader()])
57
        ;
58
59 1
        return $this->client->request($request);
60
    }
61
62 1
    private function buildQueryString(Parameter ...$parameters): string
63
    {
64 1
        $queryString = [];
65
66 1
        foreach ($parameters as $parameter) {
67 1
            $queryString[$parameter->getKey()] = $parameter->getValue();
68
        }
69
70 1
        return '?' . http_build_query($queryString);
71
    }
72
}
73