| Total Complexity | 43 |
| Total Lines | 251 |
| Duplicated Lines | 16.33 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like DefaultController 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 DefaultController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 47 | class DefaultController extends Controller |
||
| 48 | { |
||
| 49 | /** |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | private $configTwineDirectory; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var Templating |
||
| 56 | */ |
||
| 57 | private $templating; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var UserRepositoryInterface |
||
| 61 | */ |
||
| 62 | private $userRepository; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var LicenseeRepositoryInterface |
||
| 66 | */ |
||
| 67 | private $licenseeRepository; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var TopicRepositoryInterface |
||
| 71 | */ |
||
| 72 | private $topicRepository; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var ImportfileRepositoryInterface |
||
| 76 | */ |
||
| 77 | private $importfileRepository; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var UserPasswordEncoder |
||
| 81 | */ |
||
| 82 | private $userPasswordEncoder; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var string |
||
| 86 | */ |
||
| 87 | private $topicImageDirectory; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * DefaultController constructor. |
||
| 91 | * @param Templating $templating |
||
| 92 | * @param UserRepositoryInterface $userRepository |
||
| 93 | * @param LicenseeRepositoryInterface $licenseeRepository |
||
| 94 | * @param TopicRepositoryInterface $topicRepository |
||
| 95 | * @param ImportfileRepositoryInterface $importfileRepository |
||
| 96 | * @param UserPasswordEncoder $userPasswordEncoder |
||
| 97 | * @param string $configTwineDirectory |
||
| 98 | * @param string $topicImageDirectory |
||
| 99 | */ |
||
| 100 | public function __construct( |
||
| 101 | Templating $templating, |
||
| 102 | UserRepositoryInterface $userRepository, |
||
| 103 | LicenseeRepositoryInterface $licenseeRepository, |
||
| 104 | TopicRepositoryInterface $topicRepository, |
||
| 105 | ImportfileRepositoryInterface $importfileRepository, |
||
| 106 | UserPasswordEncoder $userPasswordEncoder, |
||
| 107 | string $configTwineDirectory, |
||
| 108 | string $topicImageDirectory |
||
| 109 | ) { |
||
| 110 | $this->configTwineDirectory = $configTwineDirectory; |
||
| 111 | $this->userRepository = $userRepository; |
||
| 112 | $this->templating = $templating; |
||
| 113 | $this->licenseeRepository = $licenseeRepository; |
||
| 114 | $this->topicRepository = $topicRepository; |
||
| 115 | $this->importfileRepository = $importfileRepository; |
||
| 116 | $this->userPasswordEncoder = $userPasswordEncoder; |
||
| 117 | $this->topicImageDirectory = $topicImageDirectory; |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @Route("/", name="admin_mainpage") |
||
| 122 | * |
||
| 123 | * @return Response |
||
| 124 | * @throws \RuntimeException |
||
| 125 | */ |
||
| 126 | public function indexAction(): Response |
||
| 127 | { |
||
| 128 | $mainMenuData = [ |
||
| 129 | ['id' => '1', 'type' => 'folder', 'value' => 'Benutzer', 'css' => 'folder_music'], |
||
| 130 | ['id' => '2', 'type' => 'folder', 'value' => 'Lizenznehmer', 'css' => 'folder_music'], |
||
| 131 | ['id' => '3', 'type' => 'folder', 'value' => 'Themenfelder', 'css' => 'folder_music'], |
||
| 132 | ['id' => '4', 'type' => 'folder', 'value' => 'Importe', 'css' => 'folder_music'], |
||
| 133 | ['id' => '5', 'type' => 'folder', 'value' => 'Textknoten', 'css' => 'folder_music'], |
||
| 134 | ]; |
||
| 135 | |||
| 136 | $jsonEncoder = new JsonEncoder(); |
||
| 137 | |||
| 138 | return $this->templating->renderResponse( |
||
| 139 | 'AdminBundle::index.html.twig', |
||
| 140 | [ |
||
| 141 | 'mainMenuData' => $jsonEncoder->encode($mainMenuData, 'json'), |
||
| 142 | ] |
||
| 143 | ); |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @Route("/save", name="admin_formsave") |
||
| 148 | * |
||
| 149 | * @param Request $request |
||
| 150 | * @return Response |
||
| 151 | * @throws \Exception |
||
| 152 | * @throws \RuntimeException |
||
| 153 | * @throws \InvalidArgumentException |
||
| 154 | */ |
||
| 155 | public function formsaveAction(Request $request): Response |
||
| 156 | { |
||
| 157 | $params = $request->request->all(); |
||
| 158 | |||
| 159 | if (!isset($params['formtype']) || !in_array($params['formtype'], array('user', 'licensee', 'topic', 'importfile', 'textnode'), true)) { |
||
| 160 | return new Response(\json_encode(array('error' => true))); |
||
| 161 | } |
||
| 162 | if (!isset($params['id'])) { |
||
| 163 | return new Response(\json_encode(array('error' => true))); |
||
| 164 | } |
||
| 165 | $formtype = $params['formtype']; |
||
| 166 | |||
| 167 | /* @var $repository AbstractRepository */ |
||
| 168 | switch ($formtype) { |
||
| 169 | case 'user': |
||
| 170 | $repository = $this->userRepository; |
||
| 171 | break; |
||
| 172 | case 'topic': |
||
| 173 | $repository = $this->topicRepository; |
||
| 174 | break; |
||
| 175 | case 'licensee': |
||
| 176 | $repository = $this->licenseeRepository; |
||
| 177 | break; |
||
| 178 | case 'importfile': |
||
| 179 | $repository = $this->importfileRepository; |
||
| 180 | break; |
||
| 181 | default: |
||
| 182 | throw new \RuntimeException('unknown formtype ['.$formtype.']'); |
||
| 183 | } |
||
| 184 | |||
| 185 | if (isset($params['id']) && $params['id'] === 'new') { |
||
| 186 | $className = $repository->getClassName(); |
||
| 187 | $item = new $className(); |
||
| 188 | } else { |
||
| 189 | $item = $repository->find($params['id']); |
||
| 190 | if (null === $item || $item->getId() !== $params['id']) { |
||
| 191 | return new Response(\json_encode(array('error' => true))); |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | foreach ($params as $param => $value) { |
||
| 196 | if (in_array($param, array('id', 'formtype', 'filename', 'orgname'), true)) { |
||
| 197 | continue; |
||
| 198 | } |
||
| 199 | if ($param === 'password' && empty($value)) { |
||
| 200 | continue; |
||
| 201 | } |
||
| 202 | |||
| 203 | if ($param === 'password') { |
||
| 204 | $value = $this->userPasswordEncoder->encodePassword($item, $value); |
||
|
|
|||
| 205 | } elseif ($param === 'licenseeId' && $value === '') { |
||
| 206 | $value = null; |
||
| 207 | } elseif ($param === 'imported' && $value === '') { |
||
| 208 | $value = null; |
||
| 209 | } |
||
| 210 | $method = 'set'.ucfirst($param); |
||
| 211 | if (method_exists($item, $method)) { |
||
| 212 | $item->$method($value); |
||
| 213 | } |
||
| 214 | } |
||
| 215 | //var_dump($item);die(); |
||
| 216 | if (method_exists($item, 'setMetadata')) { |
||
| 217 | $item->setMetadata('updated', time()); |
||
| 218 | } |
||
| 219 | $repository->save($item); |
||
| 220 | |||
| 221 | if ($formtype === 'topic' && array_key_exists('imageFileName', $params) && !is_null($params['imageFileName'])) { |
||
| 222 | $this->saveTopicImage($item, $params['imageFileName'], $params['originalImageName']); |
||
| 223 | $repository->save($item); |
||
| 224 | } |
||
| 225 | |||
| 226 | if ($formtype === 'importfile' && array_key_exists('filename', $params)) { |
||
| 227 | $this->saveFile($item, $params['filename'], $params['orgname']); |
||
| 228 | $repository->save($item); |
||
| 229 | } |
||
| 230 | |||
| 231 | $output = array( |
||
| 232 | 'error' => false, |
||
| 233 | 'newId' => $item->getId(), |
||
| 234 | ); |
||
| 235 | |||
| 236 | return new Response(\json_encode($output)); |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * saves temporary file to final place |
||
| 241 | * |
||
| 242 | * @param Importfile $item importfile instance |
||
| 243 | * @param string $filename filename hash |
||
| 244 | * @param string $orgname original name |
||
| 245 | * @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException |
||
| 246 | * @throws \RuntimeException |
||
| 247 | */ |
||
| 248 | View Code Duplication | private function saveFile(Importfile $item, $filename, $orgname) |
|
| 249 | { |
||
| 250 | if (empty($filename) || empty($orgname)) { |
||
| 251 | return; |
||
| 252 | } |
||
| 253 | |||
| 254 | $directory = $this->configTwineDirectory; |
||
| 255 | $file = $directory.$filename; |
||
| 256 | if (!file_exists($file)) { |
||
| 257 | return; |
||
| 258 | } |
||
| 259 | $finalDirectory = $directory.$item->getLicenseeId().'/'; |
||
| 260 | if (!is_dir($finalDirectory)) { |
||
| 261 | if (!mkdir($finalDirectory) && !is_dir($finalDirectory)) { |
||
| 262 | throw new \RuntimeException(sprintf('Directory "%s" was not created', $finalDirectory)); |
||
| 263 | } |
||
| 264 | } |
||
| 265 | $finalName = $finalDirectory.$item->getId(); |
||
| 266 | |||
| 267 | rename($file, $finalName); |
||
| 268 | |||
| 269 | $item->setOriginalname($orgname); |
||
| 270 | $item->setFilename($finalName); |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * saves temporary file to final place |
||
| 275 | * |
||
| 276 | * @param Topic $item topic instance |
||
| 277 | * @param string $filename filename hash |
||
| 278 | * @param string $orgname original name |
||
| 279 | * @throws \RuntimeException |
||
| 280 | */ |
||
| 281 | View Code Duplication | private function saveTopicImage(Topic $item, $filename, $orgname) |
|
| 298 | } |
||
| 299 | } |
||
| 300 |