Skip to content

Commit

Permalink
Merge pull request #1007 from magento-firedrakes/bugfixes
Browse files Browse the repository at this point in the history
[Firedrakes] Bugfixes
  • Loading branch information
Volodymyr Klymenko authored Apr 11, 2017
2 parents e4adb6b + 3d47143 commit a9bd85b
Show file tree
Hide file tree
Showing 67 changed files with 1,596 additions and 1,300 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,24 @@

namespace Magento\Analytics\Controller\Adminhtml\Subscription;

use Magento\Analytics\Model\Config\Backend\Enabled;
use Magento\Analytics\Model\NotificationTime;
use Magento\Analytics\Model\Subscription;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Config\Model\Config\Source\Enabledisable;
use Magento\Config\Model\PreparedValueFactory;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
use Psr\Log\LoggerInterface;

/**
* Activates subscription to Magento BI Advanced Reporting.
*/
class Activate extends Action
{
/**
* Resource for managing subscription to Magento BI.
*
* @var Subscription
*/
private $subscription;

/**
* @var LoggerInterface
*/
Expand All @@ -46,23 +43,36 @@ class Activate extends Action
*/
private $subscriptionApprovedField = 'analytics_subscription_checkbox';

/**
* @var AbstractDb
*/
private $configValueResource;

/**
* @var PreparedValueFactory
*/
private $preparedValueFactory;

/**
* Activate constructor.
*
* @param Context $context
* @param Subscription $subscription
* @param LoggerInterface $logger
* @param NotificationTime $notificationTime
* @param AbstractDb $configValueResource
* @param PreparedValueFactory $preparedValueFactory
*/
public function __construct(
Context $context,
Subscription $subscription,
LoggerInterface $logger,
NotificationTime $notificationTime
NotificationTime $notificationTime,
AbstractDb $configValueResource,
PreparedValueFactory $preparedValueFactory
) {
$this->subscription = $subscription;
$this->logger = $logger;
$this->notificationTime = $notificationTime;
$this->configValueResource = $configValueResource;
$this->preparedValueFactory = $preparedValueFactory;
parent::__construct($context);
}

Expand All @@ -85,7 +95,14 @@ public function execute()
{
try {
if ($this->getRequest()->getParam($this->subscriptionApprovedField)) {
$this->subscription->enable();
$configValue = $this->preparedValueFactory->create(
Enabled::XML_ENABLED_CONFIG_STRUCTURE_PATH,
Enabledisable::ENABLE_VALUE,
ScopeConfigInterface::SCOPE_TYPE_DEFAULT
);

$this->configValueResource
->save($configValue);
} else {
$this->notificationTime->unsetLastTimeNotificationValue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Magento\Analytics\Controller\Adminhtml\Subscription;

use Magento\Analytics\Model\Subscription;
use Magento\Analytics\Model\Config\Backend\Enabled\SubscriptionHandler;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\Result\Redirect;
Expand All @@ -21,19 +21,19 @@ class Retry extends Action
/**
* Resource for managing subscription to Magento Analytics.
*
* @var Subscription
* @var SubscriptionHandler
*/
private $subscription;
private $subscriptionHandler;

/**
* @param Context $context
* @param Subscription $subscription
* @param SubscriptionHandler $subscriptionHandler
*/
public function __construct(
Context $context,
Subscription $subscription
SubscriptionHandler $subscriptionHandler
) {
$this->subscription = $subscription;
$this->subscriptionHandler = $subscriptionHandler;
parent::__construct($context);
}

Expand All @@ -58,7 +58,7 @@ public function execute()
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
try {
$resultRedirect->setPath('adminhtml');
$this->subscription->retry();
$this->subscriptionHandler->processEnabled();
} catch (LocalizedException $e) {
$this->getMessageManager()->addExceptionMessage($e, $e->getMessage());
} catch (\Exception $e) {
Expand Down
5 changes: 5 additions & 0 deletions app/code/Magento/Analytics/Model/Config/Backend/Enabled.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
*/
class Enabled extends Value
{
/**
* Path to field subscription enabled into config structure.
*/
const XML_ENABLED_CONFIG_STRUCTURE_PATH = 'analytics/general/enabled';

/**
* Service for processing of activation/deactivation MBI subscription.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
use Magento\Analytics\Model\AnalyticsToken;
use Magento\Analytics\Model\Config\Backend\CollectionTime;
use Magento\Analytics\Model\NotificationTime;
use Magento\Framework\FlagManager;
use Magento\Framework\App\Config\ReinitableConfigInterface;
use Magento\Framework\App\Config\Storage\WriterInterface;
use Magento\Framework\FlagManager;

/**
* Class for processing of activation/deactivation MBI subscription.
Expand Down Expand Up @@ -61,22 +62,30 @@ class SubscriptionHandler
*/
private $notificationTime;

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

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

/**
Expand All @@ -92,6 +101,7 @@ public function processEnabled()
$this->setCronSchedule();
$this->setAttemptsFlag();
$this->notificationTime->unsetLastTimeNotificationValue();
$this->reinitableConfig->reinit();
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ interface CommandInterface
/**
* Execute call to external service
* Information about destination and arguments appears from config
* @return void
*
* @return bool
*/
public function execute();
}
77 changes: 55 additions & 22 deletions app/code/Magento/Analytics/Model/Connector/Http/Client/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
*/
namespace Magento\Analytics\Model\Connector\Http\Client;

use Magento\Analytics\Model\Connector\Http\ResponseFactory;
use Magento\Framework\HTTP\Adapter\CurlFactory;
use Magento\Analytics\Model\Connector\Http\ConverterInterface;
use Psr\Log\LoggerInterface;
use Magento\Framework\HTTP\Adapter\CurlFactory;
use Magento\Analytics\Model\Connector\Http\ResponseFactory;

/**
* A CURL HTTP client.
Expand All @@ -16,11 +17,6 @@
*/
class Curl implements \Magento\Analytics\Model\Connector\Http\ClientInterface
{
/**
* @var LoggerInterface
*/
private $logger;

/**
* @var CurlFactory
*/
Expand All @@ -31,46 +27,83 @@ class Curl implements \Magento\Analytics\Model\Connector\Http\ClientInterface
*/
private $responseFactory;

/**
* @var ConverterInterface
*/
private $converter;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @param CurlFactory $curlFactory
* @param ResponseFactory $responseFactory
* @param ConverterInterface $converter
* @param LoggerInterface $logger
*/
public function __construct(
CurlFactory $curlFactory,
ResponseFactory $responseFactory,
ConverterInterface $converter,
LoggerInterface $logger
) {
$this->curlFactory = $curlFactory;
$this->responseFactory = $responseFactory;
$this->converter = $converter;
$this->logger = $logger;
}

/**
* {@inheritdoc}
*/
public function request($method, $url, $body = '', array $headers = [], $version = '1.1')
public function request($method, $url, array $body = [], array $headers = [], $version = '1.1')
{
$curl = $this->curlFactory->create();
$response = new \Zend_Http_Response(0, []);

$curl->write($method, $url, $version, $headers, $body);
try {
$curl = $this->curlFactory->create();
$headers = $this->applyContentTypeHeaderFromConverter($headers);

$result = $curl->read();
$curl->write($method, $url, $version, $headers, $this->converter->toBody($body));

if ($curl->getErrno()) {
$this->logger->critical(
new \Exception(
sprintf(
'MBI service CURL connection error #%s: %s',
$curl->getErrno(),
$curl->getError()
$result = $curl->read();

if ($curl->getErrno()) {
$this->logger->critical(
new \Exception(
sprintf(
'MBI service CURL connection error #%s: %s',
$curl->getErrno(),
$curl->getError()
)
)
)
);
);

return $response;
}

return false;
$response = $this->responseFactory->create($result);
} catch (\Exception $e) {
$this->logger->critical($e);
}

return $response;
}

/**
* @param array $headers
*
* @return array
*/
private function applyContentTypeHeaderFromConverter(array $headers)
{
$contentTypeHeaderKey = array_search($this->converter->getContentTypeHeader(), $headers);
if ($contentTypeHeaderKey === false) {
$headers[] = $this->converter->getContentTypeHeader();
}

return $this->responseFactory->create($result);
return $headers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ interface ClientInterface
*
* @param string $method
* @param string $url
* @param string $body
* @param array $body
* @param array $headers
* @param string $version
*
* @return \Zend_Http_Response|bool
* @return \Zend_Http_Response
*/
public function request($method, $url, $body = '', array $headers = [], $version = '1.1');
public function request($method, $url, array $body = [], array $headers = [], $version = '1.1');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Analytics\Model\Connector\Http;

/**
* Represents converter interface for http request and response body.
*/
interface ConverterInterface
{
/**
* @param string $body
*
* @return array
*/
public function fromBody($body);

/**
* @param array $data
*
* @return string
*/
public function toBody(array $data);

/**
* @return string
*/
public function getContentTypeHeader();
}
Loading

0 comments on commit a9bd85b

Please sign in to comment.