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 CsvExportService 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 CsvExportService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class CsvExportService |
||
|
|
|||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @var resource |
||
| 38 | */ |
||
| 39 | protected $fp; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var boolean |
||
| 43 | */ |
||
| 44 | protected $closed = false; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var \Closure |
||
| 48 | */ |
||
| 49 | protected $convertEncodingCallBack; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var EntityManagerInterface |
||
| 53 | */ |
||
| 54 | protected $entityManager; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var QueryBuilder; |
||
| 58 | */ |
||
| 59 | protected $qb; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var EccubeConfig |
||
| 63 | */ |
||
| 64 | protected $eccubeConfig; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var CsvType |
||
| 68 | */ |
||
| 69 | protected $CsvType; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var Csv[] |
||
| 73 | */ |
||
| 74 | protected $Csvs; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var CsvRepository |
||
| 78 | */ |
||
| 79 | protected $csvRepository; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var CsvTypeRepository |
||
| 83 | */ |
||
| 84 | protected $csvTypeRepository; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var OrderRepository |
||
| 88 | */ |
||
| 89 | protected $orderRepository; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var ShippingRepository |
||
| 93 | */ |
||
| 94 | protected $shippingRepository; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var CustomerRepository |
||
| 98 | */ |
||
| 99 | protected $customerRepository; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var ProductRepository |
||
| 103 | */ |
||
| 104 | protected $productRepository; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var FormFactoryInterface |
||
| 108 | */ |
||
| 109 | protected $formFactory; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * CsvExportService constructor. |
||
| 113 | * |
||
| 114 | * @param EntityManagerInterface $entityManager |
||
| 115 | * @param CsvRepository $csvRepository |
||
| 116 | * @param CsvTypeRepository $csvTypeRepository |
||
| 117 | * @param OrderRepository $orderRepository |
||
| 118 | * @param CustomerRepository $customerRepository |
||
| 119 | * @param EccubeConfig $eccubeConfig |
||
| 120 | */ |
||
| 121 | 62 | public function __construct( |
|
| 142 | |||
| 143 | /** |
||
| 144 | * @param $config |
||
| 145 | */ |
||
| 146 | public function setConfig($config) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @param CsvRepository $csvRepository |
||
| 153 | */ |
||
| 154 | public function setCsvRepository(CsvRepository $csvRepository) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @param CsvTypeRepository $csvTypeRepository |
||
| 161 | */ |
||
| 162 | public function setCsvTypeRepository(CsvTypeRepository $csvTypeRepository) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param OrderRepository $orderRepository |
||
| 169 | */ |
||
| 170 | public function setOrderRepository(OrderRepository $orderRepository) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @param CustomerRepository $customerRepository |
||
| 177 | */ |
||
| 178 | public function setCustomerRepository(CustomerRepository $customerRepository) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @param ProductRepository $productRepository |
||
| 185 | */ |
||
| 186 | public function setProductRepository(ProductRepository $productRepository) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param EntityManagerInterface $entityManager |
||
| 193 | */ |
||
| 194 | public function setEntityManager(EntityManagerInterface $entityManager) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @return EntityManagerInterface |
||
| 201 | */ |
||
| 202 | public function getEntityManager() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @param QueryBuilder $qb |
||
| 209 | */ |
||
| 210 | 4 | public function setExportQueryBuilder(QueryBuilder $qb) |
|
| 211 | { |
||
| 212 | 4 | $this->qb = $qb; |
|
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Csv種別からServiceの初期化を行う. |
||
| 217 | * |
||
| 218 | * @param $CsvType|integer |
||
| 219 | */ |
||
| 220 | 5 | public function initCsvType($CsvType) |
|
| 237 | |||
| 238 | /** |
||
| 239 | * @return Csv[] |
||
| 240 | */ |
||
| 241 | 4 | public function getCsvs() |
|
| 242 | { |
||
| 243 | 4 | return $this->Csvs; |
|
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * ヘッダ行を出力する. |
||
| 248 | * このメソッドを使う場合は, 事前にinitCsvType($CsvType)で初期化しておく必要がある. |
||
| 249 | */ |
||
| 250 | 4 | public function exportHeader() |
|
| 251 | { |
||
| 252 | 4 | if (is_null($this->CsvType) || is_null($this->Csvs)) { |
|
| 253 | throw new \LogicException('init csv type incomplete.'); |
||
| 254 | } |
||
| 255 | |||
| 256 | 4 | $row = []; |
|
| 257 | 4 | foreach ($this->Csvs as $Csv) { |
|
| 258 | 4 | $row[] = $Csv->getDispName(); |
|
| 259 | } |
||
| 260 | |||
| 261 | 4 | $this->fopen(); |
|
| 262 | 4 | $this->fputcsv($row); |
|
| 263 | 4 | $this->fclose(); |
|
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * クエリビルダにもとづいてデータ行を出力する. |
||
| 268 | * このメソッドを使う場合は, 事前にsetExportQueryBuilder($qb)で出力対象のクエリビルダをわたしておく必要がある. |
||
| 269 | * |
||
| 270 | * @param \Closure $closure |
||
| 271 | */ |
||
| 272 | 4 | public function exportData(\Closure $closure) |
|
| 273 | { |
||
| 274 | 4 | if (is_null($this->qb) || is_null($this->entityManager)) { |
|
| 275 | throw new \LogicException('query builder not set.'); |
||
| 276 | } |
||
| 277 | |||
| 278 | 4 | $this->fopen(); |
|
| 279 | |||
| 280 | 4 | $query = $this->qb->getQuery(); |
|
| 281 | 4 | foreach ($query->getResult() as $iteratableResult) { |
|
| 282 | 4 | $closure($iteratableResult, $this); |
|
| 283 | 4 | $this->entityManager->detach($iteratableResult); |
|
| 284 | 4 | $query->free(); |
|
| 285 | 4 | flush(); |
|
| 286 | } |
||
| 287 | |||
| 288 | 4 | $this->fclose(); |
|
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * CSV出力項目と比較し, 合致するデータを返す. |
||
| 293 | * |
||
| 294 | * @param \Eccube\Entity\Csv $Csv |
||
| 295 | * @param $entity |
||
| 296 | * |
||
| 297 | * @return mixed|null|string|void |
||
| 298 | */ |
||
| 299 | 4 | public function getData(Csv $Csv, $entity) |
|
| 300 | { |
||
| 301 | // エンティティ名が一致するかどうかチェック. |
||
| 302 | 4 | $csvEntityName = str_replace('\\\\', '\\', $Csv->getEntityName()); |
|
| 303 | 4 | $entityName = ClassUtils::getClass($entity); |
|
| 304 | 4 | if ($csvEntityName !== $entityName) { |
|
| 305 | 2 | return null; |
|
| 306 | } |
||
| 307 | |||
| 308 | // カラム名がエンティティに存在するかどうかをチェック. |
||
| 309 | 4 | if (!$entity->offsetExists($Csv->getFieldName())) { |
|
| 310 | 2 | return null; |
|
| 311 | } |
||
| 312 | |||
| 313 | // データを取得. |
||
| 314 | 4 | $data = $entity->offsetGet($Csv->getFieldName()); |
|
| 315 | |||
| 316 | // one to one の場合は, dtb_csv.referece_field_nameと比較し, 合致する結果を取得する. |
||
| 317 | 4 | if ($data instanceof \Eccube\Entity\AbstractEntity) { |
|
| 318 | 4 | if (EntityUtil::isNotEmpty($data)) { |
|
| 319 | 4 | return $data->offsetGet($Csv->getReferenceFieldName()); |
|
| 320 | } |
||
| 321 | 4 | } elseif ($data instanceof \Doctrine\Common\Collections\Collection) { |
|
| 322 | // one to manyの場合は, カンマ区切りに変換する. |
||
| 323 | $array = []; |
||
| 324 | foreach ($data as $elem) { |
||
| 325 | if (EntityUtil::isNotEmpty($elem)) { |
||
| 326 | $array[] = $elem->offsetGet($Csv->getReferenceFieldName()); |
||
| 327 | } |
||
| 328 | } |
||
| 329 | |||
| 330 | return implode($this->eccubeConfig['eccube_csv_export_multidata_separator'], $array); |
||
| 331 | 4 | } elseif ($data instanceof \DateTime) { |
|
| 332 | // datetimeの場合は文字列に変換する. |
||
| 333 | 3 | return $data->format($this->eccubeConfig['eccube_csv_export_date_format']); |
|
| 334 | } else { |
||
| 335 | // スカラ値の場合はそのまま. |
||
| 336 | 4 | return $data; |
|
| 337 | } |
||
| 338 | |||
| 339 | return null; |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * 文字エンコーディングの変換を行うコールバック関数を返す. |
||
| 344 | * |
||
| 345 | * @return \Closure |
||
| 346 | */ |
||
| 347 | 5 | public function getConvertEncodhingCallback() |
|
| 348 | { |
||
| 349 | 5 | $config = $this->eccubeConfig; |
|
| 350 | |||
| 351 | 5 | return function ($value) use ($config) { |
|
| 352 | 5 | return mb_convert_encoding( |
|
| 353 | 5 | (string) $value, $config['eccube_csv_export_encoding'], 'UTF-8' |
|
| 354 | ); |
||
| 355 | 5 | }; |
|
| 356 | } |
||
| 357 | |||
| 358 | 5 | public function fopen() |
|
| 359 | { |
||
| 360 | 5 | if (is_null($this->fp) || $this->closed) { |
|
| 361 | 3 | $this->fp = fopen('php://output', 'w'); |
|
| 362 | } |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @param $row |
||
| 367 | * @param null $callback |
||
| 368 | */ |
||
| 369 | 5 | public function fputcsv($row) |
|
| 370 | { |
||
| 371 | 5 | if (is_null($this->convertEncodingCallBack)) { |
|
| 372 | 5 | $this->convertEncodingCallBack = $this->getConvertEncodhingCallback(); |
|
| 373 | } |
||
| 374 | |||
| 375 | 5 | fputcsv($this->fp, array_map($this->convertEncodingCallBack, $row), $this->eccubeConfig['eccube_csv_export_separator']); |
|
| 376 | } |
||
| 377 | |||
| 378 | 5 | public function fclose() |
|
| 379 | { |
||
| 380 | 5 | if (!$this->closed) { |
|
| 381 | 5 | fclose($this->fp); |
|
| 382 | 5 | $this->closed = true; |
|
| 383 | } |
||
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * 受注検索用のクエリビルダを返す. |
||
| 388 | * |
||
| 389 | * @param Request $request |
||
| 390 | * |
||
| 391 | * @return \Doctrine\ORM\QueryBuilder |
||
| 392 | */ |
||
| 393 | 1 | View Code Duplication | public function getOrderQueryBuilder(Request $request) |
| 394 | { |
||
| 395 | 1 | $session = $request->getSession(); |
|
| 396 | 1 | $builder = $this->formFactory |
|
| 397 | 1 | ->createBuilder(SearchProductType::class); |
|
| 398 | 1 | $searchForm = $builder->getForm(); |
|
| 399 | |||
| 400 | 1 | $viewData = $session->get('eccube.admin.order.search', []); |
|
| 401 | 1 | $searchData = FormUtil::submitAndGetData($searchForm, $viewData); |
|
| 402 | |||
| 403 | // 受注データのクエリビルダを構築. |
||
| 404 | 1 | $qb = $this->orderRepository |
|
| 405 | 1 | ->getQueryBuilderBySearchDataForAdmin($searchData); |
|
| 406 | |||
| 407 | 1 | return $qb; |
|
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * 会員検索用のクエリビルダを返す. |
||
| 412 | * |
||
| 413 | * @param Request $request |
||
| 414 | * |
||
| 415 | * @return \Doctrine\ORM\QueryBuilder |
||
| 416 | */ |
||
| 417 | 1 | View Code Duplication | public function getCustomerQueryBuilder(Request $request) |
| 418 | { |
||
| 419 | 1 | $session = $request->getSession(); |
|
| 420 | 1 | $builder = $this->formFactory |
|
| 421 | 1 | ->createBuilder(SearchProductType::class); |
|
| 422 | 1 | $searchForm = $builder->getForm(); |
|
| 423 | |||
| 424 | 1 | $viewData = $session->get('eccube.admin.customer.search', []); |
|
| 425 | 1 | $searchData = FormUtil::submitAndGetData($searchForm, $viewData); |
|
| 426 | |||
| 427 | // 会員データのクエリビルダを構築. |
||
| 428 | 1 | $qb = $this->customerRepository |
|
| 429 | 1 | ->getQueryBuilderBySearchData($searchData); |
|
| 430 | |||
| 431 | 1 | return $qb; |
|
| 432 | } |
||
| 433 | |||
| 434 | /** |
||
| 435 | * 商品検索用のクエリビルダを返す. |
||
| 436 | * |
||
| 437 | * @param Request $request |
||
| 438 | * |
||
| 439 | * @return \Doctrine\ORM\QueryBuilder |
||
| 440 | */ |
||
| 441 | View Code Duplication | public function getProductQueryBuilder(Request $request) |
|
| 457 | } |
||
| 458 |