Completed
Pull Request — master (#162)
by
unknown
06:39
created

WebmasterClient::getHostStats()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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