Skip to content

Commit

Permalink
Merge pull request #1310 from magento-firedrakes/MAGETWO-69606
Browse files Browse the repository at this point in the history
Fixed issue:
- MAGETWO-69606 Conflict between BaseUrl Change and Re-Sign Up functionality in the Analytics module
  • Loading branch information
slavvka authored Jul 12, 2017
2 parents 531c257 + e05e6ec commit 20e58ef
Show file tree
Hide file tree
Showing 22 changed files with 1,062 additions and 261 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace Magento\Analytics\Controller\Adminhtml\Reports;

use Magento\Analytics\Model\Exception\State\SubscriptionUpdateException;
use Magento\Analytics\Model\ReportUrlProvider;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
Expand Down Expand Up @@ -55,6 +56,9 @@ public function execute()
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
try {
$resultRedirect->setUrl($this->reportUrlProvider->getUrl());
} catch (SubscriptionUpdateException $e) {
$this->getMessageManager()->addNoticeMessage($e->getMessage());
$resultRedirect->setPath('adminhtml');
} catch (LocalizedException $e) {
$this->getMessageManager()->addExceptionMessage($e, $e->getMessage());
$resultRedirect->setPath('adminhtml');
Expand Down
41 changes: 28 additions & 13 deletions app/code/Magento/Analytics/Cron/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
*/
namespace Magento\Analytics\Cron;

use Magento\Analytics\Model\AnalyticsToken;
use Magento\Analytics\Model\Config\Backend\Baseurl\SubscriptionUpdateHandler;
use Magento\Analytics\Model\Connector;
use Magento\Analytics\Model\Plugin\BaseUrlConfigPlugin;
use Magento\Framework\FlagManager;
use Magento\Framework\App\Config\ReinitableConfigInterface;
use Magento\Framework\App\Config\Storage\WriterInterface;

/**
* Class Update
* Executes by cron schedule in case base url was changed
*/
class Update
Expand All @@ -28,8 +28,6 @@ class Update
private $configWriter;

/**
* Reinitable Config Model.
*
* @var ReinitableConfigInterface
*/
private $reinitableConfig;
Expand All @@ -40,22 +38,29 @@ class Update
private $flagManager;

/**
* Update constructor.
* @var AnalyticsToken
*/
private $analyticsToken;

/**
* @param Connector $connector
* @param WriterInterface $configWriter
* @param ReinitableConfigInterface $reinitableConfig
* @param FlagManager $flagManager
* @param AnalyticsToken $analyticsToken
*/
public function __construct(
Connector $connector,
WriterInterface $configWriter,
ReinitableConfigInterface $reinitableConfig,
FlagManager $flagManager
FlagManager $flagManager,
AnalyticsToken $analyticsToken
) {
$this->connector = $connector;
$this->configWriter = $configWriter;
$this->reinitableConfig = $reinitableConfig;
$this->flagManager = $flagManager;
$this->analyticsToken = $analyticsToken;
}

/**
Expand All @@ -65,13 +70,23 @@ public function __construct(
*/
public function execute()
{
$updateResult = $this->connector->execute('update');
if ($updateResult === false) {
return false;
$result = false;
$attemptsCount = $this->flagManager
->getFlagData(SubscriptionUpdateHandler::SUBSCRIPTION_UPDATE_REVERSE_COUNTER_FLAG_CODE);

if ($attemptsCount) {
$attemptsCount -= 1;
$result = $this->connector->execute('update');
}

if ($result || ($attemptsCount <= 0) || (!$this->analyticsToken->isTokenExist())) {
$this->flagManager
->deleteFlag(SubscriptionUpdateHandler::SUBSCRIPTION_UPDATE_REVERSE_COUNTER_FLAG_CODE);
$this->flagManager->deleteFlag(SubscriptionUpdateHandler::PREVIOUS_BASE_URL_FLAG_CODE);
$this->configWriter->delete(SubscriptionUpdateHandler::UPDATE_CRON_STRING_PATH);
$this->reinitableConfig->reinit();
}
$this->configWriter->delete(BaseUrlConfigPlugin::UPDATE_CRON_STRING_PATH);
$this->flagManager->deleteFlag(BaseUrlConfigPlugin::OLD_BASE_URL_FLAG_CODE);
$this->reinitableConfig->reinit();
return true;

return $result;
}
}
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.
*/

namespace Magento\Analytics\Model\Config\Backend\Baseurl;

use Magento\Analytics\Model\AnalyticsToken;
use Magento\Framework\App\Config\ReinitableConfigInterface;
use Magento\Framework\App\Config\Storage\WriterInterface;
use Magento\Framework\FlagManager;

/**
* Class for processing of change of Base URL.
*/
class SubscriptionUpdateHandler
{
/**
* Flag code for a reserve counter to update subscription.
*/
const SUBSCRIPTION_UPDATE_REVERSE_COUNTER_FLAG_CODE = 'analytics_link_subscription_update_reverse_counter';

/**
* Config path for schedule setting of update handler.
*/
const UPDATE_CRON_STRING_PATH = "crontab/default/jobs/analytics_update/schedule/cron_expr";

/**
* Flag code for the previous Base URL.
*/
const PREVIOUS_BASE_URL_FLAG_CODE = 'analytics_previous_base_url';

/**
* Max value for a reserve counter to update subscription.
*
* @var int
*/
private $attemptsInitValue = 48;

/**
* @var WriterInterface
*/
private $configWriter;

/**
* Cron expression for a update handler.
*
* @var string
*/
private $cronExpression = '0 * * * *';

/**
* @var FlagManager
*/
private $flagManager;

/**
* @var ReinitableConfigInterface
*/
private $reinitableConfig;

/**
* @var AnalyticsToken
*/
private $analyticsToken;

/**
* @param AnalyticsToken $analyticsToken
* @param FlagManager $flagManager
* @param ReinitableConfigInterface $reinitableConfig
* @param WriterInterface $configWriter
*/
public function __construct(
AnalyticsToken $analyticsToken,
FlagManager $flagManager,
ReinitableConfigInterface $reinitableConfig,
WriterInterface $configWriter
) {
$this->analyticsToken = $analyticsToken;
$this->flagManager = $flagManager;
$this->reinitableConfig = $reinitableConfig;
$this->configWriter = $configWriter;
}

/**
* Activate process of subscription update handling.
*
* @param string $url
* @return bool
*/
public function processUrlUpdate(string $url)
{
if ($this->analyticsToken->isTokenExist()) {
if (!$this->flagManager->getFlagData(self::PREVIOUS_BASE_URL_FLAG_CODE)) {
$this->flagManager->saveFlag(self::PREVIOUS_BASE_URL_FLAG_CODE, $url);
}

$this->flagManager
->saveFlag(self::SUBSCRIPTION_UPDATE_REVERSE_COUNTER_FLAG_CODE, $this->attemptsInitValue);
$this->configWriter->save(self::UPDATE_CRON_STRING_PATH, $this->cronExpression);
$this->reinitableConfig->reinit();
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
namespace Magento\Analytics\Model\Connector;

use Magento\Analytics\Model\AnalyticsToken;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\HTTP\ZendClient;
use Magento\Config\Model\Config;
use Psr\Log\LoggerInterface;
use Magento\Store\Model\Store;
use Magento\Analytics\Model\Connector\Http\ResponseResolver;
Expand All @@ -33,7 +33,7 @@ class NotifyDataChangedCommand implements CommandInterface
private $httpClient;

/**
* @var Config
* @var ScopeConfigInterface
*/
private $config;

Expand All @@ -51,14 +51,14 @@ class NotifyDataChangedCommand implements CommandInterface
* NotifyDataChangedCommand constructor.
* @param AnalyticsToken $analyticsToken
* @param Http\ClientInterface $httpClient
* @param Config $config
* @param ScopeConfigInterface $config
* @param ResponseResolver $responseResolver
* @param LoggerInterface $logger
*/
public function __construct(
AnalyticsToken $analyticsToken,
Http\ClientInterface $httpClient,
Config $config,
ScopeConfigInterface $config,
ResponseResolver $responseResolver,
LoggerInterface $logger
) {
Expand All @@ -80,16 +80,14 @@ public function execute()
if ($this->analyticsToken->isTokenExist()) {
$response = $this->httpClient->request(
ZendClient::POST,
$this->config->getConfigDataValue($this->notifyDataChangedUrlPath),
$this->config->getValue($this->notifyDataChangedUrlPath),
[
"access-token" => $this->analyticsToken->getToken(),
"url" => $this->config->getConfigDataValue(
Store::XML_PATH_SECURE_BASE_URL
),
"url" => $this->config->getValue(Store::XML_PATH_SECURE_BASE_URL),
]
);
$result = $this->responseResolver->getResult($response);
}
return $result;
return (bool)$result;
}
}
16 changes: 7 additions & 9 deletions app/code/Magento/Analytics/Model/Connector/SignUpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Magento\Analytics\Model\AnalyticsToken;
use Magento\Analytics\Model\Connector\Http\ResponseResolver;
use Magento\Analytics\Model\IntegrationManager;
use Magento\Config\Model\Config;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Psr\Log\LoggerInterface;
use Magento\Framework\HTTP\ZendClient;
use Magento\Store\Model\Store;
Expand Down Expand Up @@ -36,7 +36,7 @@ class SignUpCommand implements CommandInterface
private $integrationManager;

/**
* @var Config
* @var ScopeConfigInterface
*/
private $config;

Expand All @@ -60,15 +60,15 @@ class SignUpCommand implements CommandInterface
*
* @param AnalyticsToken $analyticsToken
* @param IntegrationManager $integrationManager
* @param Config $config
* @param ScopeConfigInterface $config
* @param Http\ClientInterface $httpClient
* @param LoggerInterface $logger
* @param ResponseResolver $responseResolver
*/
public function __construct(
AnalyticsToken $analyticsToken,
IntegrationManager $integrationManager,
Config $config,
ScopeConfigInterface $config,
Http\ClientInterface $httpClient,
LoggerInterface $logger,
ResponseResolver $responseResolver
Expand Down Expand Up @@ -101,12 +101,10 @@ public function execute()
$this->integrationManager->activateIntegration();
$response = $this->httpClient->request(
ZendClient::POST,
$this->config->getConfigDataValue($this->signUpUrlPath),
$this->config->getValue($this->signUpUrlPath),
[
"token" => $integrationToken->getData('token'),
"url" => $this->config->getConfigDataValue(
Store::XML_PATH_SECURE_BASE_URL
)
"url" => $this->config->getValue(Store::XML_PATH_SECURE_BASE_URL),
]
);

Expand All @@ -121,6 +119,6 @@ public function execute()
}
}

return $result;
return (bool)$result;
}
}
21 changes: 10 additions & 11 deletions app/code/Magento/Analytics/Model/Connector/UpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
namespace Magento\Analytics\Model\Connector;

