Skip to content

Commit

Permalink
magento#341: Extracted AdobeIms and AdobeImsApi modules
Browse files Browse the repository at this point in the history
  • Loading branch information
sivaschenko committed Aug 13, 2019
1 parent 9e062ad commit 90b4445
Show file tree
Hide file tree
Showing 49 changed files with 715 additions and 342 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ before_script:
env:
matrix:
- TEST_CATEGORY=static
- TEST_CATEGORY=unit_ims
- TEST_CATEGORY=unit_client
- TEST_CATEGORY=unit_image
- TEST_CATEGORY=unit_ui
- TEST_CATEGORY=unit_image_ui
matrix:
fast_finish: true
script:
- if [ $TEST_CATEGORY == 'unit_ims' ]; then vendor/bin/phpunit --configuration dev/tests/unit/phpunit.xml.dist vendor/magento/module-adobe-stock-ims/Test/Unit; fi
- if [ $TEST_CATEGORY == 'unit_client' ]; then vendor/bin/phpunit --configuration dev/tests/unit/phpunit.xml.dist vendor/magento/module-adobe-stock-client/Test/Unit; fi
- if [ $TEST_CATEGORY == 'unit_ui' ]; then vendor/bin/phpunit --configuration dev/tests/unit/phpunit.xml.dist vendor/magento/module-adobe-stock-admin-ui/Test/Unit; fi
- if [ $TEST_CATEGORY == 'unit_image' ]; then vendor/bin/phpunit --configuration dev/tests/unit/phpunit.xml.dist vendor/magento/module-adobe-stock-image/Test/Unit; fi
Expand Down
20 changes: 10 additions & 10 deletions AdobeIms/Controller/Adminhtml/OAuth/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
use DateInterval;
use DateTime;
use Exception;
use Magento\AdobeStockAssetApi\Api\Data\UserProfileInterface;
use Magento\AdobeStockAssetApi\Api\Data\UserProfileInterfaceFactory;
use Magento\AdobeStockAssetApi\Api\UserProfileRepositoryInterface;
use Magento\AdobeStockClientApi\Api\ClientInterface;
use Magento\AdobeImsApi\Api\Data\UserProfileInterface;
use Magento\AdobeImsApi\Api\Data\UserProfileInterfaceFactory;
use Magento\AdobeImsApi\Api\GetTokenInterface;
use Magento\AdobeImsApi\Api\UserProfileRepositoryInterface;
use Magento\Backend\App\Action;
use Magento\Framework\Controller\Result\Raw;
use Magento\Framework\Controller\ResultFactory;
Expand Down Expand Up @@ -60,9 +60,9 @@ class Callback extends Action
private $userProfileFactory;

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

/**
* @var LoggerInterface
Expand All @@ -74,21 +74,21 @@ class Callback extends Action
* @param Action\Context $context
* @param UserProfileRepositoryInterface $userProfileRepository
* @param UserProfileInterfaceFactory $userProfileFactory
* @param ClientInterface $client
* @param GetTokenInterface $getToken
* @param LoggerInterface $logger
*/
public function __construct(
Action\Context $context,
UserProfileRepositoryInterface $userProfileRepository,
UserProfileInterfaceFactory $userProfileFactory,
ClientInterface $client,
GetTokenInterface $getToken,
LoggerInterface $logger
) {
parent::__construct($context);

$this->userProfileRepository = $userProfileRepository;
$this->userProfileFactory = $userProfileFactory;
$this->client = $client;
$this->getToken = $getToken;
$this->logger = $logger;
}

