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