|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2013-2017 Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\Analytics\Controller\Adminhtml\Subscription; |
| 8 | + |
| 9 | +use Magento\Analytics\Model\NotificationTime; |
| 10 | +use Magento\Analytics\Model\Subscription; |
| 11 | +use Magento\Backend\App\Action; |
| 12 | +use Magento\Backend\App\Action\Context; |
| 13 | +use Magento\Framework\Controller\Result\Json; |
| 14 | +use Magento\Framework\Controller\ResultFactory; |
| 15 | +use Magento\Framework\Exception\LocalizedException; |
| 16 | +use Psr\Log\LoggerInterface; |
| 17 | + |
| 18 | +/** |
| 19 | + * Class Activate |
| 20 | + * |
| 21 | + * Activates subscription with Free Tier program |
| 22 | + */ |
| 23 | +class Activate extends Action |
| 24 | +{ |
| 25 | + /** |
| 26 | + * Resource for managing subscription to Magento Analytics. |
| 27 | + * |
| 28 | + * @var Subscription |
| 29 | + */ |
| 30 | + private $subscription; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var LoggerInterface |
| 34 | + */ |
| 35 | + private $logger; |
| 36 | + |
| 37 | + /** |
| 38 | + * Resource for managing last notification time about subscription to Magento Analytics. |
| 39 | + * |
| 40 | + * @var NotificationTime |
| 41 | + */ |
| 42 | + private $notificationTime; |
| 43 | + |
| 44 | + /** |
| 45 | + * Agreement on subscription value into request. |
| 46 | + * |
| 47 | + * @var string |
| 48 | + */ |
| 49 | + private $subscriptionApprovedField = 'analytics_subscription_checkbox'; |
| 50 | + |
| 51 | + /** |
| 52 | + * Activate constructor. |
| 53 | + * |
| 54 | + * @param Context $context |
| 55 | + * @param Subscription $subscription |
| 56 | + * @param LoggerInterface $logger |
| 57 | + * @param NotificationTime $notificationTime |
| 58 | + */ |
| 59 | + public function __construct( |
| 60 | + Context $context, |
| 61 | + Subscription $subscription, |
| 62 | + LoggerInterface $logger, |
| 63 | + NotificationTime $notificationTime |
| 64 | + ) { |
| 65 | + $this->subscription = $subscription; |
| 66 | + $this->logger = $logger; |
| 67 | + $this->notificationTime = $notificationTime; |
| 68 | + parent::__construct($context); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Check admin permissions for this controller |
| 73 | + * |
| 74 | + * @return boolean |
| 75 | + */ |
| 76 | + protected function _isAllowed() |
| 77 | + { |
| 78 | + return $this->_authorization->isAllowed('Magento_Analytics::analytics_settings'); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Activate subscription to Magento Analytics via AJAX. |
| 83 | + * |
| 84 | + * @return Json |
| 85 | + */ |
| 86 | + public function execute() |
| 87 | + { |
| 88 | + try { |
| 89 | + if ($this->getRequest()->getParam($this->subscriptionApprovedField)) { |
| 90 | + $this->subscription->enable(); |
| 91 | + } else { |
| 92 | + $this->notificationTime->unsetLastTimeNotificationValue(); |
| 93 | + } |
| 94 | + $responseContent = [ |
| 95 | + 'success' => true, |
| 96 | + 'error_message' => '', |
| 97 | + ]; |
| 98 | + } catch (LocalizedException $e) { |
| 99 | + $responseContent = [ |
| 100 | + 'success' => false, |
| 101 | + 'error_message' => $e->getMessage(), |
| 102 | + ]; |
| 103 | + $this->logger->error($e->getMessage()); |
| 104 | + } catch (\Exception $e) { |
| 105 | + $responseContent = [ |
| 106 | + 'success' => false, |
| 107 | + 'error_message' => __( |
| 108 | + 'Sorry, there was an error processing your registration request to Magento Analytics. ' |
| 109 | + . 'Please try again later.' |
| 110 | + ), |
| 111 | + ]; |
| 112 | + $this->logger->error($e->getMessage()); |
| 113 | + } |
| 114 | + /** @var Json $resultJson */ |
| 115 | + $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON); |
| 116 | + return $resultJson->setData($responseContent); |
| 117 | + } |
| 118 | +} |
0 commit comments