Completed
Pull Request — master (#161)
by
unknown
07:58
created

WebmasterClient::sendRequest()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 30
Code Lines 17

Duplication

Lines 30
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 6
eloc 17
c 1
b 0
f 1
nc 5
nop 3
dl 30
loc 30
rs 8.439
1
<?php
2
namespace Yandex\Webmaster;
3
4
use GuzzleHttp\ClientInterface;
5
use GuzzleHttp\Exception\ClientException;
6
use Yandex\Common\AbstractServiceClient;
7
use Yandex\Common\Exception\MissedArgumentException;
8
use Yandex\Common\Exception\ProfileNotFoundException;
9
use Yandex\Common\Exception\YandexException;
10
use Yandex\Webmaster\Models\GetHostsResponse;
11
use Yandex\Webmaster\Models\GetHostStatsResponse;
12
use Yandex\Webmaster\Models\GetHostVerifyResponse;
13
use Yandex\Webmaster\Models\Host;
14
15
/**
16
 * Class WebmasterClient
17
 *
18
 * @package Yandex\Webmaster
19
 */
20
class WebmasterClient extends AbstractServiceClient
21
{
22
23
    /**
24
     * @var string $serviceDomain
25
     */
26
    protected $serviceDomain = 'webmaster.yandex.ru/api/v2';
27
28
    /**
29
     * WebmasterClient constructor.
30
     *
31
     * @param string               $token
32
     * @param ClientInterface|null $client
33
     */
34
    public function __construct($token = '', ClientInterface $client = null)
35
    {
36
        $this->setAccessToken($token);
37
        if ($client instanceof ClientInterface) {
38
            $this->setClient($client);
39
        }
40
    }
41
42
    /**
43
     * @see https://tech.yandex.ru/webmaster/doc/dg/reference/hosts-docpage/
44
     *
45
     * @return Models\Hosts
46
     */
47
    public function getHosts()
48
    {
49
        $resource = 'hosts';
50
51
        $response = $this->sendGetRequest($resource);
52
        $hostsResponse = new GetHostsResponse($response);
53
54
        return $hostsResponse->getHosts();
55
    }
56
57
    /**
58
     * @see https://tech.yandex.ru/webmaster/doc/dg/reference/hosts-stats-docpage/
59
     *
60
     * @param $id
61
     * @return Host
62
     */
63
    public function getHostStats($id)
64
    {
65
        $resource = 'hosts/'.$id.'/stats';
66
67
        $response = $this->sendGetRequest($resource);
68
        $hostStatsResponse = new GetHostStatsResponse($response);
69
70
        return $hostStatsResponse->getHost();
71
    }
72
73
74
75
    public function getHostVerify($id)
76
    {
77
        $resource = 'hosts/'.$id.'/verify';
78
79
        $response = $this->sendGetRequest($resource);
80
        $hostVerifyResponse = new GetHostVerifyResponse($response);
81
82
        return $hostVerifyResponse->getHost();
83
    }
84
85
    /**
86
     * @param string $resource
87
     * @return string
88
     */
89
    public function getServiceUrl($resource = '')
90
    {
91
        return $this->serviceScheme.'://'.$this->serviceDomain.'/'.$resource;
92
    }
93
94
    /**
95
     * @param string                                $method
96
     * @param \Psr\Http\Message\UriInterface|string $uri
97
     * @param array                                 $options
98
     * @return \Psr\Http\Message\ResponseInterface
99
     * @throws MissedArgumentException
100
     * @throws ProfileNotFoundException
101
     * @throws YandexException
102
     */
103 View Code Duplication
    protected function sendRequest($method, $uri, array $options = [])
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...
104
    {
105
        //todo: implement Webmaster Exceptions
106
        try {
107
            $response = $this->getClient()->request($method, $uri, $options);
108
        } catch (ClientException $ex) {
109
            // get error from response
110
            $decodedResponseBody = $this->getDecodedBody($ex->getResponse()->getBody());
111
            $code = $ex->getResponse()->getStatusCode();
112
113
            // handle a service error message
114
            if (is_array($decodedResponseBody)
115
                && isset($decodedResponseBody['error'], $decodedResponseBody['message'])
116
            ) {
117
                switch ($decodedResponseBody['error']) {
118
                    case 'MissedRequiredArguments':
119
                        throw new MissedArgumentException($decodedResponseBody['message']);
120
                    case 'AssistantProfileNotFound':
121
                        throw new ProfileNotFoundException($decodedResponseBody['message']);
122
                    default:
123
                        throw new YandexException($decodedResponseBody['message'], $code);
124
                }
125
            }
126
127
            // unknown error
128
            throw $ex;
129
        }
130
131
        return $response;
132
    }
133
134
    /**
135
     * @param string $resource
136
     * @return mixed|\SimpleXMLIterator
137
     */
138
    public function sendGetRequest($resource = '')
139
    {
140
        $response = $this->sendRequest(
141
            'GET',
142
            $this->getServiceUrl($resource),
143
            [
144
                'headers' => [
145
                    'Authorization' => 'OAuth '.$this->getAccessToken(),
146
                ],
147
            ]
148
        );
149
150
        $decodedResponseBody = $this->getDecodedBody($response->getBody(), self::DECODE_TYPE_XML);
151
152
        return $decodedResponseBody;
153
    }
154
}
155