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 PluginService 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 PluginService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class PluginService |
||
|
|
|||
| 35 | { |
||
| 36 | const CONFIG_YML = 'config.yml'; |
||
| 37 | const EVENT_YML = 'event.yml'; |
||
| 38 | private $app; |
||
| 39 | |||
| 40 | 150 | public function __construct($app) |
|
| 44 | |||
| 45 | 9 | public function install($path, $source = 0) |
|
| 46 | { |
||
| 47 | 9 | $pluginBaseDir = null; |
|
| 48 | 9 | $tmp = null; |
|
| 49 | |||
| 50 | try { |
||
| 51 | 9 | $this->app->removePluginConfigCache(); |
|
| 52 | 9 | Cache::clear($this->app, false); |
|
| 53 | 9 | $tmp = $this->createTempDir(); |
|
| 54 | |||
| 55 | 9 | $this->unpackPluginArchive($path, $tmp); //一旦テンポラリに展開 |
|
| 56 | 9 | $this->checkPluginArchiveContent($tmp); |
|
| 57 | |||
| 58 | 7 | $config = $this->readYml($tmp.'/'.self::CONFIG_YML); |
|
| 59 | 7 | $event = $this->readYml($tmp.'/'.self::EVENT_YML); |
|
| 60 | 7 | $this->deleteFile($tmp); // テンポラリのファイルを削除 |
|
| 61 | |||
| 62 | 7 | $this->checkSamePlugin($config['code']); // 重複していないかチェック |
|
| 63 | |||
| 64 | 7 | $pluginBaseDir = $this->calcPluginDir($config['code']); |
|
| 65 | 7 | $this->createPluginDir($pluginBaseDir); // 本来の置き場所を作成 |
|
| 66 | |||
| 67 | 7 | $this->unpackPluginArchive($path, $pluginBaseDir); // 問題なければ本当のplugindirへ |
|
| 68 | |||
| 69 | 7 | $this->registerPlugin($config, $event, $source); // dbにプラグイン登録 |
|
| 70 | 6 | $this->app->writePluginConfigCache(); |
|
| 71 | 4 | } catch (PluginException $e) { |
|
| 72 | 4 | $this->deleteDirs(array($tmp, $pluginBaseDir)); |
|
| 73 | 4 | throw $e; |
|
| 74 | } catch (\Exception $e) { // インストーラがどんなExceptionを上げるかわからないので |
||
| 75 | |||
| 76 | $this->deleteDirs(array($tmp, $pluginBaseDir)); |
||
| 77 | throw $e; |
||
| 78 | } |
||
| 79 | |||
| 80 | 6 | return true; |
|
| 81 | } |
||
| 82 | |||
| 83 | 9 | public function createTempDir() |
|
| 94 | |||
| 95 | 4 | public function deleteDirs($arr) |
|
| 96 | { |
||
| 97 | 4 | foreach ($arr as $dir) { |
|
| 98 | 4 | if (file_exists($dir)) { |
|
| 99 | 3 | $fs = new Filesystem(); |
|
| 100 | 4 | $fs->remove($dir); |
|
| 101 | } |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | 9 | public function unpackPluginArchive($archive, $dir) |
|
| 106 | { |
||
| 107 | 9 | $extension = pathinfo($archive, PATHINFO_EXTENSION); |
|
| 108 | try { |
||
| 109 | 9 | if ($extension == 'zip') { |
|
| 110 | $zip = new \ZipArchive(); |
||
| 111 | $zip->open($archive); |
||
| 112 | $zip->extractTo($dir); |
||
| 113 | $zip->close(); |
||
| 114 | } else { |
||
| 115 | 9 | $phar = new \PharData($archive); |
|
| 116 | 9 | $phar->extractTo($dir, null, true); |
|
| 117 | } |
||
| 118 | } catch (\Exception $e) { |
||
| 119 | throw new PluginException('アップロードに失敗しました。圧縮ファイルを確認してください。'); |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | 150 | public function checkPluginArchiveContent($dir, array $config_cache = array()) |
|
| 124 | { |
||
| 125 | try { |
||
| 126 | 150 | if (!empty($config_cache)) { |
|
| 127 | 142 | $meta = $config_cache; |
|
| 128 | } else { |
||
| 129 | 150 | $meta = $this->readYml($dir . '/config.yml'); |
|
| 130 | } |
||
| 131 | } catch (\Symfony\Component\Yaml\Exception\ParseException $e) { |
||
| 132 | throw new PluginException($e->getMessage(), $e->getCode(), $e); |
||
| 133 | } |
||
| 134 | |||
| 135 | 150 | if (!is_array($meta)) { |
|
| 136 | 2 | throw new PluginException('config.yml not found or syntax error'); |
|
| 137 | } |
||
| 138 | 148 | View Code Duplication | if (!isset($meta['code']) || !$this->checkSymbolName($meta['code'])) { |
| 139 | throw new PluginException('config.yml code empty or invalid_character(\W)'); |
||
| 140 | } |
||
| 141 | 148 | if (!isset($meta['name'])) { |
|
| 142 | // nameは直接クラス名やPATHに使われるわけではないため文字のチェックはなしし |
||
| 143 | 1 | throw new PluginException('config.yml name empty'); |
|
| 144 | } |
||
| 145 | 147 | View Code Duplication | if (isset($meta['event']) && !$this->checkSymbolName($meta['event'])) { // eventだけは必須ではない |
| 146 | throw new PluginException('config.yml event empty or invalid_character(\W) '); |
||
| 147 | } |
||
| 148 | 147 | if (!isset($meta['version'])) { |
|
| 149 | // versionは直接クラス名やPATHに使われるわけではないため文字のチェックはなしし |
||
| 150 | throw new PluginException('config.yml version invalid_character(\W) '); |
||
| 151 | } |
||
| 152 | 147 | if (isset($meta['orm.path'])) { |
|
| 153 | if (!is_array($meta['orm.path'])) { |
||
| 154 | throw new PluginException('config.yml orm.path invalid_character(\W) '); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | 147 | if (isset($meta['service'])) { |
|
| 158 | if (!is_array($meta['service'])) { |
||
| 159 | throw new PluginException('config.yml service invalid_character(\W) '); |
||
| 160 | } |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | 10 | public function readYml($yml) |
|
| 172 | |||
| 173 | 148 | public function checkSymbolName($string) |
|
| 180 | |||
| 181 | 7 | public function deleteFile($path) |
|
| 182 | { |
||
| 183 | 7 | $f = new Filesystem(); |
|
| 184 | 7 | $f->remove($path); |
|
| 185 | } |
||
| 186 | |||
| 187 | 7 | public function checkSamePlugin($code) |
|
| 188 | { |
||
| 189 | 7 | $repo = $this->app['eccube.repository.plugin']->findOneBy(array('code' => $code)); |
|
| 190 | 7 | if ($repo) { |
|
| 191 | 1 | throw new PluginException('plugin already installed.'); |
|
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | 7 | public function calcPluginDir($name) |
|
| 199 | |||
| 200 | 7 | public function createPluginDir($d) |
|
| 201 | { |
||
| 202 | 7 | $b = @mkdir($d); |
|
| 203 | 7 | if (!$b) { |
|
| 204 | throw new PluginException($php_errormsg); |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | 7 | public function registerPlugin($meta, $event_yml, $source = 0) |
|
| 209 | { |
||
| 210 | 7 | $em = $this->app['orm.em']; |
|
| 211 | 7 | $em->getConnection()->beginTransaction(); |
|
| 212 | try { |
||
| 213 | 7 | $p = new \Eccube\Entity\Plugin(); |
|
| 214 | // インストール直後はプラグインは有効にしない |
||
| 215 | 7 | $p->setName($meta['name']) |
|
| 216 | 7 | ->setEnable(Constant::DISABLED) |
|
| 217 | 7 | ->setClassName(isset($meta['event']) ? $meta['event'] : '') |
|
| 218 | 7 | ->setVersion($meta['version']) |
|
| 219 | 7 | ->setDelflg(Constant::DISABLED) |
|
| 220 | 7 | ->setSource($source) |
|
| 221 | 7 | ->setCode($meta['code']); |
|
| 222 | |||
| 223 | 7 | $em->persist($p); |
|
| 224 | 7 | $em->flush(); |
|
| 225 | |||
| 226 | 7 | if (is_array($event_yml)) { |
|
| 227 | 2 | foreach ($event_yml as $event => $handlers) { |
|
| 228 | 2 | foreach ($handlers as $handler) { |
|
| 229 | 2 | if (!$this->checkSymbolName($handler[0])) { |
|
| 230 | throw new PluginException('Handler name format error'); |
||
| 231 | } |
||
| 232 | 2 | $peh = new \Eccube\Entity\PluginEventHandler(); |
|
| 233 | 2 | $peh->setPlugin($p) |
|
| 234 | 2 | ->setEvent($event) |
|
| 235 | 2 | ->setdelFlg(Constant::DISABLED) |
|
| 236 | 2 | ->setHandler($handler[0]) |
|
| 237 | 2 | ->setHandlerType($handler[1]) |
|
| 238 | 2 | ->setPriority($this->app['eccube.repository.plugin_event_handler']->calcNewPriority($event, $handler[1])); |
|
| 239 | 2 | $em->persist($peh); |
|
| 240 | 2 | $em->flush(); |
|
| 241 | } |
||
| 242 | } |
||
| 243 | } |
||
| 244 | |||
| 245 | 7 | $em->persist($p); |
|
| 246 | |||
| 247 | 7 | $this->callPluginManagerMethod($meta, 'install'); |
|
| 248 | |||
| 249 | 6 | $em->flush(); |
|
| 250 | 6 | $em->getConnection()->commit(); |
|
| 251 | 1 | } catch (\Exception $e) { |
|
| 252 | 1 | $em->getConnection()->rollback(); |
|
| 253 | 1 | throw new PluginException($e->getMessage()); |
|
| 254 | } |
||
| 255 | |||
| 256 | 6 | return $p; |
|
| 257 | } |
||
| 258 | |||
| 259 | 7 | public function callPluginManagerMethod($meta, $method) |
|
| 260 | { |
||
| 261 | 7 | $class = '\\Plugin'.'\\'.$meta['code'].'\\'.'PluginManager'; |
|
| 262 | 7 | if (class_exists($class)) { |
|
| 263 | 3 | $installer = new $class(); // マネージャクラスに所定のメソッドがある場合だけ実行する |
|
| 264 | 3 | if (method_exists($installer, $method)) { |
|
| 265 | 3 | $installer->$method($meta, $this->app); |
|
| 266 | } |
||
| 267 | } |
||
| 268 | } |
||
| 269 | |||
| 270 | 5 | public function uninstall(\Eccube\Entity\Plugin $plugin) |
|
| 282 | |||
| 283 | 5 | public function unregisterPlugin(\Eccube\Entity\Plugin $p) |
|
| 284 | { |
||
| 285 | try { |
||
| 286 | 5 | $em = $this->app['orm.em']; |
|
| 287 | 5 | $em->getConnection()->beginTransaction(); |
|
| 288 | |||
| 289 | 5 | $p->setDelFlg(Constant::ENABLED)->setEnable(Constant::DISABLED); |
|
| 290 | |||
| 291 | 5 | foreach ($p->getPluginEventHandlers()->toArray() as $peh) { |
|
| 292 | 5 | $peh->setDelFlg(Constant::ENABLED); |
|
| 293 | } |
||
| 294 | |||
| 295 | 5 | $em->persist($p); |
|
| 296 | 5 | $em->flush(); |
|
| 297 | 5 | $em->getConnection()->commit(); |
|
| 298 | } catch (\Exception $e) { |
||
| 299 | $em->getConnection()->rollback(); |
||
| 300 | throw $e; |
||
| 301 | } |
||
| 302 | } |
||
| 303 | |||
| 304 | 2 | public function disable(\Eccube\Entity\Plugin $plugin) |
|
| 308 | |||
| 309 | 3 | public function enable(\Eccube\Entity\Plugin $plugin, $enable = true) |
|
| 330 | |||
| 331 | 1 | public function update(\Eccube\Entity\Plugin $plugin, $path) |
|
| 332 | { |
||
| 333 | 1 | $pluginBaseDir = null; |
|
| 334 | 1 | $tmp = null; |
|
| 335 | try { |
||
| 336 | 1 | $this->app->removePluginConfigCache(); |
|
| 337 | 1 | Cache::clear($this->app, false); |
|
| 338 | 1 | $tmp = $this->createTempDir(); |
|
| 339 | |||
| 369 | |||
| 370 | 1 | public function updatePlugin(\Eccube\Entity\Plugin $plugin, $meta, $event_yml) |
|
| 440 | } |
||
| 441 |