| Total Complexity | 41 |
| Total Lines | 296 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Tx_FeatureFlag_System_Typo3_TCA 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 Tx_FeatureFlag_System_Typo3_TCA, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class Tx_FeatureFlag_System_Typo3_TCA |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | const FIELD_BEHAVIOR = 'tx_featureflag_behavior'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | const FIELD_FLAG = 'tx_featureflag_flag'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var Tx_FeatureFlag_Domain_Repository_FeatureFlag |
||
| 45 | */ |
||
| 46 | protected $featureFlagRepository; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var \TYPO3\CMS\Extbase\Object\ObjectManager |
||
| 50 | */ |
||
| 51 | protected $objectManager; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager |
||
| 55 | */ |
||
| 56 | protected $persistenceManager; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var \TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 60 | */ |
||
| 61 | protected static $hashedMappings; |
||
| 62 | |||
| 63 | |||
| 64 | /** |
||
| 65 | * @param array $PA |
||
| 66 | * @return string |
||
| 67 | */ |
||
| 68 | public function renderSelectForFlag(array $PA) |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param array $PA |
||
| 94 | * @return string |
||
| 95 | */ |
||
| 96 | public function renderInfo(array $PA) |
||
|
|
|||
| 97 | { |
||
| 98 | $langField = 'LLL:EXT:feature_flag/Resources/Private/Language/locallang_db.xml:tx_featureflag_info.text'; |
||
| 99 | return $this->getLanguageService()->sL($langField); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param array $PA |
||
| 104 | * @return string |
||
| 105 | */ |
||
| 106 | public function renderSelectForBehavior(array $PA) |
||
| 107 | { |
||
| 108 | // check, which behavior is selected |
||
| 109 | $isBehaviorHideSelected = false; |
||
| 110 | $isBehaviorShowSelected = false; |
||
| 111 | $activeMapping = $this->getMappingRepository()->findOneByForeignTableNameAndUid($PA['row']['uid'], $PA['table']); |
||
| 112 | if ($activeMapping instanceof Tx_FeatureFlag_Domain_Model_Mapping) { |
||
| 113 | if ($activeMapping->getBehavior() === Tx_FeatureFlag_Service::BEHAVIOR_HIDE) { |
||
| 114 | $isBehaviorHideSelected = true; |
||
| 115 | } elseif ($activeMapping->getBehavior() === Tx_FeatureFlag_Service::BEHAVIOR_SHOW) { |
||
| 116 | $isBehaviorShowSelected = true; |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | // build select-box |
||
| 121 | $html = ''; |
||
| 122 | $html .= '<select class="select" id="' . $PA['itemFormElID'] . '" name="' . $PA['itemFormElName'] . '">'; |
||
| 123 | $html .= '<option value="' . Tx_FeatureFlag_Service::BEHAVIOR_HIDE . '"' . ($isBehaviorHideSelected ? ' selected="selected"' : '') . |
||
| 124 | '>'; |
||
| 125 | $langField = 'LLL:EXT:feature_flag/Resources/Private/Language/locallang_db.xml:tx_featureflag_behavior.hide'; |
||
| 126 | $html .= $this->getLanguageService()->sL($langField); |
||
| 127 | $html .= '</option>'; |
||
| 128 | $html .= '<option value="' . Tx_FeatureFlag_Service::BEHAVIOR_SHOW . '"' . ($isBehaviorShowSelected ? ' selected="selected"' : '') . |
||
| 129 | '>'; |
||
| 130 | $langField = 'LLL:EXT:feature_flag/Resources/Private/Language/locallang_db.xml:tx_featureflag_behavior.show'; |
||
| 131 | $html .= $this->getLanguageService()->sL($langField); |
||
| 132 | $html .= '</option>'; |
||
| 133 | $html .= '</select>'; |
||
| 134 | |||
| 135 | return $html; |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Hook for updates in Typo3 backend |
||
| 140 | * @param array $incomingFieldArray |
||
| 141 | * @param string $table |
||
| 142 | * @param integer $id |
||
| 143 | * @param \TYPO3\CMS\Core\DataHandling\DataHandler $tceMain |
||
| 144 | * @codingStandardsIgnoreStart |
||
| 145 | */ |
||
| 146 | public function processDatamap_preProcessFieldArray( |
||
| 147 | &$incomingFieldArray, |
||
| 148 | $table, |
||
| 149 | $id, |
||
| 150 | \TYPO3\CMS\Core\DataHandling\DataHandler &$tceMain |
||
| 151 | ) { |
||
| 152 | // @codingStandardsIgnoreEnd |
||
| 153 | if (array_key_exists(self::FIELD_BEHAVIOR, $incomingFieldArray) && array_key_exists(self::FIELD_FLAG, $incomingFieldArray)) { |
||
| 154 | $pid = $tceMain->getPID($table, $id); |
||
| 155 | $this->updateMapping($table, $id, $incomingFieldArray[self::FIELD_FLAG], $pid, $incomingFieldArray[self::FIELD_BEHAVIOR]); |
||
| 156 | unset($incomingFieldArray[self::FIELD_BEHAVIOR]); |
||
| 157 | unset($incomingFieldArray[self::FIELD_FLAG]); |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Hook for deletes in Typo3 Backend. It also delete all overwrite protection |
||
| 163 | * @param string $command |
||
| 164 | * @param string $table |
||
| 165 | * @param integer $id |
||
| 166 | * @codingStandardsIgnoreStart |
||
| 167 | */ |
||
| 168 | public function processCmdmap_postProcess($command, $table, $id) |
||
| 169 | { |
||
| 170 | // @codingStandardsIgnoreEnd |
||
| 171 | if ($command !== 'delete') { |
||
| 172 | return; |
||
| 173 | } |
||
| 174 | $mappings = $this->getMappingRepository()->findAllByForeignTableNameAndUid($id, $table); |
||
| 175 | if (false === is_array($mappings) && false === ($mappings instanceof \TYPO3\CMS\Extbase\Persistence\QueryResultInterface)) { |
||
| 176 | return; |
||
| 177 | } |
||
| 178 | foreach ($mappings as $mapping) { |
||
| 179 | if ($mapping instanceof Tx_FeatureFlag_Domain_Model_Mapping) { |
||
| 180 | $this->getMappingRepository()->remove($mapping); |
||
| 181 | } |
||
| 182 | } |
||
| 183 | $this->getPersistenceManager()->persistAll(); |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @param string $table |
||
| 188 | * @param array $row |
||
| 189 | * @param string $status |
||
| 190 | * @param string $iconName |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | public function postOverlayPriorityLookup($table, $row, &$status, $iconName) |
||
| 194 | { |
||
| 195 | if ($this->isMappingAvailableForTableAndUid($row['uid'], $table)) { |
||
| 196 | $mapping = $this->getMappingRepository()->findOneByForeignTableNameAndUid($row['uid'], $table); |
||
| 197 | if ($mapping instanceof Tx_FeatureFlag_Domain_Model_Mapping) { |
||
| 198 | if ($row['hidden'] === '1') { |
||
| 199 | return 'record-has-feature-flag-which-is-hidden'; |
||
| 200 | } |
||
| 201 | if ($iconName !== '') { |
||
| 202 | // if record is e.g. hidden or protected by FE-group, than show that (e.g. 'hidden' or 'fe_group'-)overlay as default |
||
| 203 | return $iconName; |
||
| 204 | } |
||
| 205 | return 'record-has-feature-flag-which-is-visible'; |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | // return given icon-name as fall-back |
||
| 210 | return $iconName; |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param string $table |
||
| 215 | * @param int $id |
||
| 216 | * @param int $featureFlag |
||
| 217 | * @param int $pid |
||
| 218 | * @param string $behavior |
||
| 219 | */ |
||
| 220 | protected function updateMapping($table, $id, $featureFlag, $pid, $behavior) |
||
| 221 | { |
||
| 222 | $mapping = $this->getMappingRepository()->findOneByForeignTableNameAndUid($id, $table); |
||
| 223 | if ($mapping instanceof Tx_FeatureFlag_Domain_Model_Mapping) { |
||
| 224 | if ('0' === $featureFlag) { |
||
| 225 | $this->getMappingRepository()->remove($mapping); |
||
| 226 | } else { |
||
| 227 | $mapping->setFeatureFlag($this->getFeatureFlagByUid($featureFlag)); |
||
| 228 | $mapping->setBehavior($behavior); |
||
| 229 | } |
||
| 230 | $mapping->setTstamp(time()); |
||
| 231 | $this->getMappingRepository()->update($mapping); |
||
| 232 | } elseif ('0' !== $featureFlag) { |
||
| 233 | /** @var Tx_FeatureFlag_Domain_Model_Mapping $mapping */ |
||
| 234 | $mapping = $this->getObjectManager()->get('Tx_FeatureFlag_Domain_Model_Mapping'); |
||
| 235 | $mapping->setPid($pid); |
||
| 236 | $mapping->setFeatureFlag($this->getFeatureFlagByUid($featureFlag)); |
||
| 237 | $mapping->setForeignTableName($table); |
||
| 238 | $mapping->setForeignTableUid($id); |
||
| 239 | $mapping->setCrdate(time()); |
||
| 240 | $mapping->setTstamp(time()); |
||
| 241 | $mapping->setBehavior($behavior); |
||
| 242 | $this->getMappingRepository()->add($mapping); |
||
| 243 | } |
||
| 244 | $this->getPersistenceManager()->persistAll(); |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @param int $foreignTableUid |
||
| 249 | * @param string $foreignTableName |
||
| 250 | * @return bool |
||
| 251 | */ |
||
| 252 | protected function isMappingAvailableForTableAndUid($foreignTableUid, $foreignTableName) |
||
| 253 | { |
||
| 254 | if (null === self::$hashedMappings) { |
||
| 255 | self::$hashedMappings = $this->getMappingRepository()->getHashedMappings(); |
||
| 256 | } |
||
| 257 | $identifier = sha1($foreignTableUid . '_' . $foreignTableName); |
||
| 258 | if (array_key_exists($identifier, self::$hashedMappings)) { |
||
| 259 | return true; |
||
| 260 | } |
||
| 261 | |||
| 262 | return false; |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param int $uid |
||
| 267 | * @return Tx_FeatureFlag_Domain_Model_FeatureFlag |
||
| 268 | * @throws Tx_FeatureFlag_Service_Exception_FeatureNotFound |
||
| 269 | */ |
||
| 270 | protected function getFeatureFlagByUid($uid) |
||
| 271 | { |
||
| 272 | /** @var Tx_FeatureFlag_Domain_Model_FeatureFlag $featureFlag */ |
||
| 273 | $featureFlag = $this->getFeatureFlagRepository()->findByUid($uid); |
||
| 274 | if (false === ($featureFlag instanceof Tx_FeatureFlag_Domain_Model_FeatureFlag)) { |
||
| 275 | throw new Tx_FeatureFlag_Service_Exception_FeatureNotFound('Feature Flag not found by uid: "' . $uid . '"', 1384340431); |
||
| 276 | } |
||
| 277 | |||
| 278 | return $featureFlag; |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @return Tx_FeatureFlag_Domain_Repository_Mapping |
||
| 283 | */ |
||
| 284 | protected function getMappingRepository() |
||
| 285 | { |
||
| 286 | return $this->getObjectManager()->get('Tx_FeatureFlag_Domain_Repository_Mapping'); |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @return Tx_FeatureFlag_Domain_Repository_FeatureFlag |
||
| 291 | */ |
||
| 292 | protected function getFeatureFlagRepository() |
||
| 293 | { |
||
| 294 | return $this->getObjectManager()->get('Tx_FeatureFlag_Domain_Repository_FeatureFlag'); |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @return \TYPO3\CMS\Extbase\Object\ObjectManager |
||
| 299 | */ |
||
| 300 | protected function getObjectManager() |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @return \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager |
||
| 311 | */ |
||
| 312 | protected function getPersistenceManager() |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @return TYPO3\CMS\Lang\LanguageService |
||
| 323 | */ |
||
| 324 | protected function getLanguageService() |
||
| 327 | } |
||
| 328 | } |
||
| 329 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.