use Magento\Analytics\Model\AnalyticsToken;
use Magento\Analytics\Model\Config\Backend\Baseurl\SubscriptionUpdateHandler;
use Magento\Analytics\Model\Connector\Http\ResponseResolver;
use Magento\Analytics\Model\Plugin\BaseUrlConfigPlugin;
use Magento\Config\Model\Config;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\FlagManager;
use Magento\Framework\HTTP\ZendClient;
use Magento\Store\Model\Store;
Expand Down Expand Up @@ -36,7 +36,7 @@ class UpdateCommand implements CommandInterface
private $httpClient;

/**
* @var Config
* @var ScopeConfigInterface
*/
private $config;

Expand All @@ -58,15 +58,15 @@ class UpdateCommand implements CommandInterface
/**
* @param AnalyticsToken $analyticsToken
* @param Http\ClientInterface $httpClient
* @param Config $config
* @param ScopeConfigInterface $config
* @param LoggerInterface $logger
* @param FlagManager $flagManager
* @param ResponseResolver $responseResolver
*/
public function __construct(
AnalyticsToken $analyticsToken,
Http\ClientInterface $httpClient,
Config $config,
ScopeConfigInterface $config,
LoggerInterface $logger,
FlagManager $flagManager,
ResponseResolver $responseResolver
Expand All @@ -90,12 +90,11 @@ public function execute()
if ($this->analyticsToken->isTokenExist()) {
$response = $this->httpClient->request(
ZendClient::PUT,
$this->config->getConfigDataValue($this->updateUrlPath),
$this->config->getValue($this->updateUrlPath),
[
"url" => $this->flagManager->getFlagData(BaseUrlConfigPlugin::OLD_BASE_URL_FLAG_CODE),
"new-url" => $this->config->getConfigDataValue(
Store::XML_PATH_SECURE_BASE_URL
),
"url" => $this->flagManager
->getFlagData(SubscriptionUpdateHandler::PREVIOUS_BASE_URL_FLAG_CODE),
"new-url" => $this->config->getValue(Store::XML_PATH_SECURE_BASE_URL),
"access-token" => $this->analyticsToken->getToken(),
]
);
Expand All @@ -110,6 +109,6 @@ public function execute()
}
}

return $result;
return (bool)$result;
}
}
Loading

0 comments on commit 20e58ef

Please sign in to comment.