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 InstallController 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 InstallController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
41 | class InstallController |
||
|
|||
42 | { |
||
43 | private $app; |
||
44 | |||
45 | private $PDO; |
||
46 | |||
47 | private $config_path; |
||
48 | |||
49 | private $dist_path; |
||
50 | |||
51 | private $cache_path; |
||
52 | |||
53 | private $session_data; |
||
54 | |||
55 | private $required_modules = array('pdo', 'phar', 'mbstring', 'zlib', 'ctype', 'session', 'JSON', 'xml', 'libxml', 'OpenSSL', 'zip', 'cURL', 'fileinfo'); |
||
56 | |||
57 | private $recommended_module = array('hash', 'mcrypt'); |
||
58 | |||
59 | const SESSION_KEY = 'eccube.session.install'; |
||
60 | |||
61 | 7 | public function __construct() |
|
62 | { |
||
63 | 7 | $this->config_path = __DIR__ . '/../../../../app/config/eccube'; |
|
64 | 7 | $this->dist_path = __DIR__ . '/../../Resource/config'; |
|
65 | 7 | $this->cache_path = __DIR__ . '/../../../../app/cache'; |
|
66 | 7 | } |
|
67 | |||
68 | 4 | private function isValid(Request $request, Form $form) |
|
69 | { |
||
70 | $session = $request->getSession(); |
||
71 | if ('POST' === $request->getMethod()) { |
||
72 | $form->handleRequest($request); |
||
73 | if ($form->isValid()) { |
||
74 | $sessionData = $session->get(self::SESSION_KEY) ?: array(); |
||
75 | $formData = array_replace_recursive($sessionData, $form->getData()); |
||
76 | $session->set(self::SESSION_KEY, $formData); |
||
77 | |||
78 | return true; |
||
79 | } |
||
80 | } |
||
81 | |||
82 | 4 | return false; |
|
83 | 4 | } |
|
84 | |||
85 | 5 | private function getSessionData(Request $request) |
|
89 | |||
90 | // 最初からやり直す場合、SESSION情報をクリア |
||
91 | 1 | public function index(InstallApplication $app, Request $request) |
|
97 | |||
98 | // ようこそ |
||
99 | 1 | public function step1(InstallApplication $app, Request $request) |
|
117 | |||
118 | // 権限チェック |
||
119 | 1 | public function step2(InstallApplication $app, Request $request) |
|
139 | |||
140 | // サイトの設定 |
||
141 | 1 | public function step3(InstallApplication $app, Request $request) |
|
142 | { |
||
143 | $form = $app['form.factory'] |
||
144 | ->createBuilder('install_step3') |
||
145 | ->getForm(); |
||
146 | $sessionData = $this->getSessionData($request); |
||
147 | |||
148 | 1 | if (empty($sessionData['shop_name'])) { |
|
149 | |||
150 | 1 | $config_file = $this->config_path . '/config.yml'; |
|
151 | $fs = new Filesystem(); |
||
152 | |||
153 | if ($fs->exists($config_file)) { |
||
154 | // すでに登録されていた場合、登録データを表示 |
||
155 | $this->setPDO(); |
||
156 | $stmt = $this->PDO->query("SELECT shop_name, email01 FROM dtb_base_info WHERE id = 1;"); |
||
157 | |||
158 | foreach ($stmt as $row) { |
||
159 | 1 | $sessionData['shop_name'] = $row['shop_name']; |
|
160 | 1 | $sessionData['email'] = $row['email01']; |
|
161 | } |
||
162 | |||
163 | // セキュリティの設定 |
||
164 | 1 | $config_file = $this->config_path . '/path.yml'; |
|
165 | $config = Yaml::parse(file_get_contents($config_file)); |
||
166 | 1 | $sessionData['admin_dir'] = $config['admin_route']; |
|
167 | |||
168 | 1 | $config_file = $this->config_path . '/config.yml'; |
|
169 | $config = Yaml::parse(file_get_contents($config_file)); |
||
170 | |||
171 | 1 | $allowHost = $config['admin_allow_host']; |
|
172 | if (count($allowHost) > 0) { |
||
173 | $sessionData['admin_allow_hosts'] = Str::convertLineFeed(implode("\n", $allowHost)); |
||
174 | } |
||
175 | 1 | $sessionData['admin_force_ssl'] = (bool)$config['force_ssl']; |
|
176 | |||
177 | // メール設定 |
||
178 | 1 | $config_file = $this->config_path . '/mail.yml'; |
|
179 | $config = Yaml::parse(file_get_contents($config_file)); |
||
180 | 1 | $mail = $config['mail']; |
|
181 | 1 | $sessionData['mail_backend'] = $mail['transport']; |
|
182 | 1 | $sessionData['smtp_host'] = $mail['host']; |
|
183 | 1 | $sessionData['smtp_port'] = $mail['port']; |
|
184 | 1 | $sessionData['smtp_username'] = $mail['username']; |
|
185 | 1 | $sessionData['smtp_password'] = $mail['password']; |
|
186 | } else { |
||
187 | // 初期値にmailを設定 |
||
188 | $sessionData['mail_backend'] = 'mail'; |
||
189 | 1 | } |
|
190 | } |
||
191 | |||
192 | $form->setData($sessionData); |
||
193 | if ($this->isValid($request, $form)) { |
||
194 | $data = $form->getData(); |
||
195 | |||
196 | return $app->redirect($app->url('install_step4')); |
||
197 | } |
||
198 | |||
199 | 1 | return $app['twig']->render('step3.twig', array( |
|
200 | 1 | 'form' => $form->createView(), |
|
201 | )); |
||
202 | 1 | } |
|
203 | |||
204 | // データベースの設定 |
||
205 | 1 | public function step4(InstallApplication $app, Request $request) |
|
246 | |||
247 | // データベースの初期化 |
||
248 | 1 | public function step5(InstallApplication $app, Request $request) |
|
249 | { |
||
250 | set_time_limit(0); |
||
251 | 1 | $this->app = $app; |
|
252 | $form = $app['form.factory'] |
||
253 | ->createBuilder('install_step5') |
||
254 | ->getForm(); |
||
255 | $sessionData = $this->getSessionData($request); |
||
256 | $form->setData($sessionData); |
||
257 | |||
258 | if ($this->isValid($request, $form)) { |
||
259 | |||
260 | $this |
||
261 | ->createDatabaseYamlFile($sessionData) |
||
262 | ->createMailYamlFile($sessionData) |
||
263 | ->createPathYamlFile($sessionData, $request); |
||
264 | |||
265 | if (!$form['no_update']->getData()) { |
||
266 | set_time_limit(0); |
||
267 | $this->createConfigYamlFile($sessionData); |
||
268 | |||
269 | $this |
||
270 | ->setPDO() |
||
271 | ->dropTables() |
||
272 | ->createTables() |
||
273 | ->doMigrate() |
||
274 | ->insert(); |
||
275 | } else { |
||
276 | // データベースを初期化しない場合、auth_magicは初期化しない |
||
277 | $this->createConfigYamlFile($sessionData, false); |
||
278 | |||
279 | $this |
||
280 | ->setPDO() |
||
281 | ->update(); |
||
282 | |||
283 | } |
||
284 | |||
285 | |||
286 | if (isset($sessionData['agree']) && $sessionData['agree'] == '1') { |
||
287 | $host = $request->getSchemeAndHttpHost(); |
||
288 | $basePath = $request->getBasePath(); |
||
289 | $params = array( |
||
290 | 'http_url' => $host . $basePath, |
||
291 | 'shop_name' => $sessionData['shop_name'], |
||
292 | ); |
||
293 | |||
294 | $this->sendAppData($params); |
||
295 | } |
||
296 | $this->addInstallStatus(); |
||
297 | |||
298 | $request->getSession()->remove(self::SESSION_KEY); |
||
299 | |||
300 | return $app->redirect($app->url('install_complete')); |
||
301 | } |
||
302 | |||
303 | 1 | return $app['twig']->render('step5.twig', array( |
|
304 | 1 | 'form' => $form->createView(), |
|
305 | )); |
||
306 | 1 | } |
|
307 | |||
308 | // インストール完了 |
||
309 | 1 | public function complete(InstallApplication $app, Request $request) |
|
323 | |||
324 | private function resetNatTimer() |
||
331 | |||
332 | |||
333 | 1 | private function checkModules($app) |
|
373 | |||
374 | 1 | private function setPDO() |
|
390 | |||
391 | View Code Duplication | private function dropTables() |
|
405 | |||
406 | /** |
||
407 | * @return EntityManager |
||
408 | */ |
||
409 | private function getEntityManager() |
||
437 | |||
438 | View Code Duplication | private function createTables() |
|
450 | |||
451 | private function insert() |
||
510 | |||
511 | private function update() |
||
512 | { |
||
513 | $this->resetNatTimer(); |
||
514 | |||
515 | $config_file = $this->config_path . '/database.yml'; |
||
516 | $database = Yaml::parse(file_get_contents($config_file)); |
||
517 | $config['database'] = $database['database']; |
||
518 | |||
519 | $config_file = $this->config_path . '/config.yml'; |
||
520 | $baseConfig = Yaml::parse(file_get_contents($config_file)); |
||
521 | $config['config'] = $baseConfig; |
||
522 | |||
523 | $this->PDO->beginTransaction(); |
||
524 | |||
525 | try { |
||
526 | |||
527 | $config = array( |
||
528 | 'auth_type' => '', |
||
529 | 'auth_magic' => $config['config']['auth_magic'], |
||
530 | 'password_hash_algos' => 'sha256', |
||
531 | ); |
||
532 | $passwordEncoder = new \Eccube\Security\Core\Encoder\PasswordEncoder($config); |
||
533 | $salt = \Eccube\Util\Str::random(32); |
||
534 | |||
535 | $stmt = $this->PDO->prepare("SELECT member_id FROM dtb_member WHERE login_id = :login_id;"); |
||
536 | $stmt->execute(array(':login_id' => $this->session_data['login_id'])); |
||
537 | $rs = $stmt->fetch(); |
||
538 | |||
539 | $encodedPassword = $passwordEncoder->encodePassword($this->session_data['login_pass'], $salt); |
||
540 | |||
541 | if ($rs) { |
||
542 | // 同一の管理者IDであればパスワードのみ更新 |
||
543 | $sth = $this->PDO->prepare("UPDATE dtb_member set password = :admin_pass, salt = :salt, update_date = current_timestamp WHERE login_id = :login_id;"); |
||
544 | $sth->execute(array(':admin_pass' => $encodedPassword, ':salt' => $salt, ':login_id' => $this->session_data['login_id'])); |
||
545 | |||
546 | } else { |
||
547 | // 新しい管理者IDが入力されたらinsert |
||
548 | $sth = $this->PDO->prepare("INSERT INTO dtb_member (login_id, password, salt, work, del_flg, authority, creator_id, rank, update_date, create_date,name,department) VALUES (:login_id, :admin_pass , :salt , '1', '0', '0', '1', '1', current_timestamp, current_timestamp,'管理者','EC-CUBE SHOP');"); |
||
549 | $sth->execute(array(':login_id' => $this->session_data['login_id'], ':admin_pass' => $encodedPassword, ':salt' => $salt)); |
||
550 | } |
||
551 | |||
552 | $sth = $this->PDO->prepare('UPDATE dtb_base_info set |
||
553 | shop_name = :shop_name, |
||
554 | email01 = :admin_mail, |
||
555 | email02 = :admin_mail, |
||
556 | email03 = :admin_mail, |
||
557 | email04 = :admin_mail, |
||
558 | update_date = current_timestamp |
||
559 | WHERE id = 1;'); |
||
560 | $sth->execute(array( |
||
561 | ':shop_name' => $this->session_data['shop_name'], |
||
562 | ':admin_mail' => $this->session_data['email'] |
||
563 | )); |
||
564 | |||
565 | $this->PDO->commit(); |
||
566 | } catch (\Exception $e) { |
||
567 | $this->PDO->rollback(); |
||
568 | throw $e; |
||
569 | } |
||
570 | |||
571 | return $this; |
||
572 | } |
||
573 | |||
574 | |||
575 | private function getMigration() |
||
592 | |||
593 | private function doMigrate() |
||
594 | { |
||
595 | try { |
||
596 | $migration = $this->getMigration(); |
||
597 | |||
598 | // DBとのコネクションを維持するためpingさせる |
||
599 | if (is_null($this->PDO)) { |
||
600 | $this->setPDO(); |
||
601 | } |
||
602 | $this->PDO->ping(); |
||
603 | |||
604 | // nullを渡すと最新バージョンまでマイグレートする |
||
605 | $migration->migrate(null, false); |
||
606 | } catch (MigrationException $e) { |
||
607 | } |
||
608 | |||
609 | return $this; |
||
610 | } |
||
611 | |||
612 | 1 | private function getProtectedDirs() |
|
635 | |||
636 | private function createConfigYamlFile($data, $auth = true) |
||
637 | { |
||
638 | $fs = new Filesystem(); |
||
639 | $config_file = $this->config_path . '/config.yml'; |
||
640 | |||
641 | if ($fs->exists($config_file)) { |
||
642 | $config = Yaml::parse(file_get_contents($config_file)); |
||
643 | $fs->remove($config_file); |
||
644 | } |
||
645 | |||
646 | if ($auth) { |
||
647 | $auth_magic = Str::random(32); |
||
648 | } else { |
||
649 | if (isset($config['auth_magic'])) { |
||
650 | $auth_magic = $config['auth_magic']; |
||
651 | } else { |
||
652 | $auth_magic = Str::random(32); |
||
653 | } |
||
654 | } |
||
655 | |||
656 | $allowHost = Str::convertLineFeed($data['admin_allow_hosts']); |
||
657 | if (empty($allowHost)) { |
||
658 | $adminAllowHosts = array(); |
||
659 | } else { |
||
660 | $adminAllowHosts = explode("\n", $allowHost); |
||
661 | } |
||
662 | |||
663 | $target = array('${AUTH_MAGIC}', '${SHOP_NAME}', '${ECCUBE_INSTALL}', '${FORCE_SSL}'); |
||
664 | $replace = array($auth_magic, $data['shop_name'], '0', $data['admin_force_ssl']); |
||
665 | |||
666 | $fs = new Filesystem(); |
||
667 | $content = str_replace( |
||
668 | $target, |
||
669 | $replace, |
||
670 | file_get_contents($this->dist_path . '/config.yml.dist') |
||
671 | ); |
||
672 | $fs->dumpFile($config_file, $content); |
||
673 | |||
674 | $config = Yaml::parse(file_get_contents($config_file)); |
||
675 | $config['admin_allow_host'] = $adminAllowHosts; |
||
676 | $yml = Yaml::dump($config); |
||
677 | file_put_contents($config_file, $yml); |
||
678 | |||
679 | return $this; |
||
680 | } |
||
681 | |||
682 | private function addInstallStatus() |
||
692 | |||
693 | private function createDatabaseYamlFile($data) |
||
747 | |||
748 | private function createMailYamlFile($data) |
||
774 | |||
775 | private function createPathYamlFile($data, Request $request) |
||
802 | |||
803 | private function sendAppData($params) |
||
850 | |||
851 | |||
852 | /** |
||
853 | * マイグレーション画面を表示する. |
||
854 | * |
||
855 | * @param InstallApplication $app |
||
856 | * @param Request $request |
||
857 | * |
||
858 | * @return \Symfony\Component\HttpFoundation\Response |
||
859 | */ |
||
860 | public function migration(InstallApplication $app, Request $request) |
||
864 | |||
865 | /** |
||
866 | * インストール済プラグインの一覧を表示する. |
||
867 | * プラグインがインストールされていない場合は, マイグレーション実行画面へリダイレクトする. |
||
868 | * |
||
869 | * @param InstallApplication $app |
||
870 | * @param Request $request |
||
871 | * |
||
872 | * @return \Symfony\Component\HttpFoundation\Response |
||
873 | */ |
||
874 | public function migration_plugin(InstallApplication $app, Request $request) |
||
892 | |||
893 | /** |
||
894 | * マイグレーションを実行し, 完了画面を表示させる |
||
895 | * |
||
896 | * @param InstallApplication $app |
||
897 | * @param Request $request |
||
898 | * |
||
899 | * @return \Symfony\Component\HttpFoundation\Response |
||
900 | */ |
||
901 | public function migration_end(InstallApplication $app, Request $request) |
||
912 | } |
||
913 |