Expand All @@ -98,7 +98,7 @@ public function __construct(
public function execute() : \Magento\Framework\Controller\ResultInterface
{
try {
$tokenResponse = $this->client->getToken(
$tokenResponse = $this->getToken->execute(
(string)$this->getRequest()->getParam('code')
);

Expand Down
107 changes: 107 additions & 0 deletions AdobeIms/Model/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\AdobeIms\Model;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\UrlInterface;
use Magento\Config\Model\Config\Backend\Admin\Custom;
use Magento\AdobeImsApi\Api\Data\ConfigInterface;

/**
* Class Config
*/
class Config implements ConfigInterface
{
private const XML_PATH_API_KEY = 'adobe_stock/integration/api_key';
private const XML_PATH_PRIVATE_KEY = 'adobe_stock/integration/private_key';
private const XML_PATH_TOKEN_URL = 'adobe_stock/integration/token_url';
private const XML_PATH_AUTH_URL_PATTERN = 'adobe_stock/integration/auth_url_pattern';

/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* @var UrlInterface
*/
private $url;

/**
* Config constructor.
* @param ScopeConfigInterface $scopeConfig
* @param UrlInterface $url
*/
public function __construct(
ScopeConfigInterface $scopeConfig,
UrlInterface $url
) {
$this->scopeConfig = $scopeConfig;
$this->url = $url;
}

/**
* Retrieve integration API key (Client ID)
*
* @return string|null
*/
public function getApiKey():? string
{
return $this->scopeConfig->getValue(self::XML_PATH_API_KEY);
}

/**
* @inheritdoc
*/
public function getPrivateKey(): string
{
return (string)$this->scopeConfig->getValue(self::XML_PATH_PRIVATE_KEY);
}

/**
* @inheritdoc
*/
public function getTokenUrl(): string
{
return $this->scopeConfig->getValue(self::XML_PATH_TOKEN_URL);
}

/**
* @inheritdoc
*/
public function getAuthUrl(): string
{
return str_replace(
['#{client_id}', '#{redirect_uri}', '#{locale}'],
[$this->getApiKey(), $this->getCallBackUrl(), $this->getLocale()],
$this->scopeConfig->getValue(self::XML_PATH_AUTH_URL_PATTERN)
);
}

/**
* Retrieve Callback URL
*
* @return string
*/
public function getCallBackUrl(): string
{
return $this->url->getUrl('adobe_ims/oauth/callback');
}


/**
* Retrieve token URL
*
* @return string
*/
private function getLocale(): string
{
return $this->scopeConfig->getValue(Custom::XML_PATH_GENERAL_LOCALE_CODE);
}
}
71 changes: 0 additions & 71 deletions AdobeIms/Model/Config/ImsToken.php

This file was deleted.

96 changes: 96 additions & 0 deletions AdobeIms/Model/GetToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\AdobeIms\Model;

use Magento\AdobeImsApi\Api\GetTokenInterface;
use Magento\Framework\Exception\AuthorizationException;
use Magento\Framework\HTTP\Client\CurlFactory;
use Magento\AdobeImsApi\Api\Data\ConfigInterface;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\AdobeImsApi\Api\Data\TokenResponseInterface;
use Magento\AdobeImsApi\Api\Data\TokenResponseInterfaceFactory;

/**
* Class Config
*/
class GetToken implements GetTokenInterface
{
/**
* @var ConfigInterface
*/
private $config;

/**
* @var CurlFactory
*/
private $curlFactory;

/**
* @var Json
*/
private $json;

/**
* @var TokenResponseInterfaceFactory
*/
private $tokenResponseFactory;

/**
* GetToken constructor.
* @param ConfigInterface $config
* @param CurlFactory $curlFactory
* @param Json $json
* @param TokenResponseInterfaceFactory $tokenResponseFactory
*/
public function __construct(
ConfigInterface $config,
CurlFactory $curlFactory,
Json $json,
TokenResponseInterfaceFactory $tokenResponseFactory
) {
$this->config = $config;
$this->curlFactory = $curlFactory;
$this->json = $json;
$this->tokenResponseFactory = $tokenResponseFactory;
}

/**
* @inheritdoc
*/
public function execute(string $code): TokenResponseInterface
{
$curl = $this->curlFactory->create();

$curl->addHeader('Content-Type', 'application/x-www-form-urlencoded');
$curl->addHeader('cache-control', 'no-cache');

$curl->post(
$this->config->getTokenUrl(),
[
'client_id' => $this->config->getApiKey(),
'client_secret' => $this->config->getPrivateKey(),
'code' => $code,
'grant_type' => 'authorization_code'
]
);

$tokenResponse = $this->json->unserialize($curl->getBody());
/** @var TokenResponse $tokenResponse */
$tokenResponse = $this->tokenResponseFactory->create()
->addData(is_array($tokenResponse) ? $tokenResponse : ['error' => __('The response is empty.')]);

if (empty($tokenResponse->getAccessToken()) || empty($tokenResponse->getRefreshToken())) {
throw new AuthorizationException(
__('Authentication is failing. Error code: %1', $tokenResponse->getError())
);
}

return $tokenResponse;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@

declare(strict_types=1);

namespace Magento\AdobeStockClient\Model\OAuth;
namespace Magento\AdobeIms\Model\OAuth;

use Magento\AdobeImsApi\Api\Data\TokenResponseInterface;
use Magento\Framework\DataObject;

/**
* Class TokenResponse
*/
class TokenResponse extends DataObject
class TokenResponse extends DataObject implements TokenResponseInterface
{
/**
* Get access token
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

declare(strict_types=1);

namespace Magento\AdobeStockAsset\Model\ResourceModel;
namespace Magento\AdobeIms\Model\ResourceModel;

use Magento\Framework\Model\ResourceModel\Db\AbstractDb;

Expand All @@ -20,6 +20,6 @@ class UserProfile extends AbstractDb
*/
protected function _construct()
{
$this->_init('adobe_stock_user_profile', 'id');
$this->_init('adobe_user_profile', 'id');
}
}
Loading

0 comments on commit 90b4445

Please sign in to comment.