Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize getSystemConfigAttribute and Load config from ScopeConfig #431

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 76 additions & 57 deletions Model/Config/ConfigManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Magento\Config\Model\Config\Backend\Serialized\ArraySerialized;
use Magento\Eav\Model\Entity\Attribute;
use Magento\Framework\App\Area;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Adapter\AdapterInterface;
Expand Down Expand Up @@ -234,17 +235,23 @@ class ConfigManagement
*/
protected $attributeModel;

/**
* @var ScopeConfigInterface
*/
protected $scopeConfig;

/**
* ConfigManagement constructor
*
* @param ResourceConnection $resourceConnection
* @param Edition $sourceEdition
* @param Reader $moduleReader
* @param ConfigHelper $configHelper
* @param Repository $assetRepository
* @param DirectoryList $directoryList
* @param ResourceConnection $resourceConnection
* @param Edition $sourceEdition
* @param Reader $moduleReader
* @param ConfigHelper $configHelper
* @param Repository $assetRepository
* @param DirectoryList $directoryList
* @param SerializerInterface $serializer
* @param Website $websiteFormField
* @param Website $websiteFormField
* @param ScopeConfigInterface $scopeConfig
*/
public function __construct(
ResourceConnection $resourceConnection,
Expand All @@ -255,7 +262,8 @@ public function __construct(
DirectoryList $directoryList,
SerializerInterface $serializer,
Website $websiteFormField,
Attribute $attributeModel
Attribute $attributeModel,
ScopeConfigInterface $scopeConfig
) {
$this->resourceConnection = $resourceConnection;
$this->sourceEdition = $sourceEdition;
Expand All @@ -267,6 +275,7 @@ public function __construct(
$this->websiteFormField = $websiteFormField;
$this->assetRepository = $assetRepository;
$this->attributeModel = $attributeModel;
$this->scopeConfig = $scopeConfig;
}

/**
Expand All @@ -287,15 +296,15 @@ public function generatePdf()
$group = '';

/**
* @var int $index
* @var int $index
* @var string[] $config
*/
foreach ($configs as $index => $config) {
/** @var string[] $labelAndGroup */
$labelAndGroup = $this->getSystemConfigAttribute($config['path'], 'label');
/** @var string $label */
$label = (string)$labelAndGroup[self::SYSTEM_ATTRIBUTE_VALUE_ARRAY_KEY];
if (!$label) {
if ( ! $label) {
continue;
}
/** @var string $currentGroup */
Expand All @@ -322,7 +331,7 @@ public function generatePdf()
/** @var string[] $firstElement */
$firstElement = reset($configValueUnserialized);

if (!$firstElement) {
if ( ! $firstElement) {
$value = $this->renderValue('', $config['path']);
$this->page->drawText(
$value,
Expand Down Expand Up @@ -352,7 +361,7 @@ public function generatePdf()
/** @var string[] $lines */
$lines = str_split($cleanValue, 89);

if (!$cleanValue) {
if ( ! $cleanValue) {
$value = $this->renderValue('', $config['path']);
$this->page->drawText(
$value,
Expand All @@ -373,7 +382,7 @@ public function generatePdf()
continue;
}

if ($config['value'] && strpos($config['value'], ',') !== false && !$backendModelAttributeValue) {
if ($config['value'] && strpos($config['value'], ',') !== false && ! $backendModelAttributeValue) {
$this->insertMultiselect($config['value'], $config['path']);
continue;
}
Expand Down Expand Up @@ -415,7 +424,7 @@ public function generatePdf()
* Description setPageStyle function
*
* @param string $font
* @param float $fontSize
* @param float $fontSize
*
* @return void
* @throws Zend_Pdf_Exception
Expand All @@ -435,8 +444,8 @@ protected function setPageStyle(
* Description drawBoldText function
*
* @param string $value
* @param float $x
* @param float $y
* @param float $x
* @param float $y
*
* @return void
* @throws Zend_Pdf_Exception
Expand Down Expand Up @@ -518,7 +527,7 @@ protected function insertSerializedArray(array $values, array $headers, string $
* Description loadAttributeCode function
*
* @param mixed[] $values
* @param string $field
* @param string $field
*
* @return mixed[]
*/
Expand Down Expand Up @@ -618,6 +627,20 @@ protected function getEdition()
return $versions[$this->configHelper->getEdition()];
}

/**
* @return SimpleXMLElement
*/
protected function getSystemConfigXml(): SimpleXMLElement
{
/** @var string $etcDir */
$etcDir = $this->moduleReader->getModuleDir(
Dir::MODULE_ETC_DIR,
'Akeneo_Connector'
);

return simplexml_load_file($etcDir . '/adminhtml/system.xml');
}

/**
* Description getSystemConfigAttribute function
*
Expand All @@ -629,33 +652,19 @@ protected function getEdition()
protected function getSystemConfigAttribute(string $path, string $attributeName)
{
/** @var string[] $path */
$path = explode('/', $path);
/** @var string $etcDir */
$etcDir = $this->moduleReader->getModuleDir(
Dir::MODULE_ETC_DIR,
'Akeneo_Connector'
);
/** @var mixed[] $xml */
$xml = simplexml_load_file($etcDir . '/adminhtml/system.xml');
[$section, $group, $field] = explode('/', $path);

$xml = $this->getSystemConfigXml();
/** @var string $label */
$label = '';
/** @var string $attributeGroup */
$attributeGroup = '';

/** @var SimpleXMLElement $group */
foreach ($xml->{'system'}->{'section'}->{'group'} as $group) {
/** @var string[] $attributes */
$attributes = $group->attributes();
if ((string)$attributes['id'] === $path[1]) {
foreach ($group->{'field'} as $field) {
/** @var string[] $attributexs */
$attributes = $field->attributes();
if ((string)$attributes['id'] === $path[2]) {
$label = $field->{$attributeName};
$attributeGroup = $group->{'label'};
}
}
}
$field = current($xml->xpath(sprintf('//section/group[@id="%s"]/field[@id="%s"]', $group, $field)));
$group = current($field->xpath('parent::*'));
if ($field) {
$label = $field->{$attributeName};
$attributeGroup = $group->{'label'};
}

return [
Expand All @@ -671,16 +680,26 @@ protected function getSystemConfigAttribute(string $path, string $attributeName)
*/
protected function getAllAkeneoConfigs()
{
/** @var AdapterInterface $connection */
$connection = $this->resourceConnection->getConnection();
/** @var Select $select */
$select = $connection->select()->from(
[
'ccd' => 'core_config_data',
]
)->where('path like ?', '%akeneo_connector%')->order('path ASC');

return $connection->fetchAll($select);
$xml = $this->getSystemConfigXml();
$configs = [];

/** @var SimpleXMLElement $field */
foreach ($xml->xpath('//section/group/field') as $field) {
$group = current($field->xpath('parent::*'));
/** @var string[] $groupAttributes */
$groupAttributes = $group->attributes();
/** @var string[] $fieldAttributes */
$fieldAttributes = $field->attributes();
$path = 'akeneo_connector/' . $groupAttributes['id'] . '/' . $fieldAttributes['id'];
$configs[] = [
'path' => $path,
'value' => (string)$this->scopeConfig->getValue($path),
'scope' => 'default',
'scope_id' => 1
];
}

return $configs;
}

/**
Expand Down Expand Up @@ -787,8 +806,8 @@ protected function addLineBreak($nextElementHeight = null, $value = null)
* Description addArrayLine function
*
* @param string[] $values
* @param float $cellLength
* @param float $rowLength
* @param float $cellLength
* @param float $rowLength
*
* @return void
* @throws Zend_Pdf_Exception
Expand All @@ -809,7 +828,7 @@ protected function addArrayRow(
);

/**
* @var int $index
* @var int $index
* @var string $value
*/
foreach ($values as $index => $value) {
Expand Down Expand Up @@ -842,13 +861,13 @@ protected function addArrayRow(
* Description manageBooleanValue function
*
* @param string $value
* @param bool $bypassBoolean
* @param bool $bypassBoolean
*
* @return string
*/
protected function manageBooleanValue(string $value, bool $bypassBoolean)
{
if (!$bypassBoolean && is_numeric($value) && preg_match("/^[0|1]$/", $value)) {
if ( ! $bypassBoolean && is_numeric($value) && preg_match("/^[0|1]$/", $value)) {
if (preg_match("/^[1]$/", $value)) {
return (string)__('Yes');
} else {
Expand All @@ -862,15 +881,15 @@ protected function manageBooleanValue(string $value, bool $bypassBoolean)
/**
* Description renderValue function
*
* @param string $value
* @param string $field
* @param string $value
* @param string $field
* @param null|string $fieldType
*
* @return string
*/
protected function renderValue(string $value, string $field, $fieldType = null)
{
if (!$value && !is_numeric($value)) {
if ( ! $value && ! is_numeric($value)) {
return (string)__('Empty');
}

Expand Down