-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add capability to offload images to optimole
- Loading branch information
1 parent
dda54f4
commit ee86311
Showing
22 changed files
with
1,325 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
includes: | ||
- ./vendor/szepeviktor/phpstan-wordpress/extension.neon | ||
parameters: | ||
level: 5 | ||
paths: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Optimole PHP SDK. | ||
* | ||
* (c) Optimole Team <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Optimole\Sdk\Exception; | ||
|
||
class BadResponseException extends RuntimeException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Optimole PHP SDK. | ||
* | ||
* (c) Optimole Team <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Optimole\Sdk\Exception; | ||
|
||
class DashboardApiException extends BadResponseException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Optimole PHP SDK. | ||
* | ||
* (c) Optimole Team <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Optimole\Sdk\Exception; | ||
|
||
class InvalidDashboardApiResponseException extends DashboardApiException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Optimole PHP SDK. | ||
* | ||
* (c) Optimole Team <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Optimole\Sdk\Exception; | ||
|
||
class InvalidUploadApiResponseException extends UploadApiException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Optimole PHP SDK. | ||
* | ||
* (c) Optimole Team <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Optimole\Sdk\Exception; | ||
|
||
class UploadApiException extends BadResponseException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Optimole PHP SDK. | ||
* | ||
* (c) Optimole Team <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Optimole\Sdk\Exception; | ||
|
||
class UploadFailedException extends RuntimeException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Optimole PHP SDK. | ||
* | ||
* (c) Optimole Team <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Optimole\Sdk\Exception; | ||
|
||
use Optimole\Sdk\ValueObject\OffloadUsage; | ||
|
||
class UploadLimitException extends UploadApiException | ||
{ | ||
/** | ||
* The offload service usage. | ||
*/ | ||
private OffloadUsage $usage; | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
public function __construct(OffloadUsage $usage, string $message = '', int $code = 0, ?\Throwable $previous = null) | ||
{ | ||
parent::__construct($message, $code, $previous); | ||
|
||
$this->usage = $usage; | ||
} | ||
|
||
/** | ||
* Get the offload service usage. | ||
*/ | ||
public function getUsage(): OffloadUsage | ||
{ | ||
return $this->usage; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Optimole PHP SDK. | ||
* | ||
* (c) Optimole Team <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Optimole\Sdk\Http; | ||
|
||
interface ClientInterface | ||
{ | ||
/** | ||
* Sends an HTTP request and returns the JSON decoded body. | ||
*/ | ||
public function sendRequest(string $method, string $url, $body = null, array $headers = []): ?array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Optimole PHP SDK. | ||
* | ||
* (c) Optimole Team <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Optimole\Sdk\Http; | ||
|
||
use GuzzleHttp\ClientInterface as GuzzleClientInterface; | ||
use GuzzleHttp\Exception\BadResponseException as GuzzleBadResponseException; | ||
use GuzzleHttp\Exception\GuzzleException; | ||
use GuzzleHttp\Psr7\Request; | ||
use Optimole\Sdk\Exception\BadResponseException; | ||
use Optimole\Sdk\Exception\InvalidArgumentException; | ||
use Optimole\Sdk\Exception\RuntimeException; | ||
use Optimole\Sdk\Optimole; | ||
|
||
class GuzzleClient implements ClientInterface | ||
{ | ||
/** | ||
* The Guzzle HTTP client. | ||
*/ | ||
private GuzzleClientInterface $client; | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
public function __construct(GuzzleClientInterface $client) | ||
{ | ||
$this->client = $client; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function sendRequest(string $method, string $url, $body = null, array $headers = []): ?array | ||
{ | ||
try { | ||
$response = $this->client->send($this->createRequest($method, $url, $body, $headers), ['verify' => false]); | ||
} catch (GuzzleBadResponseException $exception) { | ||
throw new BadResponseException($exception->getMessage(), $exception->getCode(), $exception); | ||
} catch (GuzzleException $exception) { | ||
throw new RuntimeException($exception->getMessage(), $exception->getCode(), $exception); | ||
} | ||
|
||
$body = (string) $response->getBody(); | ||
|
||
if (empty($body)) { | ||
return null; | ||
} | ||
|
||
$body = (array) json_decode($body, true); | ||
|
||
if (JSON_ERROR_NONE !== json_last_error()) { | ||
throw new BadResponseException(sprintf('Unable to decode JSON response: %s', json_last_error_msg())); | ||
} | ||
|
||
return $body; | ||
} | ||
|
||
/** | ||
* Create a request object. | ||
*/ | ||
private function createRequest(string $method, string $url, $body = null, array $headers = []): Request | ||
{ | ||
if (is_array($body)) { | ||
$body = json_encode($body); | ||
} | ||
|
||
if (null !== $body && !is_string($body)) { | ||
throw new InvalidArgumentException('"body" must be a string or an array'); | ||
} | ||
|
||
$headers = array_merge($headers, [ | ||
'User-Agent' => sprintf('optimole-sdk-php/%s', Optimole::VERSION), | ||
]); | ||
$method = strtolower($method); | ||
|
||
return new Request($method, $url, $headers, $body); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Optimole PHP SDK. | ||
* | ||
* (c) Optimole Team <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Optimole\Sdk\Http; | ||
|
||
use Optimole\Sdk\Exception\BadResponseException; | ||
use Optimole\Sdk\Exception\InvalidArgumentException; | ||
use Optimole\Sdk\Exception\RuntimeException; | ||
use Optimole\Sdk\Optimole; | ||
|
||
class WordPressClient implements ClientInterface | ||
{ | ||
/** | ||
* The WordPress HTTP client. | ||
*/ | ||
private \WP_Http $client; | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
public function __construct(\WP_Http $client) | ||
{ | ||
$this->client = $client; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function sendRequest(string $method, string $url, $body = null, array $headers = []): ?array | ||
{ | ||
if (is_array($body)) { | ||
$body = json_encode($body); | ||
} | ||
|
||
if (null !== $body && !is_string($body)) { | ||
throw new InvalidArgumentException('"body" must be a string or an array'); | ||
} | ||
|
||
$args = [ | ||
'method' => $method, | ||
'headers' => array_merge($headers, [ | ||
'User-Agent' => sprintf('optimole-sdk-php/%s', Optimole::VERSION), | ||
]), | ||
]; | ||
|
||
if (null !== $body) { | ||
$args['body'] = $body; | ||
} | ||
|
||
$response = $this->client->request($url, $args); | ||
|
||
if ($response instanceof \WP_Error) { | ||
throw new RuntimeException((string) $response->get_error_message(), (int) $response->get_error_code()); | ||
} elseif (200 !== $this->getResponseStatusCode($response)) { | ||
throw new BadResponseException(sprintf('Response status code: %s', $this->getResponseStatusCode($response))); | ||
} | ||
|
||
if (empty($response['body'])) { | ||
return null; | ||
} | ||
|
||
$body = (array) json_decode($response['body'], true); | ||
|
||
if (JSON_ERROR_NONE !== json_last_error()) { | ||
throw new BadResponseException(sprintf('Unable to decode JSON response: %s', json_last_error_msg())); | ||
} | ||
|
||
return $body; | ||
} | ||
|
||
/** | ||
* Get the status code from the given response. | ||
*/ | ||
private function getResponseStatusCode(array $response): ?int | ||
{ | ||
return $response['response']['code'] ?? null; | ||
} | ||
} |
Oops, something went wrong.