Skip to content

Commit

Permalink
Merge pull request #52 from diazwatson/29_UserConfig
Browse files Browse the repository at this point in the history
#29 User configures Adobe Stock integration
  • Loading branch information
sivaschenko authored Jun 11, 2019
2 parents bb54a5f + d2211d7 commit d661b32
Show file tree
Hide file tree
Showing 11 changed files with 291 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\AdobeStockAsset\Controller\Adminhtml\System\Config;

use Magento\AdobeStockAssetApi\Api\ClientInterface;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filter\StripTags;

class TestConnection extends Action
{
/**
* Authorization level of a basic admin session.
*
* @see _isAllowed()
*/
const ADMIN_RESOURCE = 'Magento_AdobeStockAsset::config';

/**
* @var JsonFactory
*/
private $resultJsonFactory;

/**
* @var StripTags
*/
private $tagFilter;

/**
* @var ClientInterface
*/
private $client;

/**
* TestConnection constructor.
*
* @param Context $context
* @param ClientInterface $client
* @param JsonFactory $resultJsonFactory
* @param StripTags $tagFilter
*/
public function __construct(
Context $context,
ClientInterface $client,
JsonFactory $resultJsonFactory,
StripTags $tagFilter
) {
parent::__construct($context);
$this->resultJsonFactory = $resultJsonFactory;
$this->tagFilter = $tagFilter;
$this->client = $client;
}

/**
* Check for connection to server
*
* @return ResultInterface|ResponseInterface
*/
public function execute()
{
$result = [
'success' => false,
'errorMessage' => '',
];

try {
if (!$this->client->testConnection()) {
throw new LocalizedException(__('Invalid API Key.'));
}
$result['success'] = true;
} catch (\Exception $e) {
$message = __('Invalid API Key.');
$result['errorMessage'] = $this->tagFilter->filter($message);
}

/** @var Json $resultJson */
$resultJson = $this->resultJsonFactory->create();

return $resultJson->setData($result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\AdobeStockAsset\Model\Block\Adminhtml\System\Config;

use Magento\Config\Block\System\Config\Form\Field;
use Magento\Framework\Data\Form\Element\AbstractElement;

/**
* Adobe Stock test connection block
*/
class TestConnection extends Field
{

/**
* Set template to itself
*
* @return TestConnection
*/
protected function _prepareLayout()
{
parent::_prepareLayout();
$this->setTemplate('Magento_AdobeStockAsset::system/config/testconnection.phtml');
return $this;
}

/**
* Get the button and scripts contents
*
* @param AbstractElement $element
* @return string
*/
protected function _getElementHtml(AbstractElement $element)
{
$originalData = $element->getOriginalData();
$this->addData(
[
'button_label' => __($originalData['button_label']),
'html_id' => $element->getHtmlId(),
'ajax_url' => $this->_urlBuilder->getUrl('adobe_stock/system_config/testconnection') . 'form_key/' . $this->getFormKey(),
]
);

return $this->_toHtml();
}
}
33 changes: 27 additions & 6 deletions AdobeStockAsset/Model/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use Magento\AdobeStockAssetApi\Api\Data\SearchRequestInterface;

/**
* DataProvider for cms ui.
* Client for communication to Adobe Stock API
*/
class Client implements ClientInterface
{
Expand All @@ -41,9 +41,9 @@ class Client implements ClientInterface

/**
* Client constructor.
* @param ConfigInterface $config
* @param ConfigInterface $config
* @param AssetInterfaceFactory $assetFactory
* @param SearchResultFactory $searchResultFactory
* @param SearchResultFactory $searchResultFactory
*/
public function __construct(
ConfigInterface $config,
Expand All @@ -60,7 +60,7 @@ public function __construct(
* @return SearchResultInterface
* @throws \AdobeStock\Api\Exception\StockApi
*/
public function search(SearchRequestInterface $request) : SearchResultInterface
public function search(SearchRequestInterface $request): SearchResultInterface
{
$searchParams = new SearchParameters();
$searchParams->setLimit($request->getSize());
Expand Down Expand Up @@ -95,13 +95,13 @@ public function search(SearchRequestInterface $request) : SearchResultInterface
return $this->searchResultFactory->create(
[
'items' => $items,
'count' => $response->getNbResults()
'count' => $response->getNbResults(),
]
);
}

/**
* @param array $filters
* @param array $filters
* @param SearchParameters $searchParams
* @throws \AdobeStock\Api\Exception\StockApi
*/
Expand Down Expand Up @@ -146,4 +146,25 @@ private function getClient()
$this->config->getTargetEnvironment()
);
}

/**
* @inheritDoc
*/
public function testConnection()
{
//TODO: should be refactored
$searchParams = new SearchParameters();
$searchRequest = new SearchFilesRequest();
$resultColumnArray = [];

$resultColumnArray[] = 'nb_results';

$searchRequest->setLocale('en_GB');
$searchRequest->setSearchParams($searchParams);
$searchRequest->setResultColumns($resultColumnArray);

$client = $this->getClient()->searchFilesInitialize($searchRequest, $this->getAccessToken());

return (bool) $client->getNextResponse()->nb_results;
}
}
22 changes: 22 additions & 0 deletions AdobeStockAsset/etc/acl.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
<acl>
<resources>
<resource id="Magento_Backend::admin">
<resource id="Magento_Backend::stores">
<resource id="Magento_Backend::stores_settings">
<resource id="Magento_Config::config">
<resource id="Magento_AdobeStockAsset::config" title="Adobe Stock Section" translate="title"/>
</resource>
</resource>
</resource>
</resource>
</resources>
</acl>
</config>
14 changes: 14 additions & 0 deletions AdobeStockAsset/etc/adminhtml/routes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="admin">
<route id="adobe_stock" frontName="adobe_stock">
<module name="Magento_AdobeStockAsset" before="Magento_Backend" />
</route>
</router>
</config>
37 changes: 37 additions & 0 deletions AdobeStockAsset/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<section id="system">
<group id="adobe_stock_integration" translate="label" type="text" sortOrder="1100" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Adobe Stock Integration</label>
<field id="enabled" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Enabled Adobe Stock</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<config_path>adobe_stock/integration/enabled</config_path>
</field>
<field id="api_key" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Api Key</label>
<config_path>adobe_stock/integration/api_key</config_path>
<validate>required-entry</validate>
<depends>
<field id="enabled">1</field>
</depends>
</field>
<field id="adobe_stock_test_connect_wizard" translate="button_label" sortOrder="30" showInDefault="1" showInWebsite="0" showInStore="0">
<label/>
<button_label>Test Connection</button_label>
<frontend_model>Magento\AdobeStockAsset\Model\Block\Adminhtml\System\Config\TestConnection</frontend_model>
<depends>
<field id="enabled">1</field>
</depends>
</field>
</group>
</section>
</system>
</config>
2 changes: 1 addition & 1 deletion AdobeStockAsset/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<adobe_stock>
<integration>
<enabled>1</enabled>
<api_key></api_key>
<api_key/>
<environment>PROD</environment>
<product_name>magento-adobe-stock-integration</product_name>
</integration>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

/** @var $block \Magento\AdobeStockImageAdminUi\Model\Block\Adminhtml\System\Config\TestConnection */
?>
<button class="scalable" type="button" id="<?= $block->escapeUrl($block->getData('html_id')) ?>" data-mage-init='{"testConnection":{
"url": "<?= /* @escapeNotVerified */ $block->getData('ajax_url') ?>",
"elementId": "<?= $block->escapeHtmlAttr($block->getData('html_id')) ?>",
"successText": "<?= $block->escapeHtmlAttr(__('Successful! Test again?')) ?>",
"fieldToCheck": "success",
"failedText": "<?= $block->escapeHtmlAttr(__('Connection failed! Test again?')) ?>", "validation": {}}}'>
<span>
<span>
<span id="<?= $block->escapeHtmlAttr($block->getData('html_id')) ?>_result">
<?= $block->escapeHtml($block->getData('button_label')) ?>
</span>
</span>
</span>
</button>
15 changes: 12 additions & 3 deletions AdobeStockAssetApi/Api/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,26 @@

namespace Magento\AdobeStockAssetApi\Api;

use Magento\AdobeStockAssetApi\Api\Data\SearchResultInterface;
use Magento\AdobeStockAssetApi\Api\Data\SearchRequestInterface;
use Magento\AdobeStockAssetApi\Api\Data\SearchResultInterface;

/**
* Interface
* Client for communication to Adobe Stock API
*/
interface ClientInterface
{
/**
* Perform a call to Adobe Stock API to perform assets search based on the search request
*
* @param SearchRequestInterface $request
* @return SearchResultInterface
*/
public function search(SearchRequestInterface $request) : SearchResultInterface;
public function search(SearchRequestInterface $request): SearchResultInterface;

/**
* Perform a basic request to Adobe Stock API to check network connection, API key, etc.
*
* @return bool
*/
public function testConnection();
}
16 changes: 16 additions & 0 deletions AdobeStockImage/etc/acl.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
<acl>
<resources>
<resource id="Magento_Backend::admin">
<resource id="Magento_AdobeStockImage::actions_view" title="View" translate="title" />
</resource>
</resources>
</acl>
</config>
1 change: 1 addition & 0 deletions AdobeStockImageApi/Api/GetImageListInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
interface GetImageListInterface
{
/**
* @api
* @param SearchCriteriaInterface $searchCriteria
* @return SearchResultsInterface
*/
Expand Down

0 comments on commit d661b32

Please sign in to comment.