Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into MAGETWO-54361
Browse files Browse the repository at this point in the history
# Conflicts:
#	composer.lock
  • Loading branch information
adifucan committed Feb 13, 2017
2 parents b82f079 + 7c81c1d commit c8be94d
Show file tree
Hide file tree
Showing 279 changed files with 13,278 additions and 1,495 deletions.
@@ -0,0 +1,62 @@
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Analytics\Controller\Adminhtml\BasicTier;

use Magento\Backend\App\Action;
use Magento\Config\Model\Config;
use Magento\Backend\App\Action\Context;

/**
* Class SignUp
*
* Provides link to Basic Tier signup
*/
class SignUp extends Action
{
/**
* @var string
*/
private $basicTierUrlPath = 'analytics/url/basic_tier';

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

/**
* @param Context $context
* @param Config $config
*/
public function __construct(
Context $context,
Config $config
) {
$this->config = $config;
parent::__construct($context);
}

/**
* Check admin permissions for this controller
*
* @return boolean
*/
protected function _isAllowed()
{
return $this->_authorization->isAllowed('Magento_Analytics::report_basic_tier');
}

/**
* Provides link to Basic Tier signup
*
* @return \Magento\Framework\Controller\AbstractResult
*/
public function execute()
{
return $this->resultRedirectFactory->create()->setUrl(
$this->config->getConfigDataValue($this->basicTierUrlPath)
);
}
}
61 changes: 61 additions & 0 deletions app/code/Magento/Analytics/Controller/Adminhtml/Export/Example.php
@@ -0,0 +1,61 @@
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Analytics\Controller\Adminhtml\Export;

use Magento\Backend\App\Action;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\App\Response\Http\FileFactory;
use Magento\Analytics\Model\Export;
use Magento\Framework\App\Filesystem\DirectoryList;

/**
* Class Example
*/
class Example extends Action
{
/**
* @var Export
*/
private $export;

/**
* @var FileFactory
*/
private $fileFactory;

/**
* Example constructor.
*
* @param Action\Context $context
* @param Export $export
* @param FileFactory $fileFactory
*/
public function __construct(
Action\Context $context,
Export $export,
FileFactory $fileFactory
) {
parent::__construct($context);
$this->export = $export;
$this->fileFactory = $fileFactory;
}

/**
* Controller for demo
*
* @return ResponseInterface
* @throws \Exception
*/
public function execute()
{
return $this->fileFactory->create(
'analytics-export.tgz',
$this->export->getArchiveContent(),
DirectoryList::VAR_DIR
);
}
}
71 changes: 71 additions & 0 deletions app/code/Magento/Analytics/Controller/Adminhtml/Reports/Show.php
@@ -0,0 +1,71 @@
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Analytics\Controller\Adminhtml\Reports;

use Magento\Analytics\Model\ReportUrlProvider;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Exception\LocalizedException;

