Api
3 years ago
Integrations
4 years ago
StorageDriver
4 years ago
Url
6 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
6 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
6 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
289 lines
| 1 | <?php |
| 2 | namespace NitroPack\SDK; |
| 3 | |
| 4 | use \NitroPack\SDK\Website as Website; |
| 5 | |
| 6 | class Api { |
| 7 | private $cache; |
| 8 | private $tagger; |
| 9 | private $url; |
| 10 | private $allowedWebhooks = array('config', 'cache_clear', 'cache_ready', 'sitemap'); |
| 11 | private $children; |
| 12 | |
| 13 | public function __construct($siteId, $siteSecret) { |
| 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 | |
| 27 | foreach ($this->children as $name=>$child) { |
| 28 | $this->{$name} = $child; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | public function setBacklog($backlog) { |
| 33 | foreach ($this->children as $child) { |
| 34 | $child->setBacklog($backlog); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | public function setNitroPack($nitropack) { |
| 39 | foreach ($this->children as $child) { |
| 40 | $child->setNitroPack($nitropack); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | public function getCache($url, $userAgent, $cookies, $isAjax, $layout) { |
| 45 | return $this->cache->get($url, $userAgent, $cookies, $isAjax, $layout); |
| 46 | } |
| 47 | |
| 48 | public function getLastCachePurge() { |
| 49 | return $this->cache->getLastPurge(); |
| 50 | } |
| 51 | |
| 52 | public function purgeCache($url = NULL, $pagecacheOnly = false, $reason = NULL) { |
| 53 | return $this->cache->purge($url, $pagecacheOnly, $reason); |
| 54 | } |
| 55 | |
| 56 | public function purgeCacheByTag($tag, $reason = NULL) { |
| 57 | return $this->cache->purgeByTag($tag, $reason); |
| 58 | } |
| 59 | |
| 60 | public function getUrls($page = 1, $limit = 250, $search = NULL, $deviceType = NULL, $status = NULL) { |
| 61 | return $this->url->get($page, $limit, $search, $deviceType, $status); |
| 62 | } |
| 63 | |
| 64 | public function getUrlsCount($search = NULL, $deviceType = NULL, $status = NULL) { |
| 65 | $resp = $this->url->count($search, $deviceType, $status); |
| 66 | return (int)$resp["count"]; |
| 67 | } |
| 68 | |
| 69 | public function getPendingUrls($page = 1, $limit = 250, $priority = NULL) { |
| 70 | return $this->url->getpending($page, $limit, $priority); |
| 71 | } |
| 72 | |
| 73 | public function getPendingUrlsCount($priority = NULL) { |
| 74 | $resp = $this->url->pendingCount($priority); |
| 75 | return (int)$resp["count"]; |
| 76 | } |
| 77 | |
| 78 | public function tagUrl($url, $tag) { |
| 79 | $resp = @json_decode($this->tagger->tag($url, $tag)->getBody()); |
| 80 | if ($resp && !$resp->success) { |
| 81 | $msg = $resp->error ? $resp->error : "Unable to tag URL"; |
| 82 | throw new \RuntimeException($msg); |
| 83 | } |
| 84 | |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | public function untagUrl($url, $tag) { |
| 89 | $resp = json_decode($this->tagger->remove($url, $tag)->getBody(), true); |
| 90 | return $resp['removed']; |
| 91 | } |
| 92 | |
| 93 | public function getTaggedUrls($tag, $page = 1, $limit = 250) { |
| 94 | return $this->tagger->getUrls($tag, $page, $limit); |
| 95 | } |
| 96 | |
| 97 | public function getTaggedUrlsCount($tag) { |
| 98 | $resp = $this->tagger->getUrlsCount($tag); |
| 99 | return (int)$resp["count"]; |
| 100 | } |
| 101 | |
| 102 | public function getTags($url = NULL, $page = 1, $limit = 250) { |
| 103 | return $this->tagger->getTags($url, $page, $limit); |
| 104 | } |
| 105 | |
| 106 | public function getSavings() { |
| 107 | return $this->stats->getSavings(); |
| 108 | } |
| 109 | |
| 110 | public function getDiskUsage() { |
| 111 | return $this->stats->getDiskUsage(); |
| 112 | } |
| 113 | |
| 114 | public function getRequestUsage() { |
| 115 | return $this->stats->getRequestUsage(); |
| 116 | } |
| 117 | |
| 118 | public function resetSavingsStats() { |
| 119 | return $this->stats->resetSavings(); |
| 120 | } |
| 121 | |
| 122 | public function enableWarmup() { |
| 123 | return $this->warmup->enable(); |
| 124 | } |
| 125 | |
| 126 | public function disableWarmup() { |
| 127 | return $this->warmup->disable(); |
| 128 | } |
| 129 | |
| 130 | public function resetWarmup() { |
| 131 | return $this->warmup->reset(); |
| 132 | } |
| 133 | |
| 134 | public function setWarmupSitemap($url) { |
| 135 | return $this->warmup->setSitemap($url); |
| 136 | } |
| 137 | |
| 138 | public function unsetWarmupSitemap() { |
| 139 | return $this->warmup->setSitemap(NULL); |
| 140 | } |
| 141 | |
| 142 | public function setWarmupHomepage($url) { |
| 143 | return $this->warmup->setHomepage($url); |
| 144 | } |
| 145 | |
| 146 | public function unsetWarmupHomepage() { |
| 147 | return $this->warmup->setHomepage(NULL); |
| 148 | } |
| 149 | |
| 150 | public function estimateWarmup($id = NULL, $urls = NULL) { |
| 151 | $resp = $this->warmup->estimate($id, $urls); |
| 152 | if ($id) { |
| 153 | return (int)$resp["count"]; |
| 154 | } else { |
| 155 | return $resp["id"]; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | public function runWarmup($urls = NULL, $force = false) { |
| 160 | return $this->warmup->run($urls, $force); |
| 161 | } |
| 162 | |
| 163 | public function getWarmupStats() { |
| 164 | return $this->warmup->stats(); |
| 165 | } |
| 166 | |
| 167 | public function unsetWebhook($type) { |
| 168 | if (!in_array($type, $this->allowedWebhooks)) { |
| 169 | throw new WebhookException("The webhook type '$type' is not supported!"); |
| 170 | } |
| 171 | |
| 172 | try { |
| 173 | $this->webhook->set($type, null); |
| 174 | } catch (\RuntimeException $e) { |
| 175 | throw new WebhookException($e->getMessage()); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | public function setWebhook($type, \NitroPack\Url $url) { |
| 180 | if (!in_array($type, $this->allowedWebhooks)) { |
| 181 | throw new WebhookException("The webhook type '$type' is not supported!"); |
| 182 | } |
| 183 | |
| 184 | if (!filter_var($url->getUrl(), FILTER_VALIDATE_URL)) { |
| 185 | throw new WebhookException(sprintf("The webhook URL '%s' is invalid!", $url->getUrl())); |
| 186 | } |
| 187 | |
| 188 | try { |
| 189 | $this->webhook->set($type, $url->getUrl()); |
| 190 | } catch (\RuntimeException $e) { |
| 191 | throw new WebhookException($e->getMessage()); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | public function getWebhook($type) { |
| 196 | if (!in_array($type, $this->allowedWebhooks)) { |
| 197 | throw new WebhookException("The webhook type '$type' is not supported!"); |
| 198 | } |
| 199 | |
| 200 | try { |
| 201 | $resp = $this->webhook->get($type); |
| 202 | return $resp["url"]; |
| 203 | } catch (\RuntimeException $e) { |
| 204 | throw new WebhookException($e->getMessage()); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | public function setVariationCookie($name, $values = array(), $group = null) { |
| 209 | if (!is_array($values) && !is_string($values)) { |
| 210 | throw new VariationCookieException("The provided values is not an array or a string."); |
| 211 | } else if (is_array($values)) { |
| 212 | foreach ($values as $value) { |
| 213 | if (!is_string($value)) { |
| 214 | throw new VariationCookieException("A non-string cookie value has been detected."); |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | if (!is_string($name) || trim($name) == "") { |
| 220 | throw new VariationCookieException("The provided cookie name is not a string or is empty."); |
| 221 | } |
| 222 | |
| 223 | if (null !== $group && !filter_var($group, FILTER_VALIDATE_INT)) { |
| 224 | throw new VariationCookieException("The provided group is not null, and it is not a positive integer."); |
| 225 | } |
| 226 | |
| 227 | try { |
| 228 | return $this->variation_cookie->set($name, $values, $group); |
| 229 | } catch (\RuntimeException $e) { |
| 230 | throw new VariationCookieException($e->getMessage()); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | public function unsetVariationCookie($name) { |
| 235 | if (!is_string($name) || trim($name) == "") { |
| 236 | throw new VariationCookieException("The provided cookie name is not a string or is empty."); |
| 237 | } |
| 238 | |
| 239 | try { |
| 240 | return $this->variation_cookie->delete($name); |
| 241 | } catch (\RuntimeException $e) { |
| 242 | throw new VariationCookieException($e->getMessage()); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | public function getVariationCookies() { |
| 247 | try { |
| 248 | return $this->variation_cookie->get(); |
| 249 | } catch (\RuntimeException $e) { |
| 250 | throw new VariationCookieException($e->getMessage()); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | public function createWebsite(Website $website) { |
| 255 | return $this->integration->create($website); |
| 256 | } |
| 257 | |
| 258 | public function updateWebsite(Website $website) { |
| 259 | return $this->integration->update($website); |
| 260 | } |
| 261 | |
| 262 | public function removeWebsite($apikey) { |
| 263 | return $this->integration->remove($apikey); |
| 264 | } |
| 265 | |
| 266 | // Get a single website via API key |
| 267 | public function getWebsiteByAPIKey($apikey) { |
| 268 | return $this->integration->readByAPIKey($apikey); |
| 269 | } |
| 270 | |
| 271 | // Get all websites, along with a corresponding pagination |
| 272 | public function getWebsitesPaginated($page, $limit = 250) { |
| 273 | return $this->integration->readPaginated($page, $limit); |
| 274 | } |
| 275 | |
| 276 | public function isSafeModeEnabled() { |
| 277 | $status = $this->safe_mode->status(); |
| 278 | return !empty($status->isEnabled); |
| 279 | } |
| 280 | |
| 281 | public function enableCartCache() { |
| 282 | $this->cache->enableCartCache(); |
| 283 | } |
| 284 | |
| 285 | public function disableCartCache() { |
| 286 | $this->cache->disableCartCache(); |
| 287 | } |
| 288 | } |
| 289 |