Passed
Branch feature/php7 (3e8a2f)
by Andy
04:54
created

AbstractCsv   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Test Coverage

Coverage 61.22%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 17
eloc 36
dl 0
loc 126
ccs 30
cts 49
cp 0.6122
rs 10
c 3
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A setDelimiter() 0 5 1
A getDocument() 0 7 2
A setFile() 0 5 1
A setHasHeaders() 0 5 1
A closeDocument() 0 3 1
A getFile() 0 3 1
A setEnclosure() 0 5 1
A hasHeaders() 0 3 1
A setEscapeCharacter() 0 5 1
A getDelimiter() 0 3 1
A getEnclosure() 0 3 1
A createDocument() 0 10 1
A __construct() 0 3 1
A getEscapeCharacter() 0 3 1
A setDocument() 0 7 1
A __destruct() 0 3 1
1
<?php
2
3
namespace Palmtree\Csv;
4
5
abstract class AbstractCsv
6
{
7
    /** @var string Path to CSV file. */
8
    protected $file;
9
    /** @var bool Whether the CSV file contains headers */
10
    protected $hasHeaders = true;
11
    /** @var string Cell delimiter. Default ',' (comma) */
12
    protected $delimiter = ',';
13
    /** @var string Cell enclosure. Default '"' (double quote) */
14
    protected $enclosure = '"';
15
    /** @var string Cell escape character. Default null byte */
16
    protected $escapeCharacter = "\0";
17
    /** @var CsvFileObject */
18
    protected $document;
19
20
    /**
21
     * @param string $file Path to CSV file.
22
     */
23 9
    public function __construct(string $file)
24
    {
25 9
        $this->setFile($file);
26 9
    }
27
28 5
    public function __destruct()
29
    {
30 5
        $this->closeDocument();
31 5
    }
32
33
    abstract public function getOpenMode(): string;
34
35 6
    public function createDocument(): void
36
    {
37 6
        $this->closeDocument();
38
39 6
        $document = new CsvFileObject($this->file, $this->getOpenMode());
40
41 5
        $document->setFlags(CsvFileObject::READ_CSV);
42 5
        $document->setCsvControl($this->delimiter, $this->enclosure, $this->escapeCharacter);
43
44 5
        $this->setDocument($document);
45 5
    }
46
47
    /**
48
     * Closes the document by setting our reference to null to ensure its destructor is called.
49
     */
50 9
    public function closeDocument(): void
51
    {
52 9
        $this->document = null;
53 9
    }
54
55 5
    public function setDocument(CsvFileObject $document = null): self
56
    {
57 5
        $this->closeDocument();
58
59 5
        $this->document = $document;
60
61 5
        return $this;
62
    }
63
64 5
    public function getDocument(): CsvFileObject
65
    {
66 5
        if (!$this->document) {
67 5
            $this->createDocument();
68
        }
69
70 5
        return $this->document;
71
    }
72
73
    public function getFile(): string
74
    {
75
        return $this->file;
76
    }
77
78 9
    public function setFile(string $file): self
79
    {
80 9
        $this->file = $file;
81
82 9
        return $this;
83
    }
84
85
    public function hasHeaders(): bool
86
    {
87
        return $this->hasHeaders;
88
    }
89
90 1
    public function setHasHeaders(bool $hasHeaders): self
91
    {
92 1
        $this->hasHeaders = (bool)$hasHeaders;
93
94 1
        return $this;
95
    }
96
97
    public function getDelimiter(): string
98
    {
99
        return $this->delimiter;
100
    }
101
102
    public function setDelimiter(string $delimiter): self
103
    {
104
        $this->delimiter = $delimiter;
105
106
        return $this;
107
    }
108
109
    public function getEnclosure(): string
110
    {
111
        return $this->enclosure;
112
    }
113
114
    public function setEnclosure(string $enclosure): self
115
    {
116
        $this->enclosure = $enclosure;
117
118
        return $this;
119
    }
120
121
    public function getEscapeCharacter(): string
122
    {
123
        return $this->escapeCharacter;
124
    }
125
126
    public function setEscapeCharacter(string $escapeCharacter): self
127
    {
128
        $this->escapeCharacter = $escapeCharacter;
129
130
        return $this;
131
    }
132
}
133