/**
* Provide redirect to resource with reports.
*/
class Show extends Action
{
/**
* @var ReportUrlProvider
*/
private $reportUrlProvider;

/**
* @param Context $context
* @param ReportUrlProvider $reportUrlProvider
*/
public function __construct(
Context $context,
ReportUrlProvider $reportUrlProvider
) {
$this->reportUrlProvider = $reportUrlProvider;
parent::__construct($context);
}

/**
* Check admin permissions for this controller.
*
* @return boolean
*/
protected function _isAllowed()
{
return $this->_authorization->isAllowed('Magento_Analytics::analytics_settings');
}

/**
* Redirect to resource with reports.
*
* @return Redirect $resultRedirect
*/
public function execute()
{
/** @var Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
try {
$resultRedirect->setUrl($this->reportUrlProvider->getUrl());
} catch (LocalizedException $e) {
$this->getMessageManager()->addExceptionMessage($e, $e->getMessage());
$resultRedirect->setPath('adminhtml');
} catch (\Exception $e) {
$this->getMessageManager()->addExceptionMessage(
$e,
__('Sorry, there has been an error processing your request. Please try again later.')
);
$resultRedirect->setPath('adminhtml');
}

return $resultRedirect;
}
}
18 changes: 13 additions & 5 deletions app/code/Magento/Analytics/Model/AnalyticsConnector.php
Expand Up @@ -9,13 +9,19 @@
use Magento\Framework\ObjectManagerInterface;

/**
* Class AnalyticsConnector
* A connector to external services.
*
* Connector to MA services, aggregates calls to external services.
* Aggregates and executes commands which perform requests to external services.
*/
class AnalyticsConnector
{
/**
* A list of possible commands.
*
* An associative array in format: 'command_name' => 'command_class_name'.
*
* The list may be configured in each module via '/etc/di.xml'.
*
* @var string[]
*/
private $commands;
Expand All @@ -26,7 +32,6 @@ class AnalyticsConnector
private $objectManager;

/**
* AnalyticsConnector constructor.
* @param array $commands
* @param ObjectManagerInterface $objectManager
*/
Expand All @@ -39,18 +44,21 @@ public function __construct(
}

/**
* Create the instance of the command and execute it.
* Executes a command in accordance with the given name.
*
* @param string $commandName
* @return bool
* @throws NotFoundException
* @throws NotFoundException if the command is not found.
*/
public function execute($commandName)
{
if (!array_key_exists($commandName, $this->commands)) {
throw new NotFoundException(__('Command was not found.'));
}

/** @var \Magento\Analytics\Model\AnalyticsConnector\AnalyticsCommandInterface $command */
$command = $this->objectManager->create($this->commands[$commandName]);

return $command->execute();
}
}
129 changes: 129 additions & 0 deletions app/code/Magento/Analytics/Model/AnalyticsConnector/OTPRequest.php
@@ -0,0 +1,129 @@
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Analytics\Model\AnalyticsConnector;

use Magento\Analytics\Model\AnalyticsToken;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\HTTP\ZendClient as HttpClient;
use Magento\Framework\HTTP\ZendClientFactory as HttpClientFactory;
use Magento\Store\Model\Store;
use Psr\Log\LoggerInterface;
use Zend_Http_Response as HttpResponse;

/**
* Perform direct call to MBI services for getting OTP.
*
* OTP (One-Time Password) is a password that is valid for only one login session
* and has limited time when it is valid.
*/
class OTPRequest
{
/**
* Resource for handling MBI token value.
*
* @var AnalyticsToken
*/
private $analyticsToken;

/**
* @var HttpClientFactory
*/
private $clientFactory;

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

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

/**
* Path to config value with URL which provide OTP for MBI.
*
* @var string
*/
private $otpUrlConfigPath = 'analytics/url/otp';

/**
* @param AnalyticsToken $analyticsToken
* @param HttpClientFactory $clientFactory
* @param ScopeConfigInterface $config
* @param LoggerInterface $logger
*/
public function __construct(
AnalyticsToken $analyticsToken,
HttpClientFactory $clientFactory,
ScopeConfigInterface $config,
LoggerInterface $logger
) {
$this->analyticsToken = $analyticsToken;
$this->clientFactory = $clientFactory;
$this->config = $config;
$this->logger = $logger;
}

/**
* Performs call to MBI services for getting OTP.
*
* @return string|false OTP or false if request was unsuccessful.
*/
public function call()
{
$otp = false;
try {
if ($this->analyticsToken->isTokenExist()) {
/** @var HttpClient $client */
$client = $this->clientFactory->create();
$client->setUri($this->config->getValue($this->otpUrlConfigPath));
$client->setRawData($this->getRequestJson());
$client->setMethod(HttpClient::POST);
$otp = $this->extractOtp($client->request());
if (!$otp) {
$this->logger->critical('The request for a OTP is unsuccessful.');
}
}
} catch (\Exception $e) {
$this->logger->critical($e->getMessage());
}

return $otp;
}

/**
* Prepares json string with data for request.
*
* @return string
*/
private function getRequestJson()
{
return json_encode(
[
"token" => $this->analyticsToken->getToken(),
"url" => $this->config->getValue(Store::XML_PATH_SECURE_BASE_URL),
]
);
}

/**
* Extracts OTP from the response.
*
* @param HttpResponse $response
* @return string|false False if response doesn't contain required data.
*/
private function extractOtp(HttpResponse $response)
{
$otp = false;
if ($response->getStatus() === 200) {
$body = json_decode($response->getBody(), 1);
$otp = !empty($body['otp']) ? $body['otp'] : false;
}

return $otp;
}
}
Expand Up @@ -69,7 +69,7 @@ private function getRequestJson($integrationToken)
return json_encode(
[
"token" => $integrationToken,
"url" => $this->config->getConfigDataValue(Store::XML_PATH_UNSECURE_BASE_URL)
"url" => $this->config->getConfigDataValue(Store::XML_PATH_SECURE_BASE_URL)
]
);
}
Expand Down

0 comments on commit c8be94d

Please sign in to comment.