Skip to content

Commit

Permalink
Implements the OptionService.
Browse files Browse the repository at this point in the history
  • Loading branch information
nilov committed Feb 6, 2017
1 parent d4b6f66 commit de10092
Show file tree
Hide file tree
Showing 3 changed files with 260 additions and 42 deletions.
64 changes: 64 additions & 0 deletions src/Service/AbstractContentService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/*
* This file is part of the GLAVWEB.cms Content Block package.
*
* (c) Andrey Nilov <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Glavweb\CmsContentBlock\Service;

use Psr\Http\Message\ResponseInterface;
use Glavweb\CmsRestClient\CmsRestClient;

/**
* Class AbstractContentService
*
* @package Glavweb\CmsContentBlock
* @author Andrey Nilov <[email protected]>
*/
abstract class AbstractContentService
{
/**
* Http status constants
*/
const HTTP_OK = 200;
const HTTP_CREATED = 201;
const HTTP_PARTIAL_CONTENT = 206;

/**
* @var CmsRestClient
*/
protected $restClient;

/**
* ContentBlockService constructor.
*
* @param CmsRestClient $restClient
*/
public function __construct(CmsRestClient $restClient)
{
$this->restClient = $restClient;
}

/**
* @param ResponseInterface $response
* @return int
* @throws \Exception
*/
protected function getMaxResultByResponse(ResponseInterface $response)
{
$contentRange = $response->getHeader('Content-Range');

if (!isset($contentRange[0])) {
throw new \Exception('Header "Content-Range" is not returned from API.');
}

$maxResult = explode('/', $contentRange[0])[1];

return (int)$maxResult;
}
}
43 changes: 1 addition & 42 deletions src/Service/ContentBlockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Glavweb\CmsContentBlock\Service;

use Psr\Http\Message\ResponseInterface;
use Glavweb\CmsRestClient\CmsRestClient;

/**
Expand All @@ -20,35 +19,13 @@
* @package Glavweb\CmsContentBlock
* @author Andrey Nilov <[email protected]>
*/
class ContentBlockService
class ContentBlockService extends AbstractContentService
{
/**
* Http status constants
*/
const HTTP_OK = 200;
const HTTP_CREATED = 201;
const HTTP_PARTIAL_CONTENT = 206;

/**
* @var array
*/
private static $contentBlocksCache = null;

/**
* @var CmsRestClient
*/
private $restClient;

/**
* ContentBlockService constructor.
*
* @param CmsRestClient $restClient
*/
public function __construct(CmsRestClient $restClient)
{
$this->restClient = $restClient;
}

/**
* @param string $category
* @param string $blockName
Expand Down Expand Up @@ -221,24 +198,6 @@ private function createContentBlock($category, $blockName, $defaultContent)
self::$contentBlocksCache[$category][$blockName] = $contentBlockItem;
}

/**
* @param ResponseInterface $response
* @return int
* @throws \Exception
*/
private function getMaxResultByResponse(ResponseInterface $response)
{
$contentRange = $response->getHeader('Content-Range');

if (!isset($contentRange[0])) {
throw new \Exception('Header "Content-Range" is not returned from API.');
}

$maxResult = explode('/', $contentRange[0])[1];

return (int)$maxResult;
}

/**
* @param string $location
* @return array
Expand Down
195 changes: 195 additions & 0 deletions src/Service/OptionService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
<?php

/*
* This file is part of the GLAVWEB.cms Content Block package.
*
* (c) Andrey Nilov <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Glavweb\CmsContentBlock\Service;

use Psr\Http\Message\ResponseInterface;
use Glavweb\CmsRestClient\CmsRestClient;

/**
* Class OptionService
*
* @package Glavweb\CmsContentBlock
* @author Andrey Nilov <[email protected]>
*/
class OptionService extends AbstractContentService
{
/**
* @var array
*/
private static $optionsCache = null;

/**
* @param string $category
* @param string $optionName
* @param string $default
* @return string
*/
public function getOption($category, $optionName, $default = null)
{
$options = $this->getOptionListByCategory($category);
$option = isset($options[$optionName]) ? $options[$optionName] : null;

// Create new option
if ($option === null) {
$default = (string)$default;
$this->createOption($category, $optionName, $default);

$optionValue = $default;

} else {
$optionValue = $option['value'];
}

return $optionValue;
}

/**
* Editable option
*
* @param string $category
* @param string $name
* @return string
*/
public function editable($category, $name)
{
$attributes['data-option'] = 'true';
$attributes['data-option-category'] = $category;
$attributes['data-option-name'] = $name;

$attrParts = array();
foreach ($attributes as $attrName => $attrValue) {
$attrParts[] = sprintf('%s="%s"', $attrName, $attrValue);
}

return ' ' . implode(' ', $attrParts);
}

/**
* @return array
* @throws \Exception
*/
public function getOptions()
{
if (self::$optionsCache === null) {
self::$optionsCache = $this->doGetOptions();
}

return self::$optionsCache;
}

/**
* @param array $options
* @param int $offset
* @return array
* @throws \Exception
*/
private function doGetOptions($options = [], $offset = 0)
{
$limit = 1000;

$response = $this->restClient->get('options', [
'query' => [
'_offset' => $offset,
'_limit' => $limit
]
]);
$responseStatusCode = $response->getStatusCode();

if ($responseStatusCode != self::HTTP_OK && $responseStatusCode != self::HTTP_PARTIAL_CONTENT) {
throw new \Exception('Can not get options.');
}

$listArray = json_decode($response->getBody(), true);

foreach ($listArray as $item) {
$category = $item['category'];
$name = $item['name'];

$options[$category][$name] = $item;
}

$needAdditionalLoad =
$responseStatusCode == self::HTTP_PARTIAL_CONTENT &&
!empty($listArray) &&
$this->getMaxResultByResponse($response) > $offset + $limit
;

if ($needAdditionalLoad) {
$options = $this->doGetOptions($options, $offset + $limit);
}

return $options;
}

/**
* @param string $category
* @return array
* @throws \Exception
*/
public function getOptionListByCategory($category)
{
$options = $this->getOptions();

return isset($options[$category]) ? $options[$category] : [];
}

/**
* @param string $category
* @param string $name
* @param string $value
* @return string
* @throws \Exception
*/
private function createOption($category, $name, $value)
{
$response = $this->restClient->post('options', [
'form_params' => [
'category' => $category,
'name' => $name,
'value' => $value
]
], true);

if (!$response->getStatusCode() == self::HTTP_CREATED) {
throw new \Exception('Can not save option.');
}

$locationResponse = $response->getHeader('location');
if (!isset($locationResponse[0])) {
throw new \Exception('Location is not returned from API.');
}

// Set option in cache
$location = $locationResponse[0];
$option = $this->getOptionByLocation($location);

self::$optionsCache[$category][$name] = $option;
}

/**
* @param string $location
* @return array
* @throws \Exception
*/
private function getOptionByLocation($location)
{
$response = $this->restClient->get($location);

if (!$response->getStatusCode() == self::HTTP_OK) {
throw new \Exception('Can not get option by "' . $location . '".');
}

$optionData = json_decode($response->getBody(), true);

return $optionData;
}
}

0 comments on commit de10092

Please sign in to comment.