Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
22.00% |
22 / 100 |
|
16.67% |
3 / 18 |
CRAP | |
0.00% |
0 / 1 |
| MailTemplates | |
22.00% |
22 / 100 |
|
16.67% |
3 / 18 |
549.79 | |
0.00% |
0 / 1 |
| readEntity | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| readList | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| readListWithoutProvider | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| readListByProvider | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| readCustomizedListForProvider | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| readTemplate | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| readTemplateById | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| deleteTemplateById | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| updateTemplateContent | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| updateTemplateContentById | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| createCustomizationForProvider | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| updateEntity | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
56 | |||
| readProperty | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| replaceProperty | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| deleteProperty | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| fetchData | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| getSpecifiedValue | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| mergeMailTemplatesWithCustomizations | |
81.82% |
9 / 11 |
|
0.00% |
0 / 1 |
6.22 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BO\Zmsbackend\Mail\Service; |
| 4 | |
| 5 | use BO\Zmsentities\Mailtemplate as Mailtemplate; |
| 6 | |
| 7 | class MailTemplates extends \BO\Zmsbackend\Base |
| 8 | { |
| 9 | /** |
| 10 | * |
| 11 | * @return \BO\Zmsentities\Mailtemplate |
| 12 | */ |
| 13 | public function readEntity() |
| 14 | { |
| 15 | $query = \BO\Zmsbackend\Mail\Repository\Mailtemplate::QUERY_SELECT; |
| 16 | $config = $this->fetchData($query); |
| 17 | return $config; |
| 18 | } |
| 19 | |
| 20 | |
| 21 | /** |
| 22 | * @psalm-api |
| 23 | */ |
| 24 | public function readList(): \BO\Zmsentities\Collection\MailtemplateList |
| 25 | { |
| 26 | $query = new \BO\Zmsbackend\Mail\Repository\Mailtemplate(\BO\Zmsbackend\Query\Base::SELECT); |
| 27 | $query->addEntityMapping(); |
| 28 | $logList = new \BO\Zmsentities\Collection\MailtemplateList($this->fetchList($query, new Mailtemplate())); |
| 29 | return $logList; |
| 30 | } |
| 31 | |
| 32 | public function readListWithoutProvider(): \BO\Zmsentities\Collection\MailtemplateList |
| 33 | { |
| 34 | $query = new \BO\Zmsbackend\Mail\Repository\Mailtemplate(\BO\Zmsbackend\Query\Base::SELECT); |
| 35 | $query->addEntityMapping(); |
| 36 | $query->addConditionWithoutProvider(); |
| 37 | $logList = new \BO\Zmsentities\Collection\MailtemplateList($this->fetchList($query, new Mailtemplate())); |
| 38 | return $logList; |
| 39 | } |
| 40 | |
| 41 | public function readListByProvider($providerId): \BO\Zmsentities\Collection\MailtemplateList |
| 42 | { |
| 43 | $query = new \BO\Zmsbackend\Mail\Repository\Mailtemplate(\BO\Zmsbackend\Query\Base::SELECT); |
| 44 | $query->addEntityMapping(); |
| 45 | $query->addConditionProviderId($providerId); |
| 46 | $logList = new \BO\Zmsentities\Collection\MailtemplateList($this->fetchList($query, new Mailtemplate())); |
| 47 | return $logList; |
| 48 | } |
| 49 | |
| 50 | public function readCustomizedListForProvider($providerId) |
| 51 | { |
| 52 | $generalTemplates = $this->readListWithoutProvider(); |
| 53 | $customTemplates = $this->readListByProvider($providerId); |
| 54 | return $this->mergeMailTemplatesWithCustomizations($generalTemplates, $customTemplates); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | public function readTemplate($templateName): Mailtemplate |
| 59 | { |
| 60 | $query = new \BO\Zmsbackend\Mail\Repository\Mailtemplate(\BO\Zmsbackend\Query\Base::SELECT); |
| 61 | $query->addEntityMapping() |
| 62 | ->addConditionName($templateName); |
| 63 | return $this->fetchOne($query, new Mailtemplate()); |
| 64 | } |
| 65 | |
| 66 | public function readTemplateById($templateId): Mailtemplate |
| 67 | { |
| 68 | $query = new \BO\Zmsbackend\Mail\Repository\Mailtemplate(\BO\Zmsbackend\Query\Base::SELECT); |
| 69 | $query->addEntityMapping() |
| 70 | ->addConditionId($templateId); |
| 71 | return $this->fetchOne($query, new Mailtemplate()); |
| 72 | } |
| 73 | |
| 74 | public function deleteTemplateById($templateId): bool |
| 75 | { |
| 76 | $query = new \BO\Zmsbackend\Mail\Repository\Mailtemplate(\BO\Zmsbackend\Query\Base::DELETE); |
| 77 | $query->addConditionId($templateId); |
| 78 | return $this->deleteItem($query); |
| 79 | } |
| 80 | |
| 81 | /** @psalm-api */ |
| 82 | public function updateTemplateContent($templateName, $templateContent) |
| 83 | { |
| 84 | $query = new \BO\Zmsbackend\Mail\Repository\Mailtemplate(\BO\Zmsbackend\Query\Base::UPDATE); |
| 85 | $query->addConditionName($templateName); |
| 86 | $query->addTemplateContent($templateContent); |
| 87 | $this->writeItem($query); |
| 88 | return $this->readTemplate($templateName); |
| 89 | //return $this->readEntity($templateName, 1); |
| 90 | } |
| 91 | |
| 92 | public function updateTemplateContentById($templateId, $templateContent) |
| 93 | { |
| 94 | $query = new \BO\Zmsbackend\Mail\Repository\Mailtemplate(\BO\Zmsbackend\Query\Base::UPDATE); |
| 95 | $query->addConditionId($templateId); |
| 96 | $query->addTemplateContent($templateContent); |
| 97 | $this->writeItem($query); |
| 98 | return $this->readTemplateById($templateId); |
| 99 | //return $this->readEntity($templateName, 1); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | public function createCustomizationForProvider($providerId, $templateName, $templateContent) |
| 104 | { |
| 105 | $query = new \BO\Zmsbackend\Mail\Repository\Mailtemplate(\BO\Zmsbackend\Query\Base::INSERT); |
| 106 | $query->addValues(array( |
| 107 | 'name' => $templateName, |
| 108 | 'value' => $templateContent, |
| 109 | 'provider' => $providerId, |
| 110 | 'changeTimestamp' => (new \DateTimeImmutable())->format('Y-m-d H:i:s') |
| 111 | )); |
| 112 | $this->writeItem($query); |
| 113 | return $this->readTemplate($templateName); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * @psalm-api |
| 118 | */ |
| 119 | public function updateEntity(MailTemplate $config): Mailtemplate|null |
| 120 | { |
| 121 | $compareEntity = $this->readEntity(); |
| 122 | $result = false; |
| 123 | $query = new \BO\Zmsbackend\Mail\Repository\Mailtemplate(\BO\Zmsbackend\Query\Base::REPLACE); |
| 124 | foreach ($config as $key => $item) { |
| 125 | if (is_array($item)) { |
| 126 | foreach ($item as $itemName => $itemValue) { |
| 127 | if ($itemValue && $compareEntity->getPreference($key, $itemName) != $itemValue) { |
| 128 | $query->addValues(array( |
| 129 | 'name' => $key . '__' . $itemName, |
| 130 | 'value' => $this->getSpecifiedValue($itemValue), |
| 131 | 'changeTimestamp' => (new \DateTimeImmutable())->format('Y-m-d H:i:s') |
| 132 | )); |
| 133 | $result = $this->writeItem($query); |
| 134 | } |
| 135 | } |
| 136 | } else { |
| 137 | $query->addValues(array( |
| 138 | 'name' => $key, |
| 139 | 'value' => $this->getSpecifiedValue($item), |
| 140 | 'changeTimestamp' => (new \DateTimeImmutable())->format('Y-m-d H:i:s') |
| 141 | )); |
| 142 | $result = $this->writeItem($query); |
| 143 | } |
| 144 | } |
| 145 | return ($result) ? $this->readEntity() : null; |
| 146 | } |
| 147 | |
| 148 | /** @psalm-api */ |
| 149 | public function readProperty($property, $forUpdate = false) |
| 150 | { |
| 151 | $sql = \BO\Zmsbackend\Mail\Repository\Mailtemplate::QUERY_SELECT_PROPERTY; |
| 152 | if ($forUpdate) { |
| 153 | $sql .= " FOR UPDATE"; |
| 154 | } |
| 155 | return $this->fetchValue($sql, [$property]); |
| 156 | } |
| 157 | |
| 158 | /** @psalm-api */ |
| 159 | public function replaceProperty($property, $value) |
| 160 | { |
| 161 | return $this->perform(\BO\Zmsbackend\Mail\Repository\Mailtemplate::QUERY_REPLACE_PROPERTY, [ |
| 162 | 'property' => $property, |
| 163 | 'value' => $value, |
| 164 | ]); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * remove config data |
| 169 | * |
| 170 | * |
| 171 | * @return bool |
| 172 | * @psalm-api |
| 173 | */ |
| 174 | public function deleteProperty($property) |
| 175 | { |
| 176 | $query = new \BO\Zmsbackend\Mail\Repository\Mailtemplate(\BO\Zmsbackend\Query\Base::DELETE); |
| 177 | $query->addConditionName($property); |
| 178 | return $this->deleteItem($query); |
| 179 | } |
| 180 | |
| 181 | protected function fetchData(string $querySql): Mailtemplate |
| 182 | { |
| 183 | $splittedHash = array(); |
| 184 | $dataList = $this->getReader()->fetchAll($querySql); |
| 185 | foreach ($dataList as $data) { |
| 186 | $splittedHash[$data['name']] = $data['value']; |
| 187 | } |
| 188 | |
| 189 | return new Mailtemplate($splittedHash); |
| 190 | } |
| 191 | |
| 192 | protected function getSpecifiedValue($value): int|string |
| 193 | { |
| 194 | if (is_bool($value)) { |
| 195 | return ($value) ? 1 : 0; |
| 196 | } |
| 197 | return trim($value); |
| 198 | } |
| 199 | |
| 200 | protected function mergeMailTemplatesWithCustomizations($generalTemplates, $customTemplates): array |
| 201 | { |
| 202 | |
| 203 | $customTemplatesByName = []; |
| 204 | |
| 205 | if ($customTemplates) { |
| 206 | foreach ($customTemplates as $template) { |
| 207 | $customTemplatesByName[$template['name']] = $template; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | $mergedTemplates = []; |
| 212 | |
| 213 | if ($generalTemplates) { |
| 214 | foreach ($generalTemplates as $template) { |
| 215 | if (isset($customTemplatesByName[$template['name']])) { |
| 216 | $mergedTemplates[] = $customTemplatesByName[$template['name']]; |
| 217 | } else { |
| 218 | $mergedTemplates[] = $template; |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | return $mergedTemplates; |
| 224 | } |
| 225 | } |