| Total Complexity | 44 |
| Total Lines | 342 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like BackendLayout often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BackendLayout, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class BackendLayout |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $identifier; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | protected $title; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | protected $description; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $iconPath; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | protected $configuration; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | protected $configurationArray; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | protected $data; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var DrawingConfiguration |
||
| 70 | */ |
||
| 71 | protected $drawingConfiguration; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var ContentFetcher |
||
| 75 | */ |
||
| 76 | protected $contentFetcher; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var LanguageColumn |
||
| 80 | */ |
||
| 81 | protected $languageColumns = []; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var RecordRememberer |
||
| 85 | */ |
||
| 86 | protected $recordRememberer; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param string $identifier |
||
| 90 | * @param string $title |
||
| 91 | * @param string|array $configuration |
||
| 92 | * @return BackendLayout |
||
| 93 | */ |
||
| 94 | public static function create($identifier, $title, $configuration) |
||
| 95 | { |
||
| 96 | return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance( |
||
| 97 | static::class, |
||
| 98 | $identifier, |
||
|
|
|||
| 99 | $title, |
||
| 100 | $configuration |
||
| 101 | ); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param string $identifier |
||
| 106 | * @param string $title |
||
| 107 | * @param string|array $configuration |
||
| 108 | */ |
||
| 109 | public function __construct($identifier, $title, $configuration) |
||
| 110 | { |
||
| 111 | $this->drawingConfiguration = GeneralUtility::makeInstance(DrawingConfiguration::class); |
||
| 112 | $this->contentFetcher = GeneralUtility::makeInstance(ContentFetcher::class, $this); |
||
| 113 | $this->recordRememberer = GeneralUtility::makeInstance(RecordRememberer::class); |
||
| 114 | $this->setIdentifier($identifier); |
||
| 115 | $this->setTitle($title); |
||
| 116 | if (is_array($configuration)) { |
||
| 117 | $this->setConfigurationArray($configuration); |
||
| 118 | } else { |
||
| 119 | $this->setConfiguration($configuration); |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @return string |
||
| 125 | */ |
||
| 126 | public function getIdentifier() |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param string $identifier |
||
| 133 | * @throws \UnexpectedValueException |
||
| 134 | */ |
||
| 135 | public function setIdentifier($identifier) |
||
| 136 | { |
||
| 137 | if (strpos($identifier, '__') !== false) { |
||
| 138 | throw new \UnexpectedValueException( |
||
| 139 | 'Identifier "' . $identifier . '" must not contain "__"', |
||
| 140 | 1381597630 |
||
| 141 | ); |
||
| 142 | } |
||
| 143 | |||
| 144 | $this->identifier = $identifier; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @return string |
||
| 149 | */ |
||
| 150 | public function getTitle() |
||
| 151 | { |
||
| 152 | return $this->title; |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param string $title |
||
| 157 | */ |
||
| 158 | public function setTitle($title) |
||
| 159 | { |
||
| 160 | $this->title = $title; |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @return string |
||
| 165 | */ |
||
| 166 | public function getDescription() |
||
| 167 | { |
||
| 168 | return $this->description; |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @param string $description |
||
| 173 | */ |
||
| 174 | public function setDescription($description) |
||
| 175 | { |
||
| 176 | $this->description = $description; |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @return string |
||
| 181 | */ |
||
| 182 | public function getIconPath() |
||
| 183 | { |
||
| 184 | return $this->iconPath; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param string $iconPath |
||
| 189 | */ |
||
| 190 | public function setIconPath($iconPath) |
||
| 191 | { |
||
| 192 | $this->iconPath = $iconPath; |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @return string |
||
| 197 | */ |
||
| 198 | public function getConfiguration() |
||
| 199 | { |
||
| 200 | return $this->configuration; |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param array $configurationArray |
||
| 205 | */ |
||
| 206 | public function setConfigurationArray(array $configurationArray): void |
||
| 207 | { |
||
| 208 | if (!isset($configurationArray['__colPosList'], $configurationArray['__items'])) { |
||
| 209 | // Backend layout configuration is unprocessed, process it now to extract counts and column item lists |
||
| 210 | $colPosList = []; |
||
| 211 | $items = []; |
||
| 212 | $rowIndex = 0; |
||
| 213 | foreach ($configurationArray['backend_layout.']['rows.'] as $row) { |
||
| 214 | $index = 0; |
||
| 215 | $colCount = 0; |
||
| 216 | $columns = []; |
||
| 217 | foreach ($row['columns.'] as $column) { |
||
| 218 | if (!isset($column['colPos'])) { |
||
| 219 | continue; |
||
| 220 | } |
||
| 221 | $colPos = $column['colPos']; |
||
| 222 | $colPos = (int)$colPos; |
||
| 223 | $colPosList[$colPos] = $colPos; |
||
| 224 | $key = ($index + 1) . '.'; |
||
| 225 | $columns[$key] = $column; |
||
| 226 | $items[$colPos] = [ |
||
| 227 | (string)$this->getLanguageService()->sL($column['name']), |
||
| 228 | $colPos, |
||
| 229 | $column['icon'] |
||
| 230 | ]; |
||
| 231 | $colCount += $column['colspan'] ? $column['colspan'] : 1; |
||
| 232 | ++ $index; |
||
| 233 | } |
||
| 234 | ++ $rowIndex; |
||
| 235 | } |
||
| 236 | |||
| 237 | $configurationArray['__config'] = $configurationArray; |
||
| 238 | $configurationArray['__colPosList'] = $colPosList; |
||
| 239 | $configurationArray['__items'] = $items; |
||
| 240 | } |
||
| 241 | $this->configurationArray = $configurationArray; |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @return array |
||
| 246 | */ |
||
| 247 | public function getConfigurationArray(): array |
||
| 248 | { |
||
| 249 | return $this->configurationArray; |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param string $configuration |
||
| 254 | */ |
||
| 255 | public function setConfiguration($configuration) |
||
| 256 | { |
||
| 257 | $this->configuration = $configuration; |
||
| 258 | $this->parseConfigurationStringAndSetConfigurationArray($configuration); |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @return array |
||
| 263 | */ |
||
| 264 | public function getData() |
||
| 265 | { |
||
| 266 | return $this->data; |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @param array $data |
||
| 271 | */ |
||
| 272 | public function setData(array $data) |
||
| 273 | { |
||
| 274 | $this->data = $data; |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @return LanguageColumn[] |
||
| 279 | */ |
||
| 280 | public function getLanguageColumns(): iterable |
||
| 281 | { |
||
| 282 | if (empty($this->languageColumns)) { |
||
| 283 | $defaultLanguageElements = []; |
||
| 284 | $contentByColumn = $this->getContentFetcher()->getContentRecordsPerColumn(null, 0); |
||
| 285 | if (!empty($contentByColumn)) { |
||
| 286 | $defaultLanguageElements = array_merge(...$contentByColumn); |
||
| 287 | } |
||
| 288 | foreach ($this->getDrawingConfiguration()->getSiteLanguages() as $siteLanguage) { |
||
| 289 | if (!in_array($siteLanguage->getLanguageId(), $this->getDrawingConfiguration()->getLanguageColumns())) { |
||
| 290 | continue; |
||
| 291 | } |
||
| 292 | $backendLayout = clone $this; |
||
| 293 | $backendLayout->getDrawingConfiguration()->setLanguageColumnsPointer($siteLanguage->getLanguageId()); |
||
| 294 | $this->languageColumns[] = GeneralUtility::makeInstance(LanguageColumn::class, $backendLayout, $siteLanguage, $defaultLanguageElements); |
||
| 295 | } |
||
| 296 | } |
||
| 297 | return $this->languageColumns; |
||
| 298 | } |
||
| 299 | |||
| 300 | public function getGrid(): Grid |
||
| 301 | { |
||
| 302 | $grid = GeneralUtility::makeInstance(Grid::class, $this); |
||
| 303 | foreach ($this->getConfigurationArray()['__config']['backend_layout.']['rows.'] as $row) { |
||
| 304 | $rowObject = GeneralUtility::makeInstance(GridRow::class, $this); |
||
| 305 | foreach ($row['columns.'] as $column) { |
||
| 306 | $columnObject = GeneralUtility::makeInstance(GridColumn::class, $this, $column); |
||
| 307 | $rowObject->addColumn($columnObject); |
||
| 308 | } |
||
| 309 | $grid->addRow($rowObject); |
||
| 310 | } |
||
| 311 | $allowInconsistentLanguageHandling = (bool)(BackendUtility::getPagesTSconfig($this->id)['mod.']['web_layout.']['allowInconsistentLanguageHandling'] ?? false); |
||
| 312 | if (!$allowInconsistentLanguageHandling && $this->getLanguageModeIdentifier() === 'connected') { |
||
| 313 | $grid->setAllowNewContent(false); |
||
| 314 | } |
||
| 315 | return $grid; |
||
| 316 | } |
||
| 317 | |||
| 318 | public function getColumnPositionNumbers(): array |
||
| 319 | { |
||
| 320 | return $this->getConfigurationArray()['__colPosList']; |
||
| 321 | } |
||
| 322 | |||
| 323 | public function getContentFetcher(): ContentFetcher |
||
| 324 | { |
||
| 325 | return $this->contentFetcher; |
||
| 326 | } |
||
| 327 | |||
| 328 | public function setContentFetcher(ContentFetcher $contentFetcher): void |
||
| 331 | } |
||
| 332 | |||
| 333 | public function getDrawingConfiguration(): DrawingConfiguration |
||
| 334 | { |
||
| 335 | return $this->drawingConfiguration; |
||
| 336 | } |
||
| 337 | |||
| 338 | public function getBackendLayoutRenderer(): BackendLayoutRenderer |
||
| 339 | { |
||
| 340 | return GeneralUtility::makeInstance(BackendLayoutRenderer::class, $this); |
||
| 341 | } |
||
| 342 | |||
| 343 | public function getRecordRememberer(): RecordRememberer |
||
| 344 | { |
||
| 345 | return $this->recordRememberer; |
||
| 346 | } |
||
| 347 | |||
| 348 | public function getLanguageModeIdentifier(): string |
||
| 349 | { |
||
| 350 | $contentRecordsPerColumn = $this->contentFetcher->getContentRecordsPerColumn(null, $this->drawingConfiguration->getLanguageColumnsPointer()); |
||
| 351 | $contentRecords = empty($contentRecordsPerColumn) ? [] : array_merge(...$contentRecordsPerColumn); |
||
| 352 | $translationData = $this->contentFetcher->getTranslationData($contentRecords, $this->drawingConfiguration->getLanguageColumnsPointer()); |
||
| 353 | return $translationData['mode'] ?? ''; |
||
| 354 | } |
||
| 355 | |||
| 356 | protected function parseConfigurationStringAndSetConfigurationArray(string $configuration): void |
||
| 357 | { |
||
| 358 | $parser = GeneralUtility::makeInstance(TypoScriptParser::class); |
||
| 359 | $conditionMatcher = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher::class); |
||
| 360 | $parser->parse(TypoScriptParser::checkIncludeLines($configuration), $conditionMatcher); |
||
| 361 | $this->setConfigurationArray($parser->setup); |
||
| 362 | } |
||
| 363 | |||
| 364 | public function __clone() |
||
| 365 | { |
||
| 366 | $this->drawingConfiguration = clone $this->drawingConfiguration; |
||
| 367 | $this->contentFetcher->setBackendLayout($this); |
||
| 368 | } |
||
| 369 | |||
| 370 | protected function getLanguageService(): LanguageService |
||
| 373 | } |
||
| 374 | } |
||
| 375 |