diff --git a/composer.lock b/composer.lock index aa679b7..3dc3308 100644 --- a/composer.lock +++ b/composer.lock @@ -62,39 +62,136 @@ ], "time": "2015-06-14 21:17:01" }, + { + "name": "phpdocumentor/reflection-common", + "version": "1.0", + "source": { + "type": "git", + "url": "https://github.com/phpDocumentor/ReflectionCommon.git", + "reference": "144c307535e82c8fdcaacbcfc1d6d8eeb896687c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionCommon/zipball/144c307535e82c8fdcaacbcfc1d6d8eeb896687c", + "reference": "144c307535e82c8fdcaacbcfc1d6d8eeb896687c", + "shasum": "" + }, + "require": { + "php": ">=5.5" + }, + "require-dev": { + "phpunit/phpunit": "^4.6" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "phpDocumentor\\Reflection\\": [ + "src" + ] + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jaap van Otterdijk", + "email": "opensource@ijaap.nl" + } + ], + "description": "Common reflection classes used by phpdocumentor to reflect the code structure", + "homepage": "http://www.phpdoc.org", + "keywords": [ + "FQSEN", + "phpDocumentor", + "phpdoc", + "reflection", + "static analysis" + ], + "time": "2015-12-27 11:43:31" + }, { "name": "phpdocumentor/reflection-docblock", - "version": "2.0.4", + "version": "3.1.1", "source": { "type": "git", "url": "https://github.com/phpDocumentor/ReflectionDocBlock.git", - "reference": "d68dbdc53dc358a816f00b300704702b2eaff7b8" + "reference": "8331b5efe816ae05461b7ca1e721c01b46bafb3e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/d68dbdc53dc358a816f00b300704702b2eaff7b8", - "reference": "d68dbdc53dc358a816f00b300704702b2eaff7b8", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/8331b5efe816ae05461b7ca1e721c01b46bafb3e", + "reference": "8331b5efe816ae05461b7ca1e721c01b46bafb3e", "shasum": "" }, "require": { - "php": ">=5.3.3" + "php": ">=5.5", + "phpdocumentor/reflection-common": "^1.0@dev", + "phpdocumentor/type-resolver": "^0.2.0", + "webmozart/assert": "^1.0" }, "require-dev": { - "phpunit/phpunit": "~4.0" + "mockery/mockery": "^0.9.4", + "phpunit/phpunit": "^4.4" }, - "suggest": { - "dflydev/markdown": "~1.0", - "erusev/parsedown": "~1.0" + "type": "library", + "autoload": { + "psr-4": { + "phpDocumentor\\Reflection\\": [ + "src/" + ] + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Mike van Riel", + "email": "me@mikevanriel.com" + } + ], + "description": "With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.", + "time": "2016-09-30 07:12:33" + }, + { + "name": "phpdocumentor/type-resolver", + "version": "0.2", + "source": { + "type": "git", + "url": "https://github.com/phpDocumentor/TypeResolver.git", + "reference": "b39c7a5b194f9ed7bd0dd345c751007a41862443" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/b39c7a5b194f9ed7bd0dd345c751007a41862443", + "reference": "b39c7a5b194f9ed7bd0dd345c751007a41862443", + "shasum": "" + }, + "require": { + "php": ">=5.5", + "phpdocumentor/reflection-common": "^1.0" + }, + "require-dev": { + "mockery/mockery": "^0.9.4", + "phpunit/phpunit": "^5.2||^4.8.24" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "2.0.x-dev" + "dev-master": "1.0.x-dev" } }, "autoload": { - "psr-0": { - "phpDocumentor": [ + "psr-4": { + "phpDocumentor\\Reflection\\": [ "src/" ] } @@ -106,39 +203,39 @@ "authors": [ { "name": "Mike van Riel", - "email": "mike.vanriel@naenius.com" + "email": "me@mikevanriel.com" } ], - "time": "2015-02-03 12:10:50" + "time": "2016-06-10 07:14:17" }, { "name": "phpspec/prophecy", - "version": "v1.6.0", + "version": "v1.6.1", "source": { "type": "git", "url": "https://github.com/phpspec/prophecy.git", - "reference": "3c91bdf81797d725b14cb62906f9a4ce44235972" + "reference": "58a8137754bc24b25740d4281399a4a3596058e0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpspec/prophecy/zipball/3c91bdf81797d725b14cb62906f9a4ce44235972", - "reference": "3c91bdf81797d725b14cb62906f9a4ce44235972", + "url": "https://api.github.com/repos/phpspec/prophecy/zipball/58a8137754bc24b25740d4281399a4a3596058e0", + "reference": "58a8137754bc24b25740d4281399a4a3596058e0", "shasum": "" }, "require": { "doctrine/instantiator": "^1.0.2", "php": "^5.3|^7.0", - "phpdocumentor/reflection-docblock": "~2.0", - "sebastian/comparator": "~1.1", - "sebastian/recursion-context": "~1.0" + "phpdocumentor/reflection-docblock": "^2.0|^3.0.2", + "sebastian/comparator": "^1.1", + "sebastian/recursion-context": "^1.0" }, "require-dev": { - "phpspec/phpspec": "~2.0" + "phpspec/phpspec": "^2.0" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "1.5.x-dev" + "dev-master": "1.6.x-dev" } }, "autoload": { @@ -171,7 +268,7 @@ "spy", "stub" ], - "time": "2016-02-15 07:46:21" + "time": "2016-06-07 08:13:47" }, { "name": "phpunit/php-code-coverage", @@ -325,21 +422,24 @@ }, { "name": "phpunit/php-timer", - "version": "1.0.7", + "version": "1.0.8", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-timer.git", - "reference": "3e82f4e9fc92665fafd9157568e4dcb01d014e5b" + "reference": "38e9124049cf1a164f1e4537caf19c99bf1eb260" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-timer/zipball/3e82f4e9fc92665fafd9157568e4dcb01d014e5b", - "reference": "3e82f4e9fc92665fafd9157568e4dcb01d014e5b", + "url": "https://api.github.com/repos/sebastianbergmann/php-timer/zipball/38e9124049cf1a164f1e4537caf19c99bf1eb260", + "reference": "38e9124049cf1a164f1e4537caf19c99bf1eb260", "shasum": "" }, "require": { "php": ">=5.3.3" }, + "require-dev": { + "phpunit/phpunit": "~4|~5" + }, "type": "library", "autoload": { "classmap": [ @@ -362,7 +462,7 @@ "keywords": [ "timer" ], - "time": "2015-06-21 08:01:12" + "time": "2016-05-12 18:03:57" }, { "name": "phpunit/php-token-stream", @@ -415,16 +515,16 @@ }, { "name": "phpunit/phpunit", - "version": "4.8.24", + "version": "4.8.27", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "a1066c562c52900a142a0e2bbf0582994671385e" + "reference": "c062dddcb68e44b563f66ee319ddae2b5a322a90" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/a1066c562c52900a142a0e2bbf0582994671385e", - "reference": "a1066c562c52900a142a0e2bbf0582994671385e", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/c062dddcb68e44b563f66ee319ddae2b5a322a90", + "reference": "c062dddcb68e44b563f66ee319ddae2b5a322a90", "shasum": "" }, "require": { @@ -438,7 +538,7 @@ "phpunit/php-code-coverage": "~2.1", "phpunit/php-file-iterator": "~1.4", "phpunit/php-text-template": "~1.2", - "phpunit/php-timer": ">=1.0.6", + "phpunit/php-timer": "^1.0.6", "phpunit/phpunit-mock-objects": "~2.3", "sebastian/comparator": "~1.1", "sebastian/diff": "~1.2", @@ -483,7 +583,7 @@ "testing", "xunit" ], - "time": "2016-03-14 06:16:08" + "time": "2016-07-21 06:48:14" }, { "name": "phpunit/phpunit-mock-objects", @@ -659,23 +759,23 @@ }, { "name": "sebastian/environment", - "version": "1.3.5", + "version": "1.3.8", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/environment.git", - "reference": "dc7a29032cf72b54f36dac15a1ca5b3a1b6029bf" + "reference": "be2c607e43ce4c89ecd60e75c6a85c126e754aea" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/dc7a29032cf72b54f36dac15a1ca5b3a1b6029bf", - "reference": "dc7a29032cf72b54f36dac15a1ca5b3a1b6029bf", + "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/be2c607e43ce4c89ecd60e75c6a85c126e754aea", + "reference": "be2c607e43ce4c89ecd60e75c6a85c126e754aea", "shasum": "" }, "require": { - "php": ">=5.3.3" + "php": "^5.3.3 || ^7.0" }, "require-dev": { - "phpunit/phpunit": "~4.4" + "phpunit/phpunit": "^4.8 || ^5.0" }, "type": "library", "extra": { @@ -705,20 +805,20 @@ "environment", "hhvm" ], - "time": "2016-02-26 18:40:46" + "time": "2016-08-18 05:49:44" }, { "name": "sebastian/exporter", - "version": "1.2.1", + "version": "1.2.2", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/exporter.git", - "reference": "7ae5513327cb536431847bcc0c10edba2701064e" + "reference": "42c4c2eec485ee3e159ec9884f95b431287edde4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/7ae5513327cb536431847bcc0c10edba2701064e", - "reference": "7ae5513327cb536431847bcc0c10edba2701064e", + "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/42c4c2eec485ee3e159ec9884f95b431287edde4", + "reference": "42c4c2eec485ee3e159ec9884f95b431287edde4", "shasum": "" }, "require": { @@ -726,12 +826,13 @@ "sebastian/recursion-context": "~1.0" }, "require-dev": { + "ext-mbstring": "*", "phpunit/phpunit": "~4.4" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "1.2.x-dev" + "dev-master": "1.3.x-dev" } }, "autoload": { @@ -771,7 +872,7 @@ "export", "exporter" ], - "time": "2015-06-21 07:55:53" + "time": "2016-06-17 09:04:28" }, { "name": "sebastian/global-state", @@ -914,16 +1015,16 @@ }, { "name": "symfony/yaml", - "version": "v2.8.4", + "version": "v2.8.13", "source": { "type": "git", "url": "https://github.com/symfony/yaml.git", - "reference": "584e52cb8f788a887553ba82db6caacb1d6260bb" + "reference": "396784cd06b91f3db576f248f2402d547a077787" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/yaml/zipball/584e52cb8f788a887553ba82db6caacb1d6260bb", - "reference": "584e52cb8f788a887553ba82db6caacb1d6260bb", + "url": "https://api.github.com/repos/symfony/yaml/zipball/396784cd06b91f3db576f248f2402d547a077787", + "reference": "396784cd06b91f3db576f248f2402d547a077787", "shasum": "" }, "require": { @@ -959,7 +1060,57 @@ ], "description": "Symfony Yaml Component", "homepage": "https://symfony.com", - "time": "2016-03-04 07:54:35" + "time": "2016-10-21 20:59:10" + }, + { + "name": "webmozart/assert", + "version": "1.1.0", + "source": { + "type": "git", + "url": "https://github.com/webmozart/assert.git", + "reference": "bb2d123231c095735130cc8f6d31385a44c7b308" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/webmozart/assert/zipball/bb2d123231c095735130cc8f6d31385a44c7b308", + "reference": "bb2d123231c095735130cc8f6d31385a44c7b308", + "shasum": "" + }, + "require": { + "php": "^5.3.3|^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.6", + "sebastian/version": "^1.0.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.2-dev" + } + }, + "autoload": { + "psr-4": { + "Webmozart\\Assert\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Bernhard Schussek", + "email": "bschussek@gmail.com" + } + ], + "description": "Assertions to validate method input/output with nice error messages.", + "keywords": [ + "assert", + "check", + "validate" + ], + "time": "2016-08-09 15:02:57" } ], "aliases": [], diff --git a/lib/Tinify.php b/lib/Tinify.php index f37c244..0075afe 100644 --- a/lib/Tinify.php +++ b/lib/Tinify.php @@ -9,6 +9,10 @@ class Tinify { private static $appIdentifier = NULL; private static $compressionCount = NULL; private static $client = NULL; + private static $requestTimeout = -1; + private static $requestTimeoutMS = -1; + private static $connectionTimeout = -1; + private static $connectionTimeoutMS = -1; public static function setKey($key) { self::$key = $key; @@ -35,6 +39,18 @@ public static function getClient() { if (!self::$client) { self::$client = new Client(self::$key, self::$appIdentifier); + if (self::$requestTimeout > -1) { + self::$client->setRequestTimeout(self::$requestTimeout); + } + if (self::$requestTimeoutMS > -1) { + self::$client->setRequestTimeoutMS(self::$requestTimeoutMS); + } + if (self::$connectionTimeout > -1) { + self::$client->setConnectionTimeout(self::$connectionTimeout); + } + if (self::$connectionTimeoutMS > -1) { + self::$client->setConnectionTimeoutMS(self::$connectionTimeoutMS); + } } return self::$client; @@ -43,6 +59,26 @@ public static function getClient() { public static function setClient($client) { self::$client = $client; } + + public static function setRequestTimeout($seconds) + { + self::$requestTimeout = $seconds; + } + + public static function setRequestTimeoutMS($milliseconds) + { + self::$requestTimeoutMS = $milliseconds; + } + + public static function setConnectionTimeout($seconds) + { + self::$connectionTimeout = $seconds; + } + + public static function setConnectionTimeoutMS($milliseconds) + { + self::$connectionTimeoutMS = $milliseconds; + } } function setKey($key) { @@ -61,6 +97,22 @@ function compressionCount() { return Tinify::getCompressionCount(); } +function setRequestTimeout($seconds) { + return Tinify::setRequestTimeout($seconds); +} + +function setRequestTimeoutMS($milliseconds) { + return Tinify::setRequestTimeoutMS($milliseconds); +} + +function setConnectionTimeout($seconds) { + return Tinify::setConnectionTimeout($seconds); +} + +function setConnectionTimeoutMS($milliseconds) { + return Tinify::setConnectionTimeoutMS($milliseconds); +} + function fromFile($path) { return Source::fromFile($path); } diff --git a/lib/Tinify/Client.php b/lib/Tinify/Client.php index f76f09a..efefda8 100644 --- a/lib/Tinify/Client.php +++ b/lib/Tinify/Client.php @@ -105,4 +105,20 @@ protected static function parseHeaders($headers) { } return $res; } + + function setRequestTimeout($seconds) { + $this->options[CURLOPT_TIMEOUT] = $seconds; + } + + function setRequestTimeoutMS($milliseconds) { + $this->options[CURLOPT_TIMEOUT_MS] = $milliseconds; + } + + function setConnectionTimeout($seconds) { + $this->options[CURLOPT_CONNECTTIMEOUT] = $seconds; + } + + function setConnectionTimeoutMS($milliseconds) { + $this->options[CURLOPT_CONNECTTIMEOUT_MS] = $milliseconds; + } } diff --git a/test/TinifyClientTest.php b/test/TinifyClientTest.php index b05f936..3a45638 100644 --- a/test/TinifyClientTest.php +++ b/test/TinifyClientTest.php @@ -173,4 +173,48 @@ public function testRequestWithBadCredentialsShouldThrowExceptionWithMessage() { $client = new Tinify\Client("key"); $client->request("get", "/"); } + + public function testRequestWithServerTimeoutErrorShouldThrowExceptionWithMessage() { + CurlMock::register("https://api.tinify.com/", array( + "error" => "Operation timed out after 1001 milliseconds with 0 bytes received", "errno" => 28 + )); + $this->setExpectedExceptionRegExp("Tinify\ConnectionException", + "/Error while connecting: Operation timed out after 1001 milliseconds with 0 bytes received \(#28\)/"); + $client = new Tinify\Client("key"); + $client->setRequestTimeout(1); + $client->request("get", "/"); + } + + public function testRequestWithServerTimeoutMSErrorShouldThrowExceptionWithMessage() { + CurlMock::register("https://api.tinify.com/", array( + "error" => "Operation timed out after 2 milliseconds with 0 bytes received", "errno" => 28 + )); + $this->setExpectedExceptionRegExp("Tinify\ConnectionException", + "/Error while connecting: Operation timed out after 2 milliseconds with 0 bytes received \(#28\)/"); + $client = new Tinify\Client("key"); + $client->setRequestTimeoutMS(1); + $client->request("get", "/"); + } + + public function testRequestWithServerConnectionTimeoutErrorShouldThrowExceptionWithMessage() { + CurlMock::register("https://api.tinify.com/", array( + "error" => "Connection timed out after 1001 milliseconds", "errno" => 28 + )); + $this->setExpectedExceptionRegExp("Tinify\ConnectionException", + "/Error while connecting: Connection timed out after 1001 milliseconds \(#28\)/"); + $client = new Tinify\Client("key"); + $client->setConnectionTimeout(1); + $client->request("get", "/"); + } + + public function testRequestWithServerConnectionTimeoutMSErrorShouldThrowExceptionWithMessage() { + CurlMock::register("https://api.tinify.com/", array( + "error" => "Connection timed out after 2 milliseconds", "errno" => 28 + )); + $this->setExpectedExceptionRegExp("Tinify\ConnectionException", + "/Error while connecting: Connection timed out after 2 milliseconds \(#28\)/"); + $client = new Tinify\Client("key"); + $client->setConnectionTimeoutMS(1); + $client->request("get", "/"); + } }