|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace a9f\Typo3Fractor\TYPO3v10\Yaml; |
| 6 | + |
| 7 | +use a9f\FractorYaml\Contract\YamlFractorRule; |
| 8 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 9 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 10 | + |
| 11 | +final class EmailFinisherFractor implements YamlFractorRule |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @var string |
| 15 | + */ |
| 16 | + private const FINISHERS = 'finishers'; |
| 17 | + |
| 18 | + /** |
| 19 | + * @var string |
| 20 | + */ |
| 21 | + private const OPTIONS = 'options'; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var string |
| 25 | + */ |
| 26 | + private const RECIPIENT_ADDRESS = 'recipientAddress'; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var string |
| 30 | + */ |
| 31 | + private const RECIPIENTS = 'recipients'; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var string |
| 35 | + */ |
| 36 | + private const VARIANTS = 'variants'; |
| 37 | + |
| 38 | + public function getRuleDefinition(): RuleDefinition |
| 39 | + { |
| 40 | + return new RuleDefinition('Convert single recipient values to array for EmailFinisher', [ |
| 41 | + new CodeSample( |
| 42 | + <<<'CODE_SAMPLE' |
| 43 | +finishers: |
| 44 | + - |
| 45 | + options: |
| 46 | + recipientAddress: [email protected] |
| 47 | + recipientName: 'Bar' |
| 48 | +CODE_SAMPLE |
| 49 | + , |
| 50 | + <<<'CODE_SAMPLE' |
| 51 | +finishers: |
| 52 | + - |
| 53 | + options: |
| 54 | + recipients: |
| 55 | + |
| 56 | +CODE_SAMPLE |
| 57 | + ), |
| 58 | + ]); |
| 59 | + } |
| 60 | + |
| 61 | + public function refactor(array $yaml): array |
| 62 | + { |
| 63 | + if (array_key_exists(self::FINISHERS, $yaml)) { |
| 64 | + $this->refactorFinishers($yaml[self::FINISHERS], $yaml); |
| 65 | + } |
| 66 | + |
| 67 | + if (array_key_exists(self::VARIANTS, $yaml)) { |
| 68 | + foreach ($yaml[self::VARIANTS] as $variantKey => $variant) { |
| 69 | + if (!array_key_exists(self::FINISHERS, $variant)) { |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + $this->refactorFinishers( |
| 74 | + $variant[self::FINISHERS], |
| 75 | + $yaml[self::VARIANTS][$variantKey] |
| 76 | + ); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return $yaml; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @param mixed[] $finishers |
| 85 | + * @param mixed[] $yamlToModify |
| 86 | + */ |
| 87 | + private function refactorFinishers(array $finishers, array &$yamlToModify): void |
| 88 | + { |
| 89 | + foreach ($finishers as $finisherKey => $finisher) { |
| 90 | + if (!array_key_exists('identifier', $finisher)) { |
| 91 | + continue; |
| 92 | + } |
| 93 | + |
| 94 | + if (!in_array($finisher['identifier'], ['EmailToSender', 'EmailToReceiver'], true)) { |
| 95 | + continue; |
| 96 | + } |
| 97 | + |
| 98 | + if (!array_key_exists(self::OPTIONS, $finisher)) { |
| 99 | + continue; |
| 100 | + } |
| 101 | + |
| 102 | + $recipients = []; |
| 103 | + foreach ((array) $finisher[self::OPTIONS] as $optionKey => $optionValue) { |
| 104 | + if (!in_array( |
| 105 | + $optionKey, |
| 106 | + [ |
| 107 | + 'replyToAddress', |
| 108 | + 'carbonCopyAddress', |
| 109 | + 'blindCarbonCopyAddress', |
| 110 | + self::RECIPIENT_ADDRESS, |
| 111 | + 'recipientName', |
| 112 | + ], |
| 113 | + true |
| 114 | + )) { |
| 115 | + continue; |
| 116 | + } |
| 117 | + |
| 118 | + if ($optionKey === 'replyToAddress') { |
| 119 | + $yamlToModify[self::FINISHERS][$finisherKey][self::OPTIONS]['replyToRecipients'][] = $optionValue; |
| 120 | + } elseif ($optionKey === 'carbonCopyAddress') { |
| 121 | + $yamlToModify[self::FINISHERS][$finisherKey][self::OPTIONS]['carbonCopyRecipients'][] = $optionValue; |
| 122 | + } elseif ($optionKey === 'blindCarbonCopyAddress') { |
| 123 | + $yamlToModify[self::FINISHERS][$finisherKey][self::OPTIONS]['blindCarbonCopyRecipients'][] = $optionValue; |
| 124 | + } |
| 125 | + |
| 126 | + unset( |
| 127 | + $yamlToModify[self::FINISHERS][$finisherKey][self::OPTIONS][$optionKey] |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + if (isset($finisher[self::OPTIONS][self::RECIPIENT_ADDRESS])) { |
| 132 | + $recipients[$finisher[self::OPTIONS][self::RECIPIENT_ADDRESS]] = $finisher[self::OPTIONS]['recipientName'] ?: ''; |
| 133 | + } |
| 134 | + |
| 135 | + if (isset($finisher[self::OPTIONS][self::RECIPIENTS])) { |
| 136 | + $yamlToModify[self::FINISHERS][$finisherKey][self::OPTIONS][self::RECIPIENTS] = array_merge( |
| 137 | + $recipients, |
| 138 | + $yamlToModify[self::FINISHERS][$finisherKey][self::OPTIONS][self::RECIPIENTS] |
| 139 | + ); |
| 140 | + } else { |
| 141 | + $yamlToModify[self::FINISHERS][$finisherKey][self::OPTIONS][self::RECIPIENTS] = $recipients; |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments