Completed
Push — master ( b8323d...c7c022 )
by Anton
06:31 queued 04:22
created

src/Yandex/SiteSearchPinger/SiteSearchPinger.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Yandex PHP Library
4
 *
5
 * @copyright NIX Solutions Ltd.
6
 * @link https://github.com/nixsolutions/yandex-php-library
7
 */
8
9
/**
10
 * @namespace
11
 */
12
namespace Yandex\SiteSearchPinger;
13
14
use Yandex\Common\AbstractServiceClient;
15
use Yandex\Common\Exception\InvalidArgumentException;
16
use Yandex\SiteSearchPinger\Exception\InvalidUrlException;
17
use Yandex\SiteSearchPinger\Exception\SiteSearchPingerException;
18
use GuzzleHttp\Psr7\Response;
19
use GuzzleHttp\Exception\ClientException;
20
21
/**
22
 * SiteSearchPinger
23
 *
24
 * @category Yandex
25
 * @package  SiteSearchPinger
26
 *
27
 * @property string $key
28
 * @property string $login
29
 * @property string $searchId
30
 * @property array $invalidUrls
31
 *
32
 * @author   Anton Shevchuk
33
 * @created  06.08.13 17:30
34
 */
35
class SiteSearchPinger extends AbstractServiceClient
36
{
37
    const DECODE_TYPE_DEFAULT = self::DECODE_TYPE_XML;
38
39
    /**
40
     * @var string
41
     */
42
    protected $serviceScheme = self::HTTP_SCHEME;
43
44
    /**
45
     * @var string
46
     */
47
    protected $serviceProtocolVersion = '1.0';
48
49
    /**
50
     * @var string
51
     */
52
    protected $serviceDomain = "site.yandex.ru";
53
54
    /**
55
     * @var string
56
     */
57
    protected $path = "ping.xml";
58
59
    /**
60
     * @var string
61
     */
62
    protected $key;
63
64
    /**
65
     * @var string
66
     */
67
    protected $login;
68
69
    /**
70
     * @var string
71
     */
72
    protected $searchId;
73
74
    /**
75
     * Connection Errors
76
     */
77
    const ERROR_ILLEGAL_PARAM_VALUE = 'ILLEGAL_PARAM_VALUE';
78
    const ERROR_ILLEGAL_VALUE_TYPE = 'ILLEGAL_VALUE_TYPE';
79
    const ERROR_NO_SUCH_USER_IN_PASSPORT = 'NO_SUCH_USER_IN_PASSPORT';
80
    const ERROR_SEARCH_NOT_OWNED_BY_USER = 'SEARCH_NOT_OWNED_BY_USER';
81
    const ERROR_TOO_DELAYED_PUBLISH = 'TOO_DELAYED_PUBLISH';
82
    const ERROR_USER_NOT_PERMITTED = 'USER_NOT_PERMITTED';
83
84
    /**
85
     * URL Errors
86
     */
87
    const INVALID_MALFORMED_URLS = 'MALFORMED_URLS';
88
    const INVALID_NOT_CONFIRMED_IN_WMC = 'NOT_CONFIRMED_IN_WMC';
89
    const INVALID_OUT_OF_SEARCH_AREA = 'OUT_OF_SEARCH_AREA';
90
91
    /**
92
     * @var array
93
     */
94
    protected $invalid = [
95
        self::INVALID_MALFORMED_URLS => "Invalid URL format",
96
        self::INVALID_NOT_CONFIRMED_IN_WMC => "Invalid site URL. Site is not confirmed on http://webmaster.yandex.ru/",
97
        self::INVALID_OUT_OF_SEARCH_AREA => "Invalid site URL. Site is not under your search area",
98
    ];
99
100
    /**
101
     * @var array
102
     */
103
    protected $invalidUrls = [];
104
105
    /**
106
     * set search key
107
     *
108
     * @param $value
109
     * @return self
110
     */
111 6
    public function setKey($value)
112
    {
113 6
        $this->key = $value;
114 6
        return $this;
115
    }
116
117
    /**
118
     * set search login
119
     *
120
     * @param $value
121
     * @return self
122
     */
123 6
    public function setLogin($value)
124
    {
125 6
        $this->login = $value;
126 6
        return $this;
127
    }
128
129
    /**
130
     * set search id
131
     *
132
     * @param $value
133
     * @return self
134
     */
135 6
    public function setSearchId($value)
136
    {
137 6
        $this->searchId = $value;
138 6
        return $this;
139
    }
140
141
    /**
142
     * get invalid Urls from request
143
     *
144
     * @return array
145
     */
146 1
    public function getInvalidUrls()
147
    {
148 1
        return $this->invalidUrls;
149
    }
150
151
    /**
152
     * ping
153
     *
154
     * @param string|array $urls
155
     * @param integer $publishDate seconds from now to publish urls
156
     *
157
     * @throws Exception\SiteSearchPingerException
158
     * @throws Exception\InvalidUrlException
159
     * @throws \Yandex\Common\Exception\InvalidArgumentException
160
     * @return int
161
     */
162 7
    public function ping($urls, $publishDate = 0)
163
    {
164 7
        $this->checkSettings();
165
166 6
        $urls = (array)$urls;
167
168
        try {
169 6
            $response = $this->doRequest($urls, $publishDate);
170 6
        } catch (ClientException $e) {
171 3
            $xml = $this->getDecodedBody($e->getResponse()->getBody());
172
173 3
            if (isset($xml->error) && isset($xml->error->message)) {
174 2
                $errorMessage = (string) $xml->error->message;
175 2
                if (isset($xml->error->param) && isset($xml->error->value)) {
176 1
                    $errorMessage .= " (".$xml->error->param." is ".$xml->error->value.")";
177 1
                }
178 2
                throw new InvalidArgumentException($errorMessage);
179
            }
180 1
            return 0;
181
        }
182
183 3
        if (!$xml = $this->getDecodedBody($response->getBody())) {
184 1
            throw new SiteSearchPingerException("Wrong server response format");
185 2
        } elseif ($xml->getName() == 'empty-param') {
186
            // workaround for invalid request, with empty `urls`
187 1
            throw new InvalidUrlException("URL param is required");
188
        }
189
190
        // retrieve count of valid urls
191 1
        $addedCount = 0;
192 1
        if (isset($xml->added) && isset($xml->added['count'])) {
193 1
            $addedCount = (int)$xml->added['count'];
194 1
        }
195
196
        // check invalid urls and fill errors stack
197 1
        $this->invalidUrls = [];
198 1
        if (isset($xml->invalid)) {
199 1
            foreach ($xml->invalid as $invalid) {
200 1
                foreach ($invalid as $url) {
201 1
                    $this->invalidUrls[(string)$url] = $this->invalid[(string) $invalid['reason']];
202 1
                }
203 1
            }
204 1
        }
205
206 1
        return $addedCount;
207
    }
208
209
    /**
210
     * doCheckOptions
211
     *
212
     * @return boolean
213
     */
214 7
    protected function doCheckSettings()
215
    {
216 7
        return $this->key && $this->login && $this->searchId;
217
    }
218
219
    /**
220
     * @param array $urls
221
     * @param integer $publishDate
222
     *
223
     * @return Response;
0 ignored issues
show
The doc-type Response; could not be parsed: Expected "|" or "end of type", but got ";" at position 8. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
224
     */
225 6
    protected function doRequest($urls, $publishDate)
226
    {
227 6
        $client = $this->getClient();
228
229 6
        return $client->request(
230 6
            'POST',
231 6
            '/' . $this->path,
232
            [
233 6
                'version' => $this->serviceProtocolVersion,
234
                'form_params' => [
235 6
                    'urls' => join("\n", $urls),
236
                    'publishdate' => $publishDate
237 6
                ],
238
                'query' => [
239 6
                    'key' => $this->key,
240 6
                    'login' => $this->login,
241 6
                    'search_id' => $this->searchId
242 6
                ],
243
                'headers' => [
244
                    'Y-SDK' => 'Pinger'
245 6
                ]
246 6
            ]
247 6
        );
248
    }
249
}
250