Skip to content
This repository has been archived by the owner on Mar 7, 2024. It is now read-only.

Commit

Permalink
implement imprint API
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Gerig committed May 29, 2019
1 parent 609ad62 commit 7fe34ad
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/Api/ImprintApi/ImprintApi.php
@@ -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;
}
}
84 changes: 84 additions & 0 deletions src/Api/ImprintApi/ImprintApiCached.php
@@ -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));
}
}
29 changes: 29 additions & 0 deletions src/Api/ImprintApi/ImprintApiInterface.php
@@ -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;
}
12 changes: 12 additions & 0 deletions src/Resources/config/services.yml
Expand Up @@ -7,3 +7,15 @@ services:
class: WvisionBundle\Configuration\Configuration
arguments:
- '@pimcore.extension.bundle_manager'

# API
WvisionBundle\Api\ImprintApi\ImprintApiInterface:
alias: WvisionBundle\Api\ImprintApi\ImprintApi

WvisionBundle\Api\ImprintApi\ImprintApi:

WvisionBundle\Api\ImprintApi\ImprintApiCached:
decorates: 'WvisionBundle\Api\ImprintApi\ImprintApiInterface'
arguments:
- '@Pimcore\Cache\Core\CoreHandlerInterface'
- '@WvisionBundle\Api\ImprintApi\ImprintApiCached.inner'

0 comments on commit 7fe34ad

Please sign in to comment.