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

CreateResourceQuery   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 12.73 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
c 1
b 0
f 1
lcom 1
cbo 3
dl 7
loc 55
ccs 22
cts 22
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 2
B execute() 0 25 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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