These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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
|
|||
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 |
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.