Tinify.php
159 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Tinify; |
| 4 | |
| 5 | const VERSION = "1.6.4"; |
| 6 | |
| 7 | class Tinify { |
| 8 | const AUTHENTICATED = true; |
| 9 | const ANONYMOUS = false; |
| 10 | |
| 11 | private static $key = NULL; |
| 12 | private static $appIdentifier = NULL; |
| 13 | private static $proxy = NULL; |
| 14 | |
| 15 | private static $compressionCount = NULL; |
| 16 | private static $remainingCredits = NULL; |
| 17 | private static $payingState = NULL; |
| 18 | private static $emailAddress = NULL; |
| 19 | |
| 20 | private static $client = NULL; |
| 21 | |
| 22 | public static function setKey($key) { |
| 23 | self::$key = $key; |
| 24 | self::$client = NULL; |
| 25 | } |
| 26 | |
| 27 | public static function getKey() { |
| 28 | return self::$key; |
| 29 | } |
| 30 | |
| 31 | public static function createKey($email, $options) { |
| 32 | $body = array_merge(array("email" => $email), $options); |
| 33 | $response = self::getClient(self::ANONYMOUS)->request("post", "/keys", $body); |
| 34 | self::setKey($response->body->key); |
| 35 | } |
| 36 | |
| 37 | public static function setAppIdentifier($appIdentifier) { |
| 38 | self::$appIdentifier = $appIdentifier; |
| 39 | self::$client = NULL; |
| 40 | } |
| 41 | |
| 42 | public static function setProxy($proxy) { |
| 43 | self::$proxy = $proxy; |
| 44 | self::$client = NULL; |
| 45 | } |
| 46 | |
| 47 | public static function getCompressionCount() { |
| 48 | return self::$compressionCount; |
| 49 | } |
| 50 | |
| 51 | public static function setCompressionCount($compressionCount) { |
| 52 | self::$compressionCount = $compressionCount; |
| 53 | } |
| 54 | |
| 55 | public static function getRemainingCredits() { |
| 56 | return self::$remainingCredits; |
| 57 | } |
| 58 | |
| 59 | public static function setRemainingCredits($remainingCredits) { |
| 60 | self::$remainingCredits = $remainingCredits; |
| 61 | } |
| 62 | |
| 63 | public static function getPayingState() { |
| 64 | return self::$payingState; |
| 65 | } |
| 66 | |
| 67 | public static function setPayingState($payingState) { |
| 68 | self::$payingState = $payingState; |
| 69 | } |
| 70 | |
| 71 | public static function getEmailAddress() { |
| 72 | return self::$emailAddress; |
| 73 | } |
| 74 | |
| 75 | public static function setEmailAddress($emailAddress) { |
| 76 | self::$emailAddress = $emailAddress; |
| 77 | } |
| 78 | |
| 79 | public static function getClient($mode = self::AUTHENTICATED) { |
| 80 | if ($mode == self::AUTHENTICATED && !self::$key) { |
| 81 | throw new AccountException("Provide an API key with Tinify\setKey(...)"); |
| 82 | } |
| 83 | |
| 84 | if (!self::$client) { |
| 85 | self::$client = new Client(self::$key, self::$appIdentifier, self::$proxy); |
| 86 | } |
| 87 | |
| 88 | return self::$client; |
| 89 | } |
| 90 | |
| 91 | public static function setClient($client) { |
| 92 | self::$client = $client; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | function setKey($key) { |
| 97 | return Tinify::setKey($key); |
| 98 | } |
| 99 | |
| 100 | function getKey() { |
| 101 | return Tinify::getKey(); |
| 102 | } |
| 103 | |
| 104 | function createKey($email, $options) { |
| 105 | return Tinify::createKey($email, $options); |
| 106 | } |
| 107 | |
| 108 | function setAppIdentifier($appIdentifier) { |
| 109 | return Tinify::setAppIdentifier($appIdentifier); |
| 110 | } |
| 111 | |
| 112 | function setProxy($proxy) { |
| 113 | return Tinify::setProxy($proxy); |
| 114 | } |
| 115 | |
| 116 | function getCompressionCount() { |
| 117 | return Tinify::getCompressionCount(); |
| 118 | } |
| 119 | |
| 120 | function compressionCount() { |
| 121 | return Tinify::getCompressionCount(); |
| 122 | } |
| 123 | |
| 124 | function remainingCredits() { |
| 125 | return Tinify::getRemainingCredits(); |
| 126 | } |
| 127 | |
| 128 | function payingState() { |
| 129 | return Tinify::getPayingState(); |
| 130 | } |
| 131 | |
| 132 | function emailAddress() { |
| 133 | return Tinify::getEmailAddress(); |
| 134 | } |
| 135 | |
| 136 | function fromFile($path) { |
| 137 | return Source::fromFile($path); |
| 138 | } |
| 139 | |
| 140 | function fromBuffer($string) { |
| 141 | return Source::fromBuffer($string); |
| 142 | } |
| 143 | |
| 144 | function fromUrl($string) { |
| 145 | return Source::fromUrl($string); |
| 146 | } |
| 147 | |
| 148 | function validate() { |
| 149 | try { |
| 150 | Tinify::getClient()->request("get", "/keys/" . Tinify::getKey()); |
| 151 | return true; |
| 152 | } catch (AccountException $err) { |
| 153 | if ($err->status == 429) return true; |
| 154 | throw $err; |
| 155 | } catch (ClientException $err) { |
| 156 | return true; |
| 157 | } |
| 158 | } |
| 159 |