ltsecurityinc安防产品在线商城代码
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Plumrocket_Base
|
||||
* @copyright Copyright (c) 2020 Plumrocket Inc. (https://plumrocket.com)
|
||||
* @license https://plumrocket.com/license End-user License Agreement
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Plumrocket\Base\Model\Statistic\Usage;
|
||||
|
||||
use Plumrocket\Base\Api\ModuleUsageStatisticCollectorInterface;
|
||||
|
||||
/**
|
||||
* @since 2.3.0
|
||||
*/
|
||||
class Collector implements ModuleUsageStatisticCollectorInterface
|
||||
{
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Statistic\Usage\StatusInterface
|
||||
*/
|
||||
private $status;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Api\UsageStatisticCollectorInterface[]
|
||||
*/
|
||||
private $collectors;
|
||||
|
||||
/**
|
||||
* Collector constructor.
|
||||
*
|
||||
* @param \Plumrocket\Base\Model\Statistic\Usage\StatusInterface $status
|
||||
* @param array $collectors
|
||||
*/
|
||||
public function __construct(
|
||||
StatusInterface $status,
|
||||
array $collectors = []
|
||||
) {
|
||||
$this->status = $status;
|
||||
$this->collectors = $collectors;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function collect(): array
|
||||
{
|
||||
$result = [];
|
||||
foreach ($this->collectors as $type => $collector) {
|
||||
$result[$type] = $collector->collect();
|
||||
}
|
||||
|
||||
$this->status->switchToMiss();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function hasNewData(): bool
|
||||
{
|
||||
return $this->status->check();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Plumrocket_Base
|
||||
* @copyright Copyright (c) 2020 Plumrocket Inc. (https://plumrocket.com)
|
||||
* @license https://plumrocket.com/license End-user License Agreement
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Plumrocket\Base\Model\Statistic\Usage;
|
||||
|
||||
use Plumrocket\Base\Api\GetExtensionInformationInterface;
|
||||
use Plumrocket\Base\Api\UsageStatisticCollectorInterface;
|
||||
use Plumrocket\Base\Model\ConfigUtils;
|
||||
use Plumrocket\Base\Model\Extension\Customer\GetTrueCustomerKey;
|
||||
|
||||
/**
|
||||
* Class for integrate and merge other collectors data
|
||||
*
|
||||
* @since 2.3.0
|
||||
*/
|
||||
class CompositeCollector implements UsageStatisticCollectorInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\ConfigUtils
|
||||
*/
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Api\GetExtensionInformationInterface
|
||||
*/
|
||||
private $getExtensionInformation;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Api\ModuleUsageStatisticCollectorInterface[]
|
||||
*/
|
||||
private $moduleCollectors;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\Customer\GetTrueCustomerKey
|
||||
*/
|
||||
private $getTrueCustomerKey;
|
||||
|
||||
/**
|
||||
* @param \Plumrocket\Base\Model\ConfigUtils $config
|
||||
* @param \Plumrocket\Base\Api\GetExtensionInformationInterface $getExtensionInformation
|
||||
* @param \Plumrocket\Base\Model\Extension\Customer\GetTrueCustomerKey $getTrueCustomerKey
|
||||
* @param array $moduleCollectors
|
||||
*/
|
||||
public function __construct(
|
||||
ConfigUtils $config,
|
||||
GetExtensionInformationInterface $getExtensionInformation,
|
||||
GetTrueCustomerKey $getTrueCustomerKey,
|
||||
array $moduleCollectors = []
|
||||
) {
|
||||
$this->config = $config;
|
||||
$this->getExtensionInformation = $getExtensionInformation;
|
||||
$this->moduleCollectors = $moduleCollectors;
|
||||
$this->getTrueCustomerKey = $getTrueCustomerKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function collect(): array
|
||||
{
|
||||
$modulesData = [];
|
||||
foreach ($this->moduleCollectors as $moduleName => $moduleCollector) {
|
||||
if (! $moduleCollector->hasNewData()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$extensionInformation = $this->getExtensionInformation->execute($moduleName);
|
||||
$serialKey = (string) $this->config->getStoreConfig(
|
||||
"{$extensionInformation->getConfigSection()}/general/serial"
|
||||
);
|
||||
|
||||
$modulesData[$moduleName] = [
|
||||
'license_key' => $serialKey,
|
||||
'customer_hash'=> $this->getTrueCustomerKey->execute($moduleName),
|
||||
'statistic' => $moduleCollector->collect()
|
||||
];
|
||||
}
|
||||
|
||||
return $modulesData;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,239 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Plumrocket_Base
|
||||
* @copyright Copyright (c) 2020 Plumrocket Inc. (https://plumrocket.com)
|
||||
* @license https://plumrocket.com/license End-user License Agreement
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Plumrocket\Base\Model\Statistic\Usage\Config;
|
||||
|
||||
use Magento\Config\Model\Config\Structure\Element\Field;
|
||||
use Magento\Config\Model\Config\Structure\Element\Group;
|
||||
use Magento\Config\Model\Config\Structure\Element\Section;
|
||||
use Magento\Framework\App\Area;
|
||||
use Magento\Framework\App\Config\Initial\Reader;
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Magento\Framework\App\State;
|
||||
use Magento\Framework\Config\ScopeInterface;
|
||||
use Magento\Framework\DataObject;
|
||||
use Magento\Framework\DataObjectFactory;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Exception\State\InvalidTransitionException;
|
||||
use Plumrocket\Base\Model\Extensions\Information;
|
||||
|
||||
/**
|
||||
* @since 2.3.0
|
||||
*/
|
||||
class Collector
|
||||
{
|
||||
const ATTRIBUTE_USE_IN_STATISTIC = 'pr_use_in_statistic';
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\App\Config\ScopeConfigInterface
|
||||
*/
|
||||
private $scopeConfig;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Statistic\Usage\Config\GetConfigStructure
|
||||
*/
|
||||
private $getConfigStructure;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Config\ScopeInterface
|
||||
*/
|
||||
private $scope;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\App\State
|
||||
*/
|
||||
private $state;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\App\Config\Initial\Reader
|
||||
*/
|
||||
private $configReader;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\DataObjectFactory
|
||||
*/
|
||||
private $dataObjectFactory;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\DataObject|null
|
||||
*/
|
||||
protected $defaultConfig;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $configSection;
|
||||
|
||||
/**
|
||||
* Collector constructor.
|
||||
*
|
||||
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
|
||||
* @param \Plumrocket\Base\Model\Statistic\Usage\Config\GetConfigStructure $getConfigStructure
|
||||
* @param \Magento\Framework\Config\ScopeInterface $scope
|
||||
* @param \Magento\Framework\App\State $state
|
||||
* @param \Magento\Framework\App\Config\Initial\Reader $configReader
|
||||
* @param \Magento\Framework\DataObjectFactory $dataObjectFactory
|
||||
* @param string $configSection
|
||||
*/
|
||||
public function __construct(
|
||||
ScopeConfigInterface $scopeConfig,
|
||||
GetConfigStructure $getConfigStructure,
|
||||
ScopeInterface $scope,
|
||||
State $state,
|
||||
Reader $configReader,
|
||||
DataObjectFactory $dataObjectFactory,
|
||||
string $configSection = Information::CONFIG_SECTION
|
||||
) {
|
||||
$this->scopeConfig = $scopeConfig;
|
||||
$this->getConfigStructure = $getConfigStructure;
|
||||
$this->scope = $scope;
|
||||
$this->state = $state;
|
||||
$this->configReader = $configReader;
|
||||
$this->dataObjectFactory = $dataObjectFactory;
|
||||
$this->configSection = $configSection;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @throws \Magento\Framework\Exception\State\InvalidTransitionException
|
||||
*/
|
||||
public function collect(): array
|
||||
{
|
||||
$currentScope = $this->scope->getCurrentScope();
|
||||
|
||||
$data = [];
|
||||
|
||||
try {
|
||||
$data = $this->state->emulateAreaCode(Area::AREA_ADMINHTML, function () {
|
||||
$this->scope->setCurrentScope(Area::AREA_ADMINHTML);
|
||||
|
||||
return $this->collectConfigs();
|
||||
});
|
||||
|
||||
$this->scope->setCurrentScope($currentScope);
|
||||
} catch (\Exception $e) {
|
||||
throw new InvalidTransitionException(__('%1', $e->getMessage()), $e);
|
||||
} finally {
|
||||
$this->scope->setCurrentScope($currentScope);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function collectConfigs(): array
|
||||
{
|
||||
$configData = [];
|
||||
/**
|
||||
* Use factory (getConfigStructure) in order to initialize object after code area emulated
|
||||
* @var \Magento\Config\Model\Config\Structure\Element\Section $section
|
||||
*/
|
||||
$section = $this->getConfigStructure->execute()->getElement($this->configSection);
|
||||
/** @var \Magento\Config\Model\Config\Structure\Element\Group $group */
|
||||
foreach ($section->getChildren() as $group) {
|
||||
/** @var \Magento\Config\Model\Config\Structure\Element\Field $field */
|
||||
foreach ($group->getChildren() as $field) {
|
||||
if ($field->getAttribute(self::ATTRIBUTE_USE_IN_STATISTIC)) {
|
||||
$configData[$field->getPath()] = $this->proccesField($section, $group, $field);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $configData;
|
||||
}
|
||||
|
||||
/**
|
||||
* [
|
||||
* 'label' => '',
|
||||
* 'value' => '',
|
||||
* 'options' => [
|
||||
* ['value' => value_1, 'label' => label_1],
|
||||
* ['value' => value_1, 'label' => label_1],
|
||||
* ],
|
||||
* 'default' => ''
|
||||
* ]
|
||||
*
|
||||
* @param \Magento\Config\Model\Config\Structure\Element\Section $section
|
||||
* @param \Magento\Config\Model\Config\Structure\Element\Group $group
|
||||
* @param \Magento\Config\Model\Config\Structure\Element\Field $field
|
||||
* @return array
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
protected function proccesField(
|
||||
Section $section,
|
||||
Group $group,
|
||||
Field $field
|
||||
) {
|
||||
$fieldData = [
|
||||
'label' => $field->getLabel(),
|
||||
'value' => $this->anonymizeData($field, 'value', $this->scopeConfig->getValue($field->getPath())),
|
||||
'default' => $this->getDefaultConfig()->getDataByPath($field->getPath()),
|
||||
];
|
||||
|
||||
if (in_array($field->getType(), ['select', 'multiselect'], true)) {
|
||||
$fieldData['options'] = $this->prepareOptions(
|
||||
$this->anonymizeData($field, 'options', $field->getOptions())
|
||||
);
|
||||
}
|
||||
|
||||
return $fieldData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Magento\Framework\DataObject
|
||||
*/
|
||||
protected function getDefaultConfig(): DataObject
|
||||
{
|
||||
if (null === $this->defaultConfig) {
|
||||
try {
|
||||
$config = $this->configReader->read();
|
||||
$moduleConfig = $config['data']['default'][$this->configSection] ?? [];
|
||||
} catch (LocalizedException $e) {
|
||||
$moduleConfig = [];
|
||||
}
|
||||
|
||||
$this->defaultConfig = $this->dataObjectFactory->create(
|
||||
['data' => [$this->configSection => $moduleConfig]]
|
||||
);
|
||||
}
|
||||
|
||||
return $this->defaultConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $options
|
||||
* @return array
|
||||
*/
|
||||
protected function prepareOptions(array $options) : array
|
||||
{
|
||||
return array_map(
|
||||
static function ($option) {
|
||||
$option['label'] = (string) $option['label'];
|
||||
return $option;
|
||||
},
|
||||
$options
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Magento\Config\Model\Config\Structure\Element\Field $field
|
||||
* @param string $type
|
||||
* @param $currentValue
|
||||
* @return array|mixed|string
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
protected function anonymizeData(Field $field, string $type, $currentValue)
|
||||
{
|
||||
return $currentValue;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Plumrocket_Base
|
||||
* @copyright Copyright (c) 2020 Plumrocket Inc. (https://plumrocket.com)
|
||||
* @license https://plumrocket.com/license End-user License Agreement
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Plumrocket\Base\Model\Statistic\Usage\Config;
|
||||
|
||||
use Magento\Config\Model\Config\Structure;
|
||||
|
||||
/**
|
||||
* @since 2.3.0
|
||||
*/
|
||||
class GetConfigStructure
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Config\Model\Config\StructureFactory
|
||||
*/
|
||||
private $configStructureFactory;
|
||||
|
||||
/**
|
||||
* @var \Magento\Config\Model\Config\Structure
|
||||
*/
|
||||
private $configStructure;
|
||||
|
||||
/**
|
||||
* GetConfigStructure constructor.
|
||||
*
|
||||
* @param \Magento\Config\Model\Config\StructureFactory $configStructureFactory
|
||||
*/
|
||||
public function __construct(\Magento\Config\Model\Config\StructureFactory $configStructureFactory)
|
||||
{
|
||||
$this->configStructureFactory = $configStructureFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Magento\Config\Model\Config\Structure
|
||||
*/
|
||||
public function execute(): Structure
|
||||
{
|
||||
if (null === $this->configStructure) {
|
||||
$this->configStructure = $this->configStructureFactory->create();
|
||||
}
|
||||
|
||||
return $this->configStructure;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Plumrocket_Base
|
||||
* @copyright Copyright (c) 2020 Plumrocket Inc. (https://plumrocket.com)
|
||||
* @license https://plumrocket.com/license/ End-user License Agreement
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Plumrocket\Base\Model\Statistic\Usage;
|
||||
|
||||
use Magento\Framework\HTTP\Client\Curl;
|
||||
use Magento\Framework\Serialize\SerializerInterface;
|
||||
use Plumrocket\Base\Model\External\Urls;
|
||||
|
||||
/**
|
||||
* Send usage statistic
|
||||
*
|
||||
* @since 2.3.0
|
||||
*/
|
||||
class Sender
|
||||
{
|
||||
/**
|
||||
* @var Curl
|
||||
*/
|
||||
private $curl;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Serialize\SerializerInterface
|
||||
*/
|
||||
private $serializer;
|
||||
|
||||
/**
|
||||
* @param \Magento\Framework\HTTP\Client\Curl $curl
|
||||
* @param \Magento\Framework\Serialize\SerializerInterface $serializer
|
||||
*/
|
||||
public function __construct(
|
||||
Curl $curl,
|
||||
SerializerInterface $serializer
|
||||
) {
|
||||
$this->curl = $curl;
|
||||
$this->serializer = $serializer;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $usageStatisticData
|
||||
* @return bool
|
||||
*/
|
||||
public function send(array $usageStatisticData): bool
|
||||
{
|
||||
try {
|
||||
$this->curl->post(
|
||||
'https://' . Urls::STATISTIC_URL,
|
||||
$this->serializer->serialize($usageStatisticData)
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->curl->getStatus() === 200;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Plumrocket_Base
|
||||
* @copyright Copyright (c) 2020 Plumrocket Inc. (https://plumrocket.com)
|
||||
* @license https://plumrocket.com/license End-user License Agreement
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Plumrocket\Base\Model\Statistic\Usage;
|
||||
|
||||
use Magento\Config\Model\ConfigFactory;
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Magento\Framework\App\ResourceConnection;
|
||||
|
||||
/**
|
||||
* @since 2.3.0
|
||||
*/
|
||||
class Status implements StatusInterface
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Framework\App\Config\ScopeConfigInterface
|
||||
*/
|
||||
private $scopeConfig;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\App\ResourceConnection
|
||||
*/
|
||||
private $resourceConnection;
|
||||
|
||||
/**
|
||||
* @var \Magento\Config\Model\ConfigFactory
|
||||
*/
|
||||
private $configFactory;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $xmlPath;
|
||||
|
||||
/**
|
||||
* SystemConfigSaveAfter constructor.
|
||||
*
|
||||
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
|
||||
* @param \Magento\Framework\App\ResourceConnection $resourceConnection
|
||||
* @param \Magento\Config\Model\ConfigFactory $configFactory
|
||||
* @param string $xmlPath
|
||||
*/
|
||||
public function __construct(
|
||||
ScopeConfigInterface $scopeConfig,
|
||||
ResourceConnection $resourceConnection,
|
||||
ConfigFactory $configFactory,
|
||||
string $xmlPath
|
||||
) {
|
||||
$this->scopeConfig = $scopeConfig;
|
||||
$this->resourceConnection = $resourceConnection;
|
||||
$this->configFactory = $configFactory;
|
||||
$this->xmlPath = $xmlPath . (strpos($xmlPath, '/') === false ? '/general/new_changes' : '');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function check(): bool
|
||||
{
|
||||
return (bool) $this->scopeConfig->getValue($this->xmlPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function switchToCollect(): StatusInterface
|
||||
{
|
||||
return $this->changeStatus(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function switchToMiss(): StatusInterface
|
||||
{
|
||||
return $this->changeStatus(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $status
|
||||
* @return $this
|
||||
*/
|
||||
private function changeStatus(int $status): self
|
||||
{
|
||||
/** @var \Magento\Config\Model\Config $config */
|
||||
$config = $this->configFactory->create();
|
||||
$connection = $this->resourceConnection->getConnection('core_write');
|
||||
|
||||
$connection->delete(
|
||||
$this->resourceConnection->getTableName('core_config_data'),
|
||||
[
|
||||
$connection->quoteInto('path = ?', $this->xmlPath)
|
||||
]
|
||||
);
|
||||
|
||||
$config->setDataByPath($this->xmlPath, $status);
|
||||
|
||||
try {
|
||||
$config->save();
|
||||
} catch (\Exception $e) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Plumrocket_Base
|
||||
* @copyright Copyright (c) 2020 Plumrocket Inc. (https://plumrocket.com)
|
||||
* @license https://plumrocket.com/license End-user License Agreement
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Plumrocket\Base\Model\Statistic\Usage;
|
||||
|
||||
/**
|
||||
* @since 2.3.0
|
||||
*/
|
||||
interface StatusInterface
|
||||
{
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function check(): bool;
|
||||
|
||||
/**
|
||||
* @return \Plumrocket\Base\Model\Statistic\Usage\StatusInterface
|
||||
*/
|
||||
public function switchToCollect(): StatusInterface;
|
||||
|
||||
/**
|
||||
* @return \Plumrocket\Base\Model\Statistic\Usage\StatusInterface
|
||||
*/
|
||||
public function switchToMiss(): StatusInterface;
|
||||
}
|
||||
Reference in New Issue
Block a user