Passed
Push — master ( ea3f7c...e350eb )
by nicolas
50s
created

CreateResourceQuery::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 7
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 4
crap 2
1
<?php
2
3
namespace Dekalee\Cdn77\Query;
4
5
use Dekalee\Cdn77\Exception\QueryErrorException;
6
use GuzzleHttp\Client;
7
8
/**
9
 * Class CreateResourceQuery
10
 */
11
class CreateResourceQuery implements QueryInterface
12
{
13
    protected $url;
14
    protected $login;
15
    protected $client;
16
    protected $password;
17
18
    const URL = 'https://api.cdn77.com/v2.0/cdn-resource/create';
19
20
    /**
21
     * @param string      $login
22
     * @param string      $password
23
     * @param string      $url
24
     * @param Client|null $client
25
     */
26 4 View Code Duplication
    public function __construct($login, $password, $url = self::URL, Client $client = null)
0 ignored issues
show
Duplication introduced by Nicolas Thal
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...
27
    {
28 4
        $this->url = $url;
29 4
        $this->login = $login;
30 4
        $this->client = $client?: new Client();
31 4
        $this->password = $password;
32 4
    }
33
34
    /**
35
     * @param string|null $domain
36
     *
37
     * @return mixed|null
38
     * @throws QueryErrorException
39
     */
40 2
    public function execute($domain = null)
41
    {
42 2
        $response = $this->client->post(
43 2
            $this->url,
44
            [
45
                'form_params' => [
46 2
                    'login' => $this->login,
47 2
                    'passwd' => $this->password,
48 2
                    'label' => $domain,
49 2
                    'type' => 'standard',
50 2
                    'origin_scheme' => 'http',
51 2
                    'origin_url' => 'hosted.adback.co',
52 2
                    'cname' => $domain,
53
                ]
54 2
            ]
55 2
        );
56
57 2
        $data = json_decode($response->getBody()->getContents(), true);
58
59 2
        if ('ok' != $data['status']) {
60 1
            throw new QueryErrorException($data['description']);
61
        }
62
63 1
        return $data['cdnResource']['cdn_url'];
64
    }
65
}
66