Skip to content

Commit 95ff027

Browse files
MC-3029: Update Reader and Generate Allowed Parents List
- modified code and removed method that was not needed
1 parent a3d95d4 commit 95ff027

File tree

1 file changed

+42
-71
lines changed
  • app/code/Magento/PageBuilder/Model/Config/ContentType

1 file changed

+42
-71
lines changed

app/code/Magento/PageBuilder/Model/Config/ContentType/Converter.php

Lines changed: 42 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public function convert($source): array
5050
private function convertTypes(\DOMDocument $source): array
5151
{
5252
$typesData = [];
53+
$parentChildData = [];
5354
/** @var \DOMNodeList $contentTypes */
5455
$contentTypes = $source->getElementsByTagName('type');
5556
/** @var \DOMNode $contentType */
@@ -67,12 +68,12 @@ private function convertTypes(\DOMDocument $source): array
6768
} elseif ('additional_data' === $childNode->nodeName) {
6869
$typesData[$name][$childNode->nodeName] = $this->convertAdditionalData($childNode);
6970
} elseif ('parents' === $childNode->nodeName) {
70-
$typesData[$name][$childNode->nodeName] = [
71+
$parentChildData[$name][$childNode->nodeName] = [
7172
'defaultPolicy' => $this->getAttributeValue($childNode, 'default_policy'),
7273
'types' => $this->convertParentChildData($childNode, 'parent')
7374
];
7475
} elseif ('children' === $childNode->nodeName) {
75-
$typesData[$name][$childNode->nodeName] = [
76+
$parentChildData[$name][$childNode->nodeName] = [
7677
'defaultPolicy' => $this->getAttributeValue($childNode, 'default_policy'),
7778
'types' => $this->convertParentChildData($childNode, 'child')
7879
];
@@ -87,9 +88,9 @@ private function convertTypes(\DOMDocument $source): array
8788
return (int)$firstElement['sortOrder'] <=> (int)$secondElement['sortOrder'];
8889
});
8990

90-
$this->convertParentChildDataToAllowedParents($typesData);
91+
$allowedParents = $this->convertParentChildDataToAllowedParents(array_keys($typesData), $parentChildData);
9192

92-
return $typesData;
93+
return array_merge_recursive($typesData, $allowedParents);
9394
}
9495

