| Conditions | 10 | 
| Paths | 48 | 
| Total Lines | 79 | 
| Code Lines | 42 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php | ||
| 28 | public function load(ObjectManager $manager) | ||
| 29 |     { | ||
| 30 | // CSV Reader に設定 | ||
| 31 | $this->file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY); | ||
| 32 | |||
| 33 | // ヘッダ行を取得 | ||
| 34 | $headers = $this->file->current(); | ||
| 35 | $this->file->next(); | ||
| 36 | |||
| 37 | // ファイル名からテーブル名を取得 | ||
| 38 |         $table_name = str_replace('.'.$this->file->getExtension(), '', $this->file->getFilename()); | ||
| 39 | $sql = $this->getSql($table_name, $headers); | ||
| 40 | /** @var Connection $Connection */ | ||
| 41 | $Connection = $manager->getConnection(); | ||
| 42 | $Connection->beginTransaction(); | ||
| 43 | |||
| 44 | // mysqlの場合はNO_AUTO_VALUE_ON_ZEROを設定 | ||
| 45 |         if ('mysql' === $Connection->getDatabasePlatform()->getName()) { | ||
| 46 |             $Connection->exec("SET SESSION sql_mode='NO_AUTO_VALUE_ON_ZERO';"); | ||
| 47 | } | ||
| 48 | |||
| 49 | // TODO エラーハンドリング | ||
| 50 | $prepare = $Connection->prepare($sql); | ||
| 51 |         while (!$this->file->eof()) { | ||
| 52 | $rows = $this->file->current(); | ||
| 53 | $index = 1; | ||
| 54 | // データ行をバインド | ||
| 55 |             foreach ($rows as $col) { | ||
| 56 | $col = $col === '' ? null : $col; | ||
| 57 | $prepare->bindValue($index, $col); | ||
| 58 | $index++; | ||
| 59 | } | ||
| 60 | // batch insert | ||
| 61 | $result = $prepare->execute(); | ||
| 62 | $this->file->next(); | ||
| 63 | } | ||
| 64 | $Connection->commit(); | ||
| 65 | |||
| 66 | // postgresqlの場合はシーケンスを振り直す | ||
| 67 |         if ('postgresql' === $Connection->getDatabasePlatform()->getName()) { | ||
| 68 | // テーブル情報を取得 | ||
| 69 | $sm = $Connection->getSchemaManager(); | ||
| 70 | $table = $sm->listTableDetails($table_name); | ||
| 71 | |||
| 72 | // 主キーがないテーブルはスキップ | ||
| 73 |             if (!$table->hasPrimaryKey()) { | ||
| 74 | return; | ||
| 75 | } | ||
| 76 | |||
| 77 | // 複合主キーのテーブルはスキップ | ||
| 78 | $pkColumns = $table->getPrimaryKey()->getColumns(); | ||
| 79 |             if (count($pkColumns) != 1) { | ||
| 80 | return; | ||
| 81 | } | ||
| 82 | |||
| 83 | // シーケンス名を取得 | ||
| 84 | $pk_name = $pkColumns[0]; | ||
| 85 |             $sequence_name = sprintf('%s_%s_seq', $table_name, $pk_name); | ||
| 86 | |||
| 87 | // シーケンスの存在チェック | ||
| 88 | $sql = 'SELECT COUNT(*) FROM information_schema.sequences WHERE sequence_name = ?'; | ||
| 89 | $count = $Connection->fetchColumn($sql, [$sequence_name]); | ||
| 90 |             if ($count < 1) { | ||
| 91 | return; | ||
| 92 | } | ||
| 93 | |||
| 94 | // シーケンスを更新 | ||
| 95 |             $sql = sprintf('SELECT MAX(%s) FROM %s', $pk_name, $table_name); | ||
| 96 | $max = $Connection->fetchColumn($sql); | ||
| 97 |             if (is_null($max)) { | ||
| 98 | // レコードが無い場合は1を初期値に設定 | ||
| 99 |                 $sql = sprintf("SELECT SETVAL('%s', 1, false)", $sequence_name); | ||
| 100 |             } else { | ||
| 101 | // レコードがある場合は最大値を設定 | ||
| 102 |                 $sql = sprintf("SELECT SETVAL('%s', %s)", $sequence_name, $max); | ||
| 103 | } | ||
| 104 | $Connection->executeQuery($sql); | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 130 |