This repository has been archived by the owner on Mar 7, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Aaron Gerig
committed
May 29, 2019
1 parent
609ad62
commit 7fe34ad
Showing
4 changed files
with
193 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
/** | ||
* w-vision | ||
* | ||
* LICENSE | ||
* | ||
* For the full copyright and license information, please view the LICENSE.md | ||
* file that is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) 2018 w-vision AG (https://www.w-vision.ch) | ||
*/ | ||
|
||
namespace WvisionBundle\Api\ImprintApi; | ||
|
||
use GuzzleHttp\Client; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; | ||
|
||
class ImprintApi implements ImprintApiInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $baseUri = 'https://www.w-vision.ch'; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getData(array $addresses = []): ?array | ||
{ | ||
$client = new Client([ | ||
'base_uri' => $this->baseUri, | ||
'headers' => ['ACCEPT' => 'application/json'], | ||
]); | ||
|
||
$uri = '/api/internal/imprint'; | ||
|
||
if (!empty($addresses)) { | ||
$uri .= sprintf('/%s', implode('/', $addresses)); | ||
} | ||
|
||
$response = $client->get($uri); | ||
|
||
return $this->parseResponse($response); | ||
} | ||
|
||
/** | ||
* Parses the API JSON response | ||
* | ||
* @param ResponseInterface $response | ||
* @return array|null | ||
*/ | ||
private function parseResponse(ResponseInterface $response): ?array | ||
{ | ||
if ($response->getStatusCode() < 300) { | ||
$result = (string) $response->getBody(); | ||
$data = json_decode($result, true); | ||
|
||
if (json_last_error() !== JSON_ERROR_NONE) { | ||
throw new BadRequestHttpException('Invalid json body: ' . json_last_error_msg()); | ||
} | ||
|
||
return is_array($data) ? $data : []; | ||
} | ||
|
||
return null; | ||
} | ||
} |
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,84 @@ | ||
<?php | ||
/** | ||
* w-vision | ||
* | ||
* LICENSE | ||
* | ||
* For the full copyright and license information, please view the LICENSE.md | ||
* file that is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) 2018 w-vision AG (https://www.w-vision.ch) | ||
*/ | ||
|
||
namespace WvisionBundle\Api\ImprintApi; | ||
|
||
use Pimcore\Cache\Core\CoreHandlerInterface; | ||
|
||
class ImprintApiCached implements ImprintApiInterface | ||
{ | ||
/** | ||
* Cache key for all API calls. | ||
*/ | ||
private const CACHE_KEY = 'wvision_imprint'; | ||
|
||
/** | ||
* @var CoreHandlerInterface | ||
*/ | ||
private $cacheHelper; | ||
|
||
/** | ||
* The default cache lifetime is one week. | ||
* | ||
* @var int | ||
*/ | ||
private $cacheLifetime = 604800; | ||
|
||
/** | ||
* @var ImprintApiInterface | ||
*/ | ||
private $decorated; | ||
|
||
/** | ||
* @param CoreHandlerInterface $cacheHelper | ||
* @param ImprintApiInterface $decorated | ||
*/ | ||
public function __construct(CoreHandlerInterface $cacheHelper, ImprintApiInterface $decorated) | ||
{ | ||
$this->cacheHelper = $cacheHelper; | ||
$this->decorated = $decorated; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getData(array $addresses = []): ?array | ||
{ | ||
$cacheKey = $this->getCacheKey($addresses); | ||
|
||
if (!$result = $this->cacheHelper->load($cacheKey)) { | ||
$result = $this->decorated->getData($addresses); | ||
|
||
$this->cacheHelper->save( | ||
$cacheKey, | ||
$result, | ||
[static::CACHE_KEY], | ||
$this->cacheLifetime | ||
); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* Assembles a unique key for caching | ||
* | ||
* @param array $addresses | ||
* @return string | ||
*/ | ||
private function getCacheKey(array $addresses = []): string | ||
{ | ||
return empty($addresses) | ||
? static::CACHE_KEY | ||
: sprintf('%s_%s', static::CACHE_KEY, implode('_', $addresses)); | ||
} | ||
} |
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,29 @@ | ||
<?php | ||
/** | ||
* w-vision | ||
* | ||
* LICENSE | ||
* | ||
* For the full copyright and license information, please view the LICENSE.md | ||
* file that is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) 2018 w-vision AG (https://www.w-vision.ch) | ||
*/ | ||
|
||
namespace WvisionBundle\Api\ImprintApi; | ||
|
||
use GuzzleHttp\Exception\GuzzleException; | ||
|
||
interface ImprintApiInterface | ||
{ | ||
/** | ||
* Returns the default imprint address and any other additionally provided addresses. | ||
* | ||
* @param array $addresses | ||
* | ||
* @return array|null | ||
* | ||
* @throws GuzzleException | ||
*/ | ||
public function getData(array $addresses = []): ?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