| Total Complexity | 40 |
| Total Lines | 241 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like SetupExporter 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 SetupExporter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class SetupExporter extends ExportBase |
||
| 32 | { |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @inheritDoc |
||
| 36 | */ |
||
| 37 | public static function export(WithId $object) : array { |
||
| 38 | return self::start($object)->get(); |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @inheritDoc |
||
| 43 | */ |
||
| 44 | public static function start(WithId $object) : Export { |
||
| 45 | return new self($object); |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Finish the export query -> get the result |
||
| 50 | * |
||
| 51 | * @return array The query result |
||
| 52 | * @throws Exception |
||
| 53 | */ |
||
| 54 | public function get() : array { |
||
| 55 | $data = $this->getBasic(); |
||
| 56 | $this->applyModifiers($data); |
||
| 57 | return $data; |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @inheritDoc |
||
| 62 | * @throws Exception |
||
| 63 | */ |
||
| 64 | public function getBasic() : array { |
||
| 65 | $data = []; |
||
| 66 | $this->getTournamentData($data); |
||
| 67 | $this->getCategoriesData($data); |
||
| 68 | $this->getRoundsData($data); |
||
| 69 | $this->getGroupsData($data); |
||
| 70 | return $data; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Get all setup information from a Tournament class |
||
| 75 | * |
||
| 76 | * @param array $data |
||
| 77 | * |
||
| 78 | * @throws Exception |
||
| 79 | */ |
||
| 80 | protected function getTournamentData(array &$data) : void { |
||
| 81 | if (!$this->object instanceof Tournament) { |
||
| 82 | return; |
||
| 83 | } |
||
| 84 | $data['tournament'] = (object) [ |
||
| 85 | 'type' => $this->object instanceof Preset ? get_class($this->object) : 'general', |
||
| 86 | 'name' => $this->object->getName(), |
||
|
|
|||
| 87 | 'skip' => $this->object->getSkip(), |
||
| 88 | 'timing' => (object) [ |
||
| 89 | 'play' => $this->object->getPlay(), |
||
| 90 | 'gameWait' => $this->object->getGameWait(), |
||
| 91 | 'categoryWait' => $this->object->getCategoryWait(), |
||
| 92 | 'roundWait' => $this->object->getRoundWait(), |
||
| 93 | 'expectedTime' => $this->object->getTournamentTime(), |
||
| 94 | ], |
||
| 95 | 'categories' => $this->object instanceof WithCategories ? $this->object->queryCategories()->ids()->get() : [], |
||
| 96 | 'rounds' => $this->object instanceof WithRounds ? $this->object->queryRounds()->ids()->get() : [], |
||
| 97 | 'groups' => $this->object instanceof WithGroups ? $this->object->queryGroups()->ids()->get() : [], |
||
| 98 | 'teams' => $this->object instanceof WithTeams ? $this->object->getTeamContainer()->ids()->unique()->get() : [], |
||
| 99 | 'games' => $this->object instanceof WithGames ? $this->object->getGameContainer()->ids()->get() : [], |
||
| 100 | ]; |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Get all setup information for categories |
||
| 105 | * |
||
| 106 | * @param array $data |
||
| 107 | * |
||
| 108 | * @throws Exception |
||
| 109 | */ |
||
| 110 | protected function getCategoriesData(array &$data) : void { |
||
| 111 | if ($this->object instanceof Category) { |
||
| 112 | $data['categories'] = [ |
||
| 113 | $this->object->getId() => $this->getCategoryData($this->object), |
||
| 114 | ]; |
||
| 115 | } |
||
| 116 | elseif ($this->object instanceof WithCategories) { |
||
| 117 | $data['categories'] = []; |
||
| 118 | foreach ($this->object->getCategories() as $category) { |
||
| 119 | $data['categories'][$category->getId()] = $this->getCategoryData($category); |
||
| 120 | } |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Get all setup information from a Category class |
||
| 126 | * |
||
| 127 | * @param Category $category Category class to export |
||
| 128 | * |
||
| 129 | * @return object |
||
| 130 | * @throws Exception |
||
| 131 | */ |
||
| 132 | protected function getCategoryData(Category $category) : object { |
||
| 133 | return (object) [ |
||
| 134 | 'id' => $category->getId(), |
||
| 135 | 'name' => $category->getName(), |
||
| 136 | 'skip' => $category->getSkip(), |
||
| 137 | 'rounds' => $category instanceof WithRounds ? $category->queryRounds()->ids()->get() : [], |
||
| 138 | 'groups' => $category instanceof WithGroups ? $category->queryGroups()->ids()->get() : [], |
||
| 139 | 'teams' => $category instanceof WithTeams ? $category->getTeamContainer()->ids()->unique()->get() : [], |
||
| 140 | 'games' => $category instanceof WithGames ? $category->getGameContainer()->ids()->get() : [], |
||
| 141 | ]; |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Get all setup information for rounds |
||
| 146 | * |
||
| 147 | * @param array $data |
||
| 148 | * |
||
| 149 | * @throws Exception |
||
| 150 | */ |
||
| 151 | protected function getRoundsData(array &$data) : void { |
||
| 152 | if ($this->object instanceof Round) { |
||
| 153 | $data['rounds'] = [ |
||
| 154 | $this->object->getId() => $this->getRoundData($this->object), |
||
| 155 | ]; |
||
| 156 | } |
||
| 157 | elseif ($this->object instanceof WithRounds) { |
||
| 158 | $data['rounds'] = []; |
||
| 159 | foreach ($this->object->getRounds() as $round) { |
||
| 160 | $data['rounds'][$round->getId()] = $this->getRoundData($round); |
||
| 161 | } |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Get all setup information from a Round class |
||
| 167 | * |
||
| 168 | * @param Round $round Round class to export |
||
| 169 | * |
||
| 170 | * @return object |
||
| 171 | * @throws Exception |
||
| 172 | */ |
||
| 173 | protected function getRoundData(Round $round) : object { |
||
| 182 | ]; |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Get all setup information for groups and progressions |
||
| 187 | * |
||
| 188 | * @param array $data |
||
| 189 | * |
||
| 190 | * @throws Exception |
||
| 191 | */ |
||
| 192 | protected function getGroupsData(array &$data) : void { |
||
| 193 | $data['groups'] = []; |
||
| 194 | $data['progressions'] = []; |
||
| 195 | if ($this->object instanceof Group) { |
||
| 196 | $data['groups'][$this->object->getId()] = $this->getGroupData($this->object); |
||
| 197 | foreach ($this->object->getProgressions() as $progression) { |
||
| 198 | $data['progressions'][] = $this->getProgressionData($progression); |
||
| 199 | } |
||
| 200 | } |
||
| 201 | elseif ($this->object instanceof WithGroups) { |
||
| 202 | foreach ($this->object->getGroups() as $group) { |
||
| 203 | $data['groups'][$group->getId()] = $this->getGroupData($group); |
||
| 204 | foreach ($group->getProgressions() as $progression) { |
||
| 205 | $data['progressions'][] = $this->getProgressionData($progression); |
||
| 206 | } |
||
| 207 | } |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Get all setup information from a Group class |
||
| 213 | * |
||
| 214 | * @param Group $group Group class to export |
||
| 215 | * |
||
| 216 | * @return object |
||
| 217 | * @throws Exception |
||
| 218 | */ |
||
| 219 | protected function getGroupData(Group $group) : object { |
||
| 220 | return (object) [ |
||
| 221 | 'id' => $group->getId(), |
||
| 222 | 'name' => $group->getName(), |
||
| 223 | 'type' => $group->getType(), |
||
| 224 | 'skip' => $group->getSkip(), |
||
| 225 | 'points' => (object) [ |
||
| 226 | 'win' => $group->getWinPoints(), |
||
| 227 | 'loss' => $group->getLostPoints(), |
||
| 228 | 'draw' => $group->getDrawPoints(), |
||
| 229 | 'second' => $group->getSecondPoints(), |
||
| 230 | 'third' => $group->getThirdPoints(), |
||
| 231 | 'progression' => $group->getProgressPoints(), |
||
| 232 | ], |
||
| 233 | 'played' => $group->isPlayed(), |
||
| 234 | 'inGame' => $group->getInGame(), |
||
| 235 | 'maxSize' => $group->getMaxSize(), |
||
| 236 | 'teams' => $group instanceof WithTeams ? $group->getTeamContainer()->ids()->unique()->get() : [], |
||
| 237 | 'games' => $group instanceof WithGames ? $group->getGameContainer()->ids()->get() : [], |
||
| 238 | ]; |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Get all setup information from a Progression class |
||
| 243 | * |
||
| 244 | * @param Progression $progression Progression class to export |
||
| 245 | * |
||
| 246 | * @return object |
||
| 247 | */ |
||
| 248 | protected function getProgressionData(Progression $progression) : object { |
||
| 256 | ]; |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Get all setup information from a TeamFilter class |
||
| 261 | * |
||
| 262 | * @param TeamFilter $filter TeamFilter class to export |
||
| 263 | * |
||
| 264 | * @return object |
||
| 265 | */ |
||
| 266 | protected function getTeamFilterData(TeamFilter $filter) : object { |
||
| 274 | } |