public function apply() { if (TinifyReport::isRunning()) { return $this->displayError($this->l('Tinify already running.')); } $output = ''; $report = new TinifyReport(); // Tinify try { \Tinify\setKey(Configuration::get('TINIFY_API_KEY')); \Tinify\validate(); $report->beginTinify(); foreach (DirList::getDirList() as $dir) { if ($dir->enabled) { $this->tinifyDirectory($dir->path, $dir->recursive, $report); } } $output .= $this->displayConfirmation($this->l('Tinify finished successfully.')); } catch (\Tinify\AccountException $e) { $output .= $this->logError('Verify your account limit: ' . $e->getMessage(), $report); } catch (\Tinify\ClientException $e) { $output .= $this->logError('Check your source image and request options: ' . $e->getMessage(), $report); } catch (\Tinify\ServerException $e) { $output .= $this->logError('Temporary issue with the Tinify API: ' . $e->getMessage(), $report); } catch (\Tinify\ConnectionException $e) { $output .= $this->logError('A network connection error occurred: ' . $e->getMessage(), $report); } catch (Exception $e) { $output .= $this->logError('Unrelated exception: ' . $e->getMessage(), $report); } $report->endTinify(); if (isset($this->logger)) { $this->logger->logInfo('Report ' . TinifyReport::getLast(true)); } return $output; }
public function testValidateWithErrorShouldThrowException() { Tinify\setKey("invalid"); CurlMock::register("https://api.tinify.com/shrink", array("status" => 401, "body" => '{"error":"Unauthorized","message":"Credentials are invalid"}')); $this->setExpectedException("Tinify\\AccountException"); Tinify\validate(); }