Api
3 years ago
Integrations
3 years ago
StorageDriver
4 years ago
Url
3 years ago
data
6 years ago
Api.php
3 years ago
Backlog.php
4 years ago
BacklogReplayTimeoutException.php
4 years ago
ChallengeProcessingException.php
4 years ago
ChallengeVerificationException.php
4 years ago
ConfigFetcherException.php
4 years ago
Crypto.php
3 years ago
Device.php
6 years ago
DeviceType.php
6 years ago
ElementRevision.php
3 years ago
EmptyConfigException.php
6 years ago
FileHandle.php
5 years ago
Filesystem.php
4 years ago
HealthStatus.php
5 years ago
IntegrationUrl.php
6 years ago
NitroPack.php
3 years ago
NoConfigException.php
5 years ago
Pagecache.php
3 years ago
PurgeType.php
3 years ago
ServiceDownException.php
5 years ago
StorageException.php
6 years ago
VariationCookieException.php
5 years ago
WebhookException.php
5 years ago
Website.php
6 years ago
Api.php
391 lines
| 1 | <?php |
| 2 | |
| 3 | namespace NitroPack\SDK; |
| 4 | |
| 5 | use \NitroPack\SDK\Website as Website; |
| 6 | |
| 7 | class Api |
| 8 | { |
| 9 | private $allowedWebhooks = array('config', 'cache_clear', 'cache_ready', 'sitemap'); |
| 10 | private $children; |
| 11 | |
| 12 | public function __construct($siteId, $siteSecret) |
| 13 | { |
| 14 | $this->children = []; |
| 15 | $this->children["cache"] = new Api\Cache($siteId, $siteSecret); |
| 16 | $this->children["tagger"] = new Api\Tagger($siteId, $siteSecret); |
| 17 | $this->children["url"] = new Api\Url($siteId, $siteSecret); |
| 18 | $this->children["stats"] = new Api\Stats($siteId, $siteSecret); |
| 19 | $this->children["webhook"] = new Api\Webhook($siteId, $siteSecret); |
| 20 | $this->children["warmup"] = new Api\Warmup($siteId, $siteSecret); |
| 21 | $this->children["integration"] = new Api\Integration($siteId, $siteSecret); |
| 22 | $this->children["variation_cookie"] = new Api\VariationCookie($siteId, $siteSecret); |
| 23 | $this->children["safe_mode"] = new Api\SafeMode($siteId, $siteSecret); |
| 24 | $this->children["request_maker"] = new Api\RequestMaker($siteId); |
| 25 | $this->children["secure_request_maker"] = new Api\SecureRequestMaker($siteId, $siteSecret); |
| 26 | $this->children["varnish"] = new Api\Varnish($siteId, $siteSecret); |
| 27 | $this->children["excludedurls"] = new Api\ExcludedUrls($siteId, $siteSecret); |
| 28 | $this->children["additionaldomains"] = new Api\AdditionalDomains($siteId, $siteSecret); |
| 29 | } |
| 30 | |
| 31 | public function __get($prop) |
| 32 | { |
| 33 | return !empty($this->children[$prop]) ? $this->children[$prop] : NULL; |
| 34 | } |
| 35 | |
| 36 | public function setBacklog($backlog) |
| 37 | { |
| 38 | foreach ($this->children as $child) { |
| 39 | $child->setBacklog($backlog); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | public function setNitroPack($nitropack) |
| 44 | { |
| 45 | foreach ($this->children as $child) { |
| 46 | $child->setNitroPack($nitropack); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | public function getCache($url, $userAgent, $cookies, $isAjax, $layout, $referer) |
| 51 | { |
| 52 | return $this->cache->get($url, $userAgent, $cookies, $isAjax, $layout, NULL, $referer); |
| 53 | } |
| 54 | |
| 55 | public function getLastCachePurge() |
| 56 | { |
| 57 | return $this->cache->getLastPurge(); |
| 58 | } |
| 59 | |
| 60 | public function purgeCache($url = NULL, $pagecacheOnly = false, $reason = NULL, $lightPurge = false) |
| 61 | { |
| 62 | return $this->cache->purge($url, $pagecacheOnly, $reason, $lightPurge); |
| 63 | } |
| 64 | |
| 65 | public function purgeCacheByTag($tag, $reason = NULL, $lightPurge = false) |
| 66 | { |
| 67 | return $this->cache->purgeByTag($tag, $reason, $lightPurge); |
| 68 | } |
| 69 | |
| 70 | public function getUrls($page = 1, $limit = 250, $search = NULL, $deviceType = NULL, $status = NULL) |
| 71 | { |
| 72 | return $this->url->get($page, $limit, $search, $deviceType, $status); |
| 73 | } |
| 74 | |
| 75 | public function getUrlsCount($search = NULL, $deviceType = NULL, $status = NULL) |
| 76 | { |
| 77 | $resp = $this->url->count($search, $deviceType, $status); |
| 78 | return (int)$resp["count"]; |
| 79 | } |
| 80 | |
| 81 | public function getPendingUrls($page = 1, $limit = 250, $priority = NULL) |
| 82 | { |
| 83 | return $this->url->getpending($page, $limit, $priority); |
| 84 | } |
| 85 | |
| 86 | public function getPendingUrlsCount($priority = NULL) |
| 87 | { |
| 88 | $resp = $this->url->pendingCount($priority); |
| 89 | return (int)$resp["count"]; |
| 90 | } |
| 91 | |
| 92 | public function tagUrl($url, $tag) |
| 93 | { |
| 94 | $resp = @json_decode($this->tagger->tag($url, $tag)->getBody()); |
| 95 | if ($resp && !$resp->success) { |
| 96 | $msg = $resp->error ? $resp->error : "Unable to tag URL"; |
| 97 | throw new \RuntimeException($msg); |
| 98 | } |
| 99 | |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | public function untagUrl($url, $tag) |
| 104 | { |
| 105 | $resp = json_decode($this->tagger->remove($url, $tag)->getBody(), true); |
| 106 | return $resp['removed']; |
| 107 | } |
| 108 | |
| 109 | public function getTaggedUrls($tag, $page = 1, $limit = 250) |
| 110 | { |
| 111 | return $this->tagger->getUrls($tag, $page, $limit); |
| 112 | } |
| 113 | |
| 114 | public function getTaggedUrlsCount($tag) |
| 115 | { |
| 116 | $resp = $this->tagger->getUrlsCount($tag); |
| 117 | return (int)$resp["count"]; |
| 118 | } |
| 119 | |
| 120 | public function getTags($url = NULL, $page = 1, $limit = 250) |
| 121 | { |
| 122 | return $this->tagger->getTags($url, $page, $limit); |
| 123 | } |
| 124 | |
| 125 | public function getSavings() |
| 126 | { |
| 127 | return $this->stats->getSavings(); |
| 128 | } |
| 129 | |
| 130 | public function getDiskUsage() |
| 131 | { |
| 132 | return $this->stats->getDiskUsage(); |
| 133 | } |
| 134 | |
| 135 | public function getRequestUsage() |
| 136 | { |
| 137 | return $this->stats->getRequestUsage(); |
| 138 | } |
| 139 | |
| 140 | public function resetSavingsStats() |
| 141 | { |
| 142 | return $this->stats->resetSavings(); |
| 143 | } |
| 144 | |
| 145 | public function enableWarmup() |
| 146 | { |
| 147 | return $this->warmup->enable(); |
| 148 | } |
| 149 | |
| 150 | public function disableWarmup() |
| 151 | { |
| 152 | return $this->warmup->disable(); |
| 153 | } |
| 154 | |
| 155 | public function resetWarmup() |
| 156 | { |
| 157 | return $this->warmup->reset(); |
| 158 | } |
| 159 | |
| 160 | public function setWarmupSitemap($url) |
| 161 | { |
| 162 | return $this->warmup->setSitemap($url); |
| 163 | } |
| 164 | |
| 165 | public function unsetWarmupSitemap() |
| 166 | { |
| 167 | return $this->warmup->setSitemap(NULL); |
| 168 | } |
| 169 | |
| 170 | public function setWarmupHomepage($url) |
| 171 | { |
| 172 | return $this->warmup->setHomepage($url); |
| 173 | } |
| 174 | |
| 175 | public function unsetWarmupHomepage() |
| 176 | { |
| 177 | return $this->warmup->setHomepage(NULL); |
| 178 | } |
| 179 | |
| 180 | public function estimateWarmup($id = NULL, $urls = NULL) |
| 181 | { |
| 182 | $resp = $this->warmup->estimate($id, $urls); |
| 183 | if ($id) { |
| 184 | return (int)$resp["count"]; |
| 185 | } else { |
| 186 | return $resp["id"]; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | public function runWarmup($urls = NULL, $force = false) |
| 191 | { |
| 192 | return $this->warmup->run($urls, $force); |
| 193 | } |
| 194 | |
| 195 | public function getWarmupStats() |
| 196 | { |
| 197 | return $this->warmup->stats(); |
| 198 | } |
| 199 | |
| 200 | public function unsetWebhook($type) |
| 201 | { |
| 202 | if (!in_array($type, $this->allowedWebhooks)) { |
| 203 | throw new WebhookException("The webhook type '$type' is not supported!"); |
| 204 | } |
| 205 | |
| 206 | try { |
| 207 | $this->webhook->set($type, null); |
| 208 | } catch (\RuntimeException $e) { |
| 209 | throw new WebhookException($e->getMessage()); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | public function setWebhook($type, \NitroPack\Url\Url $url) |
| 214 | { |
| 215 | if (!in_array($type, $this->allowedWebhooks)) { |
| 216 | throw new WebhookException("The webhook type '$type' is not supported!"); |
| 217 | } |
| 218 | |
| 219 | if (!filter_var($url->getUrl(), FILTER_VALIDATE_URL)) { |
| 220 | throw new WebhookException(sprintf("The webhook URL '%s' is invalid!", $url->getUrl())); |
| 221 | } |
| 222 | |
| 223 | try { |
| 224 | $this->webhook->set($type, $url->getUrl()); |
| 225 | } catch (\RuntimeException $e) { |
| 226 | throw new WebhookException($e->getMessage()); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | public function getWebhook($type) |
| 231 | { |
| 232 | if (!in_array($type, $this->allowedWebhooks)) { |
| 233 | throw new WebhookException("The webhook type '$type' is not supported!"); |
| 234 | } |
| 235 | |
| 236 | try { |
| 237 | $resp = $this->webhook->get($type); |
| 238 | return $resp["url"]; |
| 239 | } catch (\RuntimeException $e) { |
| 240 | throw new WebhookException($e->getMessage()); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | public function setVariationCookie($name, $values = array(), $group = null) |
| 245 | { |
| 246 | if (!is_array($values) && !is_string($values)) { |
| 247 | throw new VariationCookieException("The provided values is not an array or a string."); |
| 248 | } else if (is_array($values)) { |
| 249 | foreach ($values as $value) { |
| 250 | if (!is_string($value)) { |
| 251 | throw new VariationCookieException("A non-string cookie value has been detected."); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | if (!is_string($name) || trim($name) == "") { |
| 257 | throw new VariationCookieException("The provided cookie name is not a string or is empty."); |
| 258 | } |
| 259 | |
| 260 | if (null !== $group && !filter_var($group, FILTER_VALIDATE_INT)) { |
| 261 | throw new VariationCookieException("The provided group is not null, and it is not a positive integer."); |
| 262 | } |
| 263 | |
| 264 | try { |
| 265 | return $this->variation_cookie->set($name, $values, $group); |
| 266 | } catch (\RuntimeException $e) { |
| 267 | throw new VariationCookieException($e->getMessage()); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | public function unsetVariationCookie($name) |
| 272 | { |
| 273 | if (!is_string($name) || trim($name) == "") { |
| 274 | throw new VariationCookieException("The provided cookie name is not a string or is empty."); |
| 275 | } |
| 276 | |
| 277 | try { |
| 278 | return $this->variation_cookie->delete($name); |
| 279 | } catch (\RuntimeException $e) { |
| 280 | throw new VariationCookieException($e->getMessage()); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | public function getVariationCookies() |
| 285 | { |
| 286 | try { |
| 287 | return $this->variation_cookie->get(); |
| 288 | } catch (\RuntimeException $e) { |
| 289 | throw new VariationCookieException($e->getMessage()); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | public function createWebsite(Website $website) |
| 294 | { |
| 295 | return $this->integration->create($website); |
| 296 | } |
| 297 | |
| 298 | public function updateWebsite(Website $website) |
| 299 | { |
| 300 | return $this->integration->update($website); |
| 301 | } |
| 302 | |
| 303 | public function removeWebsite($apikey) |
| 304 | { |
| 305 | return $this->integration->remove($apikey); |
| 306 | } |
| 307 | |
| 308 | // Get a single website via API key |
| 309 | public function getWebsiteByAPIKey($apikey) |
| 310 | { |
| 311 | return $this->integration->readByAPIKey($apikey); |
| 312 | } |
| 313 | |
| 314 | // Get all websites, along with a corresponding pagination |
| 315 | public function getWebsitesPaginated($page, $limit = 250) |
| 316 | { |
| 317 | return $this->integration->readPaginated($page, $limit); |
| 318 | } |
| 319 | |
| 320 | public function isSafeModeEnabled() |
| 321 | { |
| 322 | $status = $this->safe_mode->status(); |
| 323 | return !empty($status->isEnabled); |
| 324 | } |
| 325 | |
| 326 | public function enableCartCache() |
| 327 | { |
| 328 | $this->cache->enableCartCache(); |
| 329 | } |
| 330 | |
| 331 | public function disableCartCache() |
| 332 | { |
| 333 | $this->cache->disableCartCache(); |
| 334 | } |
| 335 | |
| 336 | public function enableVarnishIntegration() |
| 337 | { |
| 338 | $this->varnish->enable(); |
| 339 | } |
| 340 | |
| 341 | public function disableVarnishIntegration() |
| 342 | { |
| 343 | $this->varnish->disable(); |
| 344 | } |
| 345 | |
| 346 | public function configureVarnishIntegration($settings) |
| 347 | { |
| 348 | $this->varnish->configure($settings); |
| 349 | } |
| 350 | |
| 351 | public function enableExcludedUrls() |
| 352 | { |
| 353 | $this->excludedurls->enable(); |
| 354 | } |
| 355 | |
| 356 | public function disableExcludedUrls() |
| 357 | { |
| 358 | $this->excludedurls->disable(); |
| 359 | } |
| 360 | |
| 361 | public function addExcludedUrl($urlPattern) |
| 362 | { |
| 363 | $this->excludedurls->add($urlPattern); |
| 364 | } |
| 365 | |
| 366 | public function removeExcludedUrl($urlPattern) |
| 367 | { |
| 368 | $this->excludedurls->remove($urlPattern); |
| 369 | } |
| 370 | |
| 371 | public function enableAdditionalDomains() |
| 372 | { |
| 373 | $this->additionaldomains->enable(); |
| 374 | } |
| 375 | |
| 376 | public function disableAdditionalDomains() |
| 377 | { |
| 378 | $this->additionaldomains->disable(); |
| 379 | } |
| 380 | |
| 381 | public function addAdditionalDomain($domain) |
| 382 | { |
| 383 | $this->additionaldomains->add($domain); |
| 384 | } |
| 385 | |
| 386 | public function removeAdditionalDomain($domain) |
| 387 | { |
| 388 | $this->additionaldomains->remove($domain); |
| 389 | } |
| 390 | } |
| 391 |