Completed
Pull Request — master (#20)
by satoru
04:37
created

LargeCsvExport::getTmpFileName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
namespace CsvCombine\LargeExport;
3
use Cake\Network\Exception\MethodNotAllowedException;
4
use Cake\Utility\Security;
5
use Cake\Filesystem\File;
6
7
/**
8
 * LargeCsvExport  code license:
9
 *
10
 * @copyright Copyright (C) 2011 hagiwara.
11
 * @since CakePHP(tm) v 1.3
12
 * @license http://www.opensource.org/licenses/mit-license.php The MIT License
13
 */
14
class LargeCsvExport {
15
16
    private $tmpCsvFp;
17
    private $defaultSettings = [
18
        'delimiter' => ',',
19
        'export_encoding' => 'SJIS-win',
20
        'array_encoding' => 'UTF-8',
21
    ];
22
    private $settings;
23
24
    /**
25
     * __construct
26
     *
27
     */
28
    public function __construct($settings = [])
29
    {
30
        // 設定値
31
        $this->settings = array_merge(
32
            $this->defaultSettings,
33
            $settings
34
        );
35
        $this->tmpCsvFp = new File($this->getTmpFileName());
36
    }
37
38
    /**
39
     * getTmpFileName
40
     * 一時ファイルの取得
41
     */
42
    private function getTmpFileName()
43
    {
44
        $tmpFileName = TMP . 'csv_file_' . Security::hash(time() . rand());
45
        // ファイルが存在した場合は何もしない
46
        if (file_exists($tmpFileName)) {
47
            return $this->getTmpFileName();
48
        }
49
        return $tmpFileName;
50
    }
51
52
    /**
53
     * addRow
54
     * 都度都度ファイルに追記をしていく
55
     */
56
    public function addRow($lists)
57
    {
58
        if (!is_array($lists)) {
59
            throw new MethodNotAllowedException('$list must be array.');
60
        }
61
        $csvRow = $this->parseCsv($lists);
62
        $this->tmpCsvFp->write($csvRow, 'a');
63
    }
64
65
    /**
66
     * read
67
     * csvテキストデータの読み込み(及び一時ファイルの削除)
68
     */
69
    public function read()
70
    {
71
        $csvText = $this->tmpCsvFp->read();
72
        // ファイル削除
73
        $this->tmpCsvFp->delete();
74
        $this->tmpCsvFp->close();
75
        return $csvText;
76
    }
77
78
    /**
79
     * getPath
80
     * ファイルパスの取得。readメソッドでメモリで落ちる場合はパスを取得してrequest->fileでDLする
81
     */
82
    public function getPath()
83
    {
84
        return $this->tmpCsvFp->pwd();
85
    }
86
87
    /*
88
     * _parseCsv
89
     * csv(など)の形式に変更
90
     *
91
     * @param array $lists 変換する値
92
     * @param string 1行分のCSVテキストデータ
93
     */
94
    private function parseCsv($lists)
95
    {
96
        // 文字コードの変換
97
        mb_convert_variables($this->settings['export_encoding'], $this->settings['array_encoding'], $lists);
98
        foreach ($lists as $listKey => $list) {
99
            //区切り文字・改行・ダブルクオートの時
100 View Code Duplication
            if (preg_match('/[' . $this->settings['delimiter'] . '\\n"]/', $list)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
                $list = str_replace('"', '""', $list);
102
                $lists[$listKey] = '"' . $list . '"';
103
            }
104
        }
105
        // カンマ区切り
106
        return implode($this->settings['delimiter'], $lists) . "\r\n";
107
    }
108
109
}
110