9596
/**
@@ -448,42 +449,43 @@ private function convertParentChildData(\DOMElement $elementNode, string $tagNam
448449
{
449450
$data = [];
450451
foreach ($elementNode->getElementsByTagName($tagName) as $node) {
451-
$data[] = [
452-
'name' => $node->attributes->getNamedItem('name')->nodeValue,
453-
'policy' => $node->attributes->getNamedItem('policy')->nodeValue
454-
];
452+
$name = $node->attributes->getNamedItem('name')->nodeValue;
453+
$data[$node->attributes->getNamedItem('policy')->nodeValue][] = $name;
455454
}
456455
return $data;
457456
}
458457

459458
/**
460459
* Convert parent and child data to allowed parents
461460
*
462-
* @param array $typesData
461+
* @param array $types
462+
* @param array $parentChildData
463+
* @return array
463464
*/
464-
private function convertParentChildDataToAllowedParents(array &$typesData)
465+
private function convertParentChildDataToAllowedParents(array $types, array $parentChildData): array
465466
{
466-
// get an array of all content type names
467-
$types = array_keys($typesData);
467+
$allowedParentsData = [];
468468

469469
// convert children
470-
$allowedParents = $this->convertChildrenToAllowedParents($typesData, $types);
471-
foreach ($typesData as $key => &$typeData) {
472-
$typeData['allowed_parents'] = $allowedParents[$key];
470+
$allowedParents = $this->convertChildrenToAllowedParents($parentChildData, $types);
471+
foreach ($types as $type) {
472+
$allowedParentsData[$type]['allowed_parents'] = $allowedParents[$type];
473473
}
474474

475475
// convert parents
476-
$typesData = $this->convertParentsToAllowedParents($typesData, $types);
476+
$allowedParentsData = $this->convertParentsToAllowedParents($parentChildData, $types, $allowedParentsData);
477+
478+
return $allowedParentsData;
477479
}
478480

479481
/**
480482
* Convert children data to allow parents
481483
*
482-
* @param array $typesData
483-
* @param array $allowedParents
484+
* @param array $parentChildData
485+
* @param array $types
484486
* @return array
485487
*/
486-
private function convertChildrenToAllowedParents(array $typesData, array $types): array
488+
private function convertChildrenToAllowedParents(array $parentChildData, array $types): array
487489
{
488490
$allowedParents = [];
489491

@@ -492,30 +494,20 @@ private function convertChildrenToAllowedParents(array $typesData, array $types)
492494
$allowedParents[$type] = [];
493495
}
494496

495-
foreach ($typesData as $key => $value) {
497+
foreach ($parentChildData as $key => $value) {
496498
$children = $value['children'] ?? [];
497499

498500
if (empty($children)) {
499501
continue;
500502
}
501503

502-
if ($children['defaultPolicy'] === 'deny') {
503-
$allow = $this->getContentTypesByPolicy($children['types'], 'allow');
504-
foreach ($allowedParents as $type => $parents) {
505-
if (!in_array($type, $allow)) {
506-
$allowedParents[$type] = $this->removeDataInArray($key, $parents);
507-
} else {
508-
$allowedParents[$type][] = $key;
509-
}
510-
}
511-
} else {
512-
$deny = $this->getContentTypesByPolicy($children['types'], 'deny');
513-
foreach ($allowedParents as $type => $parents) {
514-
if (in_array($type, $deny)) {
515-
$allowedParents[$type] = $this->removeDataInArray($key, $parents);
516-
} else {
517-
$allowedParents[$type][] = $key;
518-
}
504+
foreach ($allowedParents as $type => $parents) {
505+
$typeAllowed = in_array($type, $children['types']['allow'] ?? []);
506+
$typeDenied = in_array($type, $children['types']['deny'] ?? []);
507+
if (($children['defaultPolicy'] === 'deny' && !$typeAllowed) || $typeDenied) {
508+
$allowedParents[$type] = $this->removeDataInArray($key, $parents);
509+
} else {
510+
$allowedParents[$type][] = $key;
519511
}
520512
}
521513
}
@@ -526,56 +518,35 @@ private function convertChildrenToAllowedParents(array $typesData, array $types)
526518
/**
527519
* Convert parents data to allowed parents
528520
*
529-
* @param array $typesData
521+
* @param array $parentChildData
530522
* @param array $types
523+
* @param array $allowedParentsData
531524
* @return array
532525
*/
533-
private function convertParentsToAllowedParents(array $typesData, array $types): array
526+
private function convertParentsToAllowedParents(array $parentChildData, array $types, array $allowedParentsData): array
534527
{
535-
foreach ($typesData as $key => $value) {
528+
foreach ($parentChildData as $key => $value) {
536529
$parent = $value['parents'] ?? [];
537530

538531
if (empty($parent)) {
539532
continue;
540533
}
541534

535+
$allowedTypes = $parent['types']['allow'] ?? [];
536+
$deniedTypes = $parent['types']['deny'] ?? [];
537+
542538
if ($parent['defaultPolicy'] === 'deny') {
543-
$allowedParents = $this->getContentTypesByPolicy($parent['types'], 'allow');
539+
$allowedParents = $allowedTypes;
544540
} else {
545-
$allowedParents = $types;
546-
foreach ($parent['types'] as $type) {
547-
if ($type['policy'] === 'deny') {
548-
$allowedParents = $this->removeDataInArray($type['name'], $allowedParents);
549-
} else {
550-
$allowedParents = array_merge(
551-
$allowedParents,
552-
$this->getContentTypesByPolicy($parent['types'], 'allow')
553-
);
554-
}
541+
$allowedParents = array_merge($types, $allowedTypes);
542+
foreach ($deniedTypes as $type) {
543+
$allowedParents = $this->removeDataInArray($type, $allowedParents);
555544
}
556545
}
557-
$typesData[$key]['allowed_parents'] = $allowedParents;
546+
$allowedParentsData[$key]['allowed_parents'] = $allowedParents;
558547
}
559548

560-
return $typesData;
561-
}
562-
563-
/**
564-
* Get an array of content type names by policy (allow or deny)
565-
*
566-
* @param array $contentTypes
567-
* @param string $policy
568-
* @return array
569-
*/
570-
private function getContentTypesByPolicy(array $contentTypes, string $policy): array
571-
{
572-
$data = [];
573-
foreach ($contentTypes as $type) {
574-
if ($type['policy'] === $policy) {
575-
$data[] = $type['name'];
576-
}
577-
}
578-
return $data;
549+
return $allowedParentsData;
579550
}
580551

581552
/**

0 commit comments

Comments
 (0)