Passed
Push — master ( 75715f...53a757 )
by Andy
05:10
created

AbstractCsv::closeDocument()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Palmtree\Csv;
4
5
abstract class AbstractCsv
6
{
7
    /** @var string */
8
    protected $openMode;
9
    /** @var string */
10
    protected $file;
11
    /** @var bool */
12
    protected $hasHeaders;
13
    /** @var string */
14
    protected $delimiter;
15
    /** @var string */
16
    protected $enclosure;
17
    /** @var CsvFileObject */
18
    protected $document;
19
20
    /**
21
     * AbstractCsv constructor.
22
     *
23
     * @param string $file       Path to CSV file.
24
     * @param bool   $hasHeaders Whether the CSV file contains headers.
25
     * @param string $delimiter  Cell delimiter. Default ',' (comma).
26
     * @param string $enclosure  Cell enclosure. Default '"' (double quote)
27
     */
28 5
    public function __construct($file, $hasHeaders = true, $delimiter = ',', $enclosure = '"')
29
    {
30 5
        $this->setFile($file)
31 5
             ->setHasHeaders($hasHeaders)
32 5
             ->setDelimiter($delimiter)
33 5
             ->setEnclosure($enclosure);
34 5
    }
35
36
    /**
37
     * AbstractCsv destructor.
38
     */
39 5
    public function __destruct()
40
    {
41 5
        $this->closeDocument();
42 5
    }
43
44
    /**
45
     * Creates a new SplFileObject instance.
46
     */
47 2
    public function createDocument()
48
    {
49 2
        $this->closeDocument();
50
51 2
        $document = new CsvFileObject($this->getFile(), $this->getOpenMode());
52
53
        $document->setFlags(
54
            CsvFileObject::READ_CSV |
55
            CsvFileObject::READ_AHEAD |
56
            CsvFileObject::SKIP_EMPTY |
57
            CsvFileObject::DROP_NEW_LINE
58
        );
59
60
        $document->setCsvControl($this->getDelimiter(), $this->getEnclosure());
61
62
        $this->setDocument($document);
63
    }
64
65
    /**
66
     * Closes the document by setting our reference to null
67
     * to ensure its destructor is called.
68
     */
69 5
    public function closeDocument()
70
    {
71 5
        $this->document = null;
72 5
    }
73
74
    /**
75
     * @param CsvFileObject|null $document
76
     *
77
     * @return AbstractCsv
78
     */
79
    public function setDocument($document)
80
    {
81
        $this->closeDocument();
82
83
        $this->document = $document;
84
85
        return $this;
86
    }
87
88
    /**
89
     * @return CsvFileObject
90
     */
91
    public function getDocument()
92
    {
93
        if (!$this->document) {
94
            $this->createDocument();
95
        }
96
97
        return $this->document;
98
    }
99
100
    /**
101
     * @return string
102
     */
103 2
    public function getFile()
104
    {
105 2
        return $this->file;
106
    }
107
108
    /**
109
     * @param string $file
110
     *
111
     * @return $this
112
     */
113 5
    public function setFile($file)
114
    {
115 5
        $this->file = $file;
116
117 5
        return $this;
118
    }
119
120
    /**
121
     * @return bool
122
     */
123
    public function hasHeaders()
124
    {
125
        return $this->hasHeaders;
126
    }
127
128
    /**
129
     * @param bool $hasHeaders
130
     *
131
     * @return AbstractCsv
132
     */
133 5
    public function setHasHeaders($hasHeaders)
134
    {
135 5
        $this->hasHeaders = (bool)$hasHeaders;
136
137 5
        return $this;
138
    }
139
140
    /**
141
     * @return string
142
     */
143
    public function getDelimiter()
144
    {
145
        return $this->delimiter;
146
    }
147
148
    /**
149
     * @param string $delimiter
150
     *
151
     * @return AbstractCsv
152
     */
153 5
    public function setDelimiter($delimiter)
154
    {
155 5
        $this->delimiter = $delimiter;
156
157 5
        return $this;
158
    }
159
160
    /**
161
     * @return string
162
     */
163
    public function getEnclosure()
164
    {
165
        return $this->enclosure;
166
    }
167
168
    /**
169
     * @param string $enclosure
170
     *
171
     * @return AbstractCsv
172
     */
173 5
    public function setEnclosure($enclosure)
174
    {
175 5
        $this->enclosure = $enclosure;
176
177 5
        return $this;
178
    }
179
180
    /**
181
     * @return string
182
     */
183 2
    public function getOpenMode()
184
    {
185 2
        return $this->openMode;
186
    }
187
}
188