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 |
||
33 | class CsvExportService |
||
|
|||
34 | { |
||
35 | /** |
||
36 | * @var |
||
37 | */ |
||
38 | protected $fp; |
||
39 | |||
40 | /** |
||
41 | * @var |
||
42 | */ |
||
43 | protected $closed = false; |
||
44 | |||
45 | /** |
||
46 | * @var \Closure |
||
47 | */ |
||
48 | protected $convertEncodingCallBack; |
||
49 | |||
50 | /** |
||
51 | * @var \Doctrine\ORM\EntityManager |
||
52 | */ |
||
53 | protected $em; |
||
54 | |||
55 | /** |
||
56 | * @var \Doctrine\ORM\QueryBuilder; |
||
57 | */ |
||
58 | protected $qb; |
||
59 | |||
60 | /** |
||
61 | * @var array |
||
62 | */ |
||
63 | protected $config; |
||
64 | |||
65 | /** |
||
66 | * @var \Eccube\Entity\Master\CsvType |
||
67 | */ |
||
68 | protected $CsvType; |
||
69 | |||
70 | /** |
||
71 | * @var \Eccube\Entity\Csv[] |
||
72 | */ |
||
73 | protected $Csvs; |
||
74 | |||
75 | /** |
||
76 | * @var \Eccube\Repository\CsvRepository |
||
77 | */ |
||
78 | protected $csvRepository; |
||
79 | |||
80 | /** |
||
81 | * @var \Eccube\Repository\Master\CsvTypeRepository |
||
82 | */ |
||
83 | protected $csvTypeRepository; |
||
84 | |||
85 | /** |
||
86 | * @var \Eccube\Repository\OrderRepository |
||
87 | */ |
||
88 | protected $orderRepository; |
||
89 | |||
90 | /** |
||
91 | * @var \Eccube\Repository\CustomerRepository |
||
92 | */ |
||
93 | protected $customerRepository; |
||
94 | |||
95 | /** |
||
96 | * @var \Eccube\Repository\ProductRepository |
||
97 | */ |
||
98 | protected $productRepository; |
||
99 | |||
100 | /** |
||
101 | * @param $config |
||
102 | */ |
||
103 | 2 | public function setConfig($config) |
|
104 | { |
||
105 | 2 | $this->config = $config; |
|
106 | } |
||
107 | |||
108 | /** |
||
109 | * @param \Eccube\Repository\CsvRepository $csvRepository |
||
110 | */ |
||
111 | 2 | public function setCsvRepository(\Eccube\Repository\CsvRepository $csvRepository) |
|
112 | { |
||
113 | 2 | $this->csvRepository = $csvRepository; |
|
114 | } |
||
115 | |||
116 | /** |
||
117 | * @param \Eccube\Repository\Master\CsvTypeRepository $csvTypeRepository |
||
118 | */ |
||
119 | 2 | public function setCsvTypeRepository(\Eccube\Repository\Master\CsvTypeRepository $csvTypeRepository) |
|
120 | { |
||
121 | 2 | $this->csvTypeRepository = $csvTypeRepository; |
|
122 | } |
||
123 | |||
124 | /** |
||
125 | * @param \Eccube\Repository\OrderRepository $orderRepository |
||
126 | */ |
||
127 | 2 | public function setOrderRepository(\Eccube\Repository\OrderRepository $orderRepository) |
|
128 | { |
||
129 | 2 | $this->orderRepository = $orderRepository; |
|
130 | } |
||
131 | |||
132 | /** |
||
133 | * @param \Eccube\Repository\CustomerRepository $customerRepository |
||
134 | */ |
||
135 | 2 | public function setCustomerRepository(\Eccube\Repository\CustomerRepository $customerRepository) |
|
136 | { |
||
137 | 2 | $this->customerRepository = $customerRepository; |
|
138 | } |
||
139 | |||
140 | /** |
||
141 | * @param \Eccube\Repository\ProductRepository $productRepository |
||
142 | */ |
||
143 | 2 | public function setProductRepository(\Eccube\Repository\ProductRepository $productRepository) |
|
144 | { |
||
145 | 2 | $this->productRepository = $productRepository; |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * @param \Doctrine\ORM\EntityManager $em |
||
150 | */ |
||
151 | 2 | public function setEntityManager(\Doctrine\ORM\EntityManager $em) |
|
152 | { |
||
153 | 2 | $this->em = $em; |
|
154 | } |
||
155 | |||
156 | /** |
||
157 | * @return \Doctrine\ORM\EntityManager |
||
158 | */ |
||
159 | public function getEntityManager() |
||
160 | { |
||
161 | return $this->em; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @param \Doctrine\ORM\QueryBuilder $qb |
||
166 | */ |
||
167 | 1 | public function setExportQueryBuilder(\Doctrine\ORM\QueryBuilder $qb) |
|
171 | |||
172 | /** |
||
173 | * Csv種別からServiceの初期化を行う. |
||
174 | * |
||
175 | * @param $CsvType|integer |
||
176 | */ |
||
177 | 2 | public function initCsvType($CsvType) |
|
178 | { |
||
179 | 2 | if ($CsvType instanceof \Eccube\Entity\Master\CsvType) { |
|
194 | |||
195 | /** |
||
196 | * @return \Eccube\Entity\Csv[] |
||
197 | */ |
||
198 | 1 | public function getCsvs() |
|
202 | |||
203 | /** |
||
204 | * ヘッダ行を出力する. |
||
205 | * このメソッドを使う場合は, 事前にinitCsvType($CsvType)で初期化しておく必要がある. |
||
206 | */ |
||
207 | 1 | public function exportHeader() |
|
222 | |||
223 | /** |
||
224 | * クエリビルダにもとづいてデータ行を出力する. |
||
225 | * このメソッドを使う場合は, 事前にsetExportQueryBuilder($qb)で出力対象のクエリビルダをわたしておく必要がある. |
||
226 | * |
||
227 | * @param \Closure $closure |
||
228 | */ |
||
229 | 1 | public function exportData(\Closure $closure) |
|
230 | { |
||
231 | 1 | if (is_null($this->qb) || is_null($this->em)) { |
|
232 | throw new \LogicException('query builder not set.'); |
||
233 | } |
||
234 | |||
235 | 1 | $this->fopen(); |
|
236 | |||
237 | 1 | $query = $this->qb->getQuery(); |
|
238 | 1 | foreach ($query->getResult() as $iteratableResult) { |
|
239 | 1 | $closure($iteratableResult, $this); |
|
240 | 1 | $this->em->detach($iteratableResult); |
|
241 | 1 | $query->free(); |
|
242 | 1 | flush(); |
|
243 | } |
||
244 | |||
245 | 1 | $this->fclose(); |
|
246 | } |
||
247 | |||
248 | /** |
||
249 | * CSV出力項目と比較し, 合致するデータを返す. |
||
250 | * |
||
251 | * @param \Eccube\Entity\Csv $Csv |
||
252 | * @param $entity |
||
253 | * @return mixed|null|string|void |
||
254 | */ |
||
255 | 1 | public function getData(\Eccube\Entity\Csv $Csv, $entity) |
|
256 | { |
||
257 | // エンティティ名が一致するかどうかチェック. |
||
258 | 1 | $csvEntityName = str_replace('\\\\', '\\', $Csv->getEntityName()); |
|
259 | 1 | $entityName = str_replace('\\\\', '\\', get_class($entity)); |
|
260 | 1 | if ($csvEntityName !== $entityName) { |
|
261 | 1 | $entityName = str_replace('DoctrineProxy\\__CG__\\', '', $entityName); |
|
262 | 1 | if ($csvEntityName !== $entityName) { |
|
263 | 1 | return null; |
|
264 | } |
||
265 | } |
||
266 | |||
267 | // カラム名がエンティティに存在するかどうかをチェック. |
||
268 | 1 | if (!$entity->offsetExists($Csv->getFieldName())) { |
|
269 | return null; |
||
270 | } |
||
271 | |||
272 | // データを取得. |
||
273 | 1 | $data = $entity->offsetGet($Csv->getFieldName()); |
|
274 | |||
275 | // one to one の場合は, dtb_csv.referece_field_nameと比較し, 合致する結果を取得する. |
||
276 | 1 | if ($data instanceof \Eccube\Entity\AbstractEntity) { |
|
277 | 1 | if (EntityUtil::isNotEmpty($data)) { |
|
278 | 1 | return $data->offsetGet($Csv->getReferenceFieldName()); |
|
279 | } |
||
280 | 1 | } elseif ($data instanceof \Doctrine\Common\Collections\Collection) { |
|
281 | // one to manyの場合は, カンマ区切りに変換する. |
||
282 | $array = array(); |
||
283 | foreach ($data as $elem) { |
||
284 | if (EntityUtil::isNotEmpty($elem)) { |
||
285 | $array[] = $elem->offsetGet($Csv->getReferenceFieldName()); |
||
286 | } |
||
287 | } |
||
288 | return implode($this->config['csv_export_multidata_separator'], $array); |
||
289 | |||
290 | 1 | } elseif ($data instanceof \DateTime) { |
|
291 | // datetimeの場合は文字列に変換する. |
||
292 | 1 | return $data->format($this->config['csv_export_date_format']); |
|
293 | |||
294 | } else { |
||
295 | // スカラ値の場合はそのまま. |
||
296 | 1 | return $data; |
|
297 | } |
||
298 | |||
299 | return null; |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * 文字エンコーディングの変換を行うコールバック関数を返す. |
||
304 | * |
||
305 | * @return \Closure |
||
306 | */ |
||
307 | 2 | public function getConvertEncodhingCallback() |
|
308 | { |
||
309 | 2 | $config = $this->config; |
|
310 | |||
311 | return function ($value) use ($config) { |
||
312 | 2 | return mb_convert_encoding( |
|
313 | 2 | (string) $value, $config['csv_export_encoding'], 'UTF-8' |
|
314 | ); |
||
315 | 2 | }; |
|
316 | } |
||
317 | |||
318 | /** |
||
319 | * |
||
320 | */ |
||
321 | 2 | public function fopen() |
|
322 | { |
||
323 | 2 | if (is_null($this->fp) || $this->closed) { |
|
324 | $this->fp = fopen('php://output', 'w'); |
||
325 | } |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * @param $row |
||
330 | * @param null $callback |
||
331 | */ |
||
332 | 2 | public function fputcsv($row) |
|
333 | { |
||
334 | 2 | if (is_null($this->convertEncodingCallBack)) { |
|
335 | 2 | $this->convertEncodingCallBack = $this->getConvertEncodhingCallback(); |
|
336 | } |
||
337 | |||
338 | 2 | fputcsv($this->fp, array_map($this->convertEncodingCallBack, $row), $this->config['csv_export_separator']); |
|
339 | } |
||
340 | |||
341 | /** |
||
342 | * |
||
343 | */ |
||
344 | 2 | public function fclose() |
|
345 | { |
||
346 | 2 | if (!$this->closed) { |
|
347 | 2 | fclose($this->fp); |
|
348 | 2 | $this->closed = true; |
|
349 | } |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * 受注検索用のクエリビルダを返す. |
||
354 | * |
||
355 | * @param Request $request |
||
356 | * @return \Doctrine\ORM\QueryBuilder |
||
357 | */ |
||
358 | View Code Duplication | public function getOrderQueryBuilder(Request $request) |
|
359 | { |
||
360 | $session = $request->getSession(); |
||
361 | if ($session->has('eccube.admin.order.search')) { |
||
362 | $searchData = $session->get('eccube.admin.order.search'); |
||
363 | $this->findDeserializeObjects($searchData); |
||
364 | } else { |
||
365 | $searchData = array(); |
||
366 | } |
||
367 | |||
368 | // 受注データのクエリビルダを構築. |
||
369 | $qb = $this->orderRepository |
||
370 | ->getQueryBuilderBySearchDataForAdmin($searchData); |
||
371 | |||
372 | return $qb; |
||
373 | } |
||
374 | |||
375 | /** |
||
376 | * 会員検索用のクエリビルダを返す. |
||
377 | * |
||
378 | * @param Request $request |
||
379 | * @return \Doctrine\ORM\QueryBuilder |
||
380 | */ |
||
381 | public function getCustomerQueryBuilder(Request $request) |
||
382 | { |
||
383 | $session = $request->getSession(); |
||
384 | $viewData = $session->get('eccube.admin.customer.search', array()); |
||
385 | |||
386 | $app = \Eccube\Application::getInstance(); |
||
387 | $searchForm = $app['form.factory'] |
||
388 | ->create('admin_search_customer', null, array('csrf_protection' => true)); |
||
389 | |||
390 | $searchData = \Eccube\Util\FormUtil::submitAndGetData($searchForm, $viewData); |
||
391 | |||
392 | // 会員データのクエリビルダを構築. |
||
393 | $qb = $this->customerRepository |
||
394 | ->getQueryBuilderBySearchData($searchData); |
||
395 | |||
396 | return $qb; |
||
397 | } |
||
398 | |||
399 | /** |
||
400 | * 商品検索用のクエリビルダを返す. |
||
401 | * |
||
402 | * @param Request $request |
||
403 | * @return \Doctrine\ORM\QueryBuilder |
||
404 | */ |
||
405 | View Code Duplication | public function getProductQueryBuilder(Request $request) |
|
421 | |||
422 | /** |
||
423 | * セッションでシリアライズされた Doctrine のオブジェクトを取得し直す. |
||
424 | * |
||
425 | * XXX self::setExportQueryBuilder() をコールする前に EntityManager を取得したいので、引数で渡している |
||
426 | * |
||
427 | * @param array $searchData セッションから取得した検索条件の配列 |
||
428 | * @param EntityManager $em |
||
429 | */ |
||
430 | protected function findDeserializeObjects(array &$searchData) |
||
447 | } |
||
448 |