forked from magento/adobe-stock-integration
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
magento#341: Extracted AdobeIms and AdobeImsApi modules
- Loading branch information
1 parent
9e062ad
commit 90b4445
Showing
49 changed files
with
715 additions
and
342 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
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
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,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); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,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; | ||
} | ||
} |
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
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
Oops, something went